name: Release # Push a tag like v0.1.0 (it must match `.version` in build.zig.zon) to build the binaries for # every platform and publish a release. on: push: tags: ['v*'] workflow_dispatch: inputs: tag: description: 'Existing tag to build and publish (e.g. v0.1.0)' required: true env: TAG: ${{ inputs.tag || github.ref_name }} jobs: release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: ref: ${{ env.TAG }} - name: Install Zig run: echo "$(.github/ci/install-zig.sh)" >> "$GITHUB_PATH" - name: Check the tag matches build.zig.zon run: | version=$(sed -n 's/^[[:space:]]*\.version = "\(.*\)",$/\1/p' build.zig.zon) [ "v$version" = "$TAG" ] || { echo "tag $TAG != v$version"; exit 1; } - name: Test run: | zig build test --summary all zig build e2e - name: Build release binaries run: | mkdir -p dist build() { # build zig build -Dtarget="$1" -Doptimize=ReleaseSafe -Dstrip=true --prefix "zig-out/$1" cp "zig-out/$1/bin/zag" "dist/zag-$2" } build x86_64-linux-musl linux-x64 build aarch64-linux-musl linux-arm64 build aarch64-macos macos-arm64 build x86_64-macos macos-x64 (cd dist && sha256sum zag-* > SHA256SUMS) cp install.sh dist/install.sh - name: Publish the release env: # RELEASE_TOKEN (a personal access token with write:repository) wins when the # built-in token is not allowed to create releases. TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }} run: | api="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" notes=$(awk -v v="${TAG#v}" '$0 ~ "^## \\[?"v {f=1; next} /^## / {f=0} f' CHANGELOG.md) body=$(printf '%s' "$notes" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))' 2>/dev/null || echo '""') resp=$(mktemp) code=$(curl -sS -o "$resp" -w '%{http_code}' -X POST "$api/releases" \ -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"body\":${body}}") if [ "$code" = "409" ]; then # The release already exists (e.g. an earlier run created it): reuse it. code=$(curl -sS -o "$resp" -w '%{http_code}' -H "Authorization: token $TOKEN" "$api/releases/tags/$TAG") fi if [ "$code" != "201" ] && [ "$code" != "200" ]; then echo "creating the release failed: HTTP $code"; cat "$resp"; echo exit 1 fi # The release's own id is the first key; later "id"s belong to nested objects. id=$(sed -n 's/^{"id":\([0-9]*\),.*/\1/p' "$resp" | head -1) [ -n "$id" ] || { echo "could not read the release id"; cat "$resp"; echo; exit 1; } for f in dist/*; do echo "uploading $f" code=$(curl -sS -o "$resp" -w '%{http_code}' -X POST "$api/releases/$id/assets?name=$(basename "$f")" \ -H "Authorization: token $TOKEN" -F "attachment=@$f") [ "$code" = "201" ] || { echo "upload of $f failed: HTTP $code"; cat "$resp"; echo; exit 1; } done - name: Update the Homebrew tap # Needs a TAP_TOKEN secret: a personal access token with write access to # samson-media/homebrew-tap. Skipped when the secret is not set. env: TAP_TOKEN: ${{ secrets.TAP_TOKEN }} run: | if [ -z "$TAP_TOKEN" ]; then echo "TAP_TOKEN not set; skipping the tap update"; exit 0; fi tap=$(mktemp -d) git clone "https://x-access-token:${TAP_TOKEN}@${GITHUB_SERVER_URL#https://}/samson-media/homebrew-tap.git" "$tap" mkdir -p "$tap/Formula" sh .github/ci/render-formula.sh "${TAG#v}" dist/SHA256SUMS > "$tap/Formula/zag.rb" cd "$tap" git config user.name "zag release workflow" git config user.email "actions@gitea.samson.media" git add Formula/zag.rb if git diff --cached --quiet; then echo "formula unchanged"; exit 0; fi git commit -m "Update zag to ${TAG#v}" git push origin HEAD:main