#!/bin/sh set -e # version msg="${CI_COMMIT_MESSAGE:-}" bump=patch echo "$msg" | grep -qi minor: && bump=minor echo "$msg" | grep -qi major: && bump=major cur=$(awk '/"version"/ { match($0, /[0-9]+\.[0-9]+\.[0-9]+/); print substr($0, RSTART, RLENGTH); exit }' package.json) major=$(echo "$cur" | cut -d. -f1); minor=$(echo "$cur" | cut -d. -f2); patch=$(echo "$cur" | cut -d. -f3) case "$bump" in major) major=$((major+1)); minor=0; patch=0 ;; minor) minor=$((minor+1)); patch=0 ;; patch) patch=$((patch+1)) ;; esac newVersion="$major.$minor.$patch" [ -z "$cur" ] && { echo "error: could not read version from package.json"; exit 1; } # changelog entry (strip prefix from first line) changelogEntry=$(echo "$msg" | head -1 | awk '{sub(/^[mM]ajor:[ \t]*/,""); sub(/^[mM]inor:[ \t]*/,""); sub(/^[pP]atch:[ \t]*/,""); print}') [ -z "$changelogEntry" ] && changelogEntry="Release v$newVersion" # bump files awk -v v="$newVersion" '/"version"/ { sub(/[0-9]+\.[0-9]+\.[0-9]+/, v) } { print }' package.json > package.json.tmp && mv package.json.tmp package.json awk -v v="$newVersion" '/^version:/ { $0 = "version: " v }; /^appVersion:/ { $0 = "appVersion: \"" v "\"" }; { print }' helm/kestrelos/Chart.yaml > helm/kestrelos/Chart.yaml.tmp && mv helm/kestrelos/Chart.yaml.tmp helm/kestrelos/Chart.yaml awk -v v="$newVersion" '/^ tag:/ { $0 = " tag: " v }; { print }' helm/kestrelos/values.yaml > helm/kestrelos/values.yaml.tmp && mv helm/kestrelos/values.yaml.tmp helm/kestrelos/values.yaml # changelog new="## [$newVersion] - $(date +%Y-%m-%d) ### Changed - $changelogEntry " { [ ! -f CHANGELOG.md ] && printf '# Changelog\n\n'; printf '%s' "$new"; [ -f CHANGELOG.md ] && cat CHANGELOG.md; } > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md # git git config user.email "ci@kestrelos" && git config user.name "CI" git add package.json helm/kestrelos/Chart.yaml helm/kestrelos/values.yaml CHANGELOG.md git commit -m "release v$newVersion [skip ci]" url="https://${CI_REPO_OWNER}:${GITEA_REPO_TOKEN}@${CI_FORGE_URL#https://}/${CI_REPO_OWNER}/${CI_REPO_NAME}.git" git tag "v$newVersion" # artifact for kaniko (tag list) printf '%s\n%s\n' "$newVersion" "latest" > .tags retry() { n=0; while ! "$@"; do n=$((n+1)); [ $n -ge 3 ] && return 1; sleep 2; done; } retry git push "$url" HEAD:main "v$newVersion" # gitea release body="## Changelog ### Changed - $changelogEntry ## Installation - [Docker image](${CI_FORGE_URL}/${CI_REPO_OWNER}/-/packages/container/${CI_REPO_NAME}) - [Helm chart](${CI_FORGE_URL}/${CI_REPO_OWNER}/-/packages/helm/${CI_REPO_NAME})" release_url="${CI_FORGE_URL}/api/v1/repos/${CI_REPO_OWNER}/${CI_REPO_NAME}/releases" echo "$body" | awk -v tag="v$newVersion" 'BEGIN{printf "{\"tag_name\":\"" tag "\",\"name\":\"" tag "\",\"body\":\""} { gsub(/\\/,"\\\\"); gsub(/"/,"\\\""); if (NR>1) printf "\\n"; printf "%s", $0 } END{printf "\"}\n"}' > /tmp/release.json wget -q -O /dev/null --post-file=/tmp/release.json \ --header="Authorization: token ${GITEA_REPO_TOKEN}" \ --header="Content-Type: application/json" \ "$release_url"