| 1 | #!/usr/bin/env bash |
| 2 | # Create the BlueGit release on CI_COMMIT_SHA with the build artifacts as |
| 3 | # assets. The release API creates the tag itself (bare version, e.g. 0.5.9). |
| 4 | set -euo pipefail |
| 5 | : "${CI_API_URL:?}" "${CI_JOB_TOKEN:?}" "${CI_COMMIT_SHA:?}" "${CI_PROJECT_PATH:?}" |
| 6 | cd "$(dirname "$0")/../.." |
| 7 | v="$(cat artifacts/version.txt)" |
| 8 | mkdir -p artifacts |
| 9 | skip() { echo "No release: $1"; echo skip > artifacts/released.txt; exit 0; } |
| 10 | # Never release twice: not when HEAD already carries a tag, and not when only |
| 11 | # docs/CI files changed since the last version tag. |
| 12 | git fetch -q --tags origin 2>/dev/null || true |
| 13 | if [[ -n "$(git tag --points-at HEAD)" ]]; then skip "HEAD is already tagged ($(git tag --points-at HEAD | tr '\n' ' '))"; fi |
| 14 | last="$(git tag -l | sed 's/^v//' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1 || true)" |
| 15 | if [[ -n "$last" ]]; then |
| 16 | lt="$(git tag -l "$last" "v$last" | head -n1)" |
| 17 | changed="$(git diff --name-only "$lt" HEAD -- . ':(exclude)*.md' ':(exclude)docs/' ':(exclude)blue-ci-cd.yml' ':(exclude).gitlab-ci.yml' ':(exclude).cursor/' || true)" |
| 18 | [[ -n "$changed" ]] || skip "only docs/CI changed since $lt" |
| 19 | fi |
| 20 | [[ "$v" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "Invalid version: $v" >&2; exit 1; } |
| 21 | now="$(bash scripts/ci/next-version.sh)" |
| 22 | [[ "$now" == "$v" ]] || { echo "Version $v is already taken (next is $now). Rerun the pipeline." >&2; exit 1; } |
| 23 | asset() { printf '{"job":"%s","path":"%s","name":"%s"}' "$1" "$2" "$3"; } |
| 24 | assets="$(asset build:linux-deb dist/amd64.deb "posprint_${v}-1_amd64.deb"),$(asset build:linux-arch dist/x86_64.pkg.tar.zst "posprint-${v}-1-x86_64.pkg.tar.zst"),$(asset build:linux-arch dist/x86_64-bin.tar.gz "posprint-${v}-x86_64.tar.gz"),$(asset build:windows dist/posprint.exe posprint.exe),$(asset build:macos dist/macos-universal.zip "posprint-${v}-macos-universal.zip")" |
| 25 | notes="" |
| 26 | if [[ -s RELEASE_NOTES.md ]]; then notes="$(python3 -c 'import json,sys;print(json.dumps(open("RELEASE_NOTES.md").read().strip()+"\n\n")[1:-1])' 2>/dev/null || sed ':a;N;$!ba;s/\\/\\\\/g;s/"/\\"/g;s/\n/\\n/g' RELEASE_NOTES.md)\n\n"; fi |
| 27 | desc="${notes}"'* Debian/Ubuntu: posprint_<version>-1_amd64.deb\n* Arch: posprint-<version>-1-x86_64.pkg.tar.zst\n* Linux binary tarball (AUR posprint-bin): posprint-<version>-x86_64.tar.gz\n* Windows: posprint.exe\n* macOS (universal, niet ondertekend): posprint-<version>-macos-universal.zip\n\nAUR: posprint, posprint-bin.' |
| 28 | body="$(printf '{"tag":"%s","commit":"%s","name":"posprint %s","body":"%s","assets":[%s]}' "$v" "$CI_COMMIT_SHA" "$v" "$desc" "$assets")" |
| 29 | echo "Release $v on $CI_COMMIT_SHA" |
| 30 | resp="$(mktemp)" |
| 31 | code="$(curl -sS -o "$resp" -w '%{http_code}' -X POST "$CI_API_URL/repos/$CI_PROJECT_PATH/releases" \ |
| 32 | -H "Authorization: Bearer $CI_JOB_TOKEN" -H "Content-Type: application/json" -d "$body")" || code=000 |
| 33 | cat "$resp"; echo |
| 34 | [[ "$code" == 2* ]] || { echo "Release API returned HTTP $code" >&2; exit 1; } |
| 35 | echo "$v" > artifacts/released.txt |