|
| 1 | +name: Release on merge |
| 2 | + |
| 3 | +# Auto-tag + auto-publish GitHub Release whenever VERSION changes on main. |
| 4 | +# Composes with validate-release.yml (PR gate) so this workflow trusts that |
| 5 | +# the merged VERSION is semver, monotonic, and matched by a CHANGELOG section. |
| 6 | +# |
| 7 | +# Idempotent: if `vX.Y.Z` already exists (e.g. tagged manually before this |
| 8 | +# workflow shipped) the run skips silently — no overwrite, no duplicate. |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: [main] |
| 13 | + paths: |
| 14 | + - VERSION |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write # tag push + gh release create |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: release-on-merge |
| 21 | + cancel-in-progress: false |
| 22 | + |
| 23 | +jobs: |
| 24 | + release: |
| 25 | + name: Tag + GitHub Release |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 # full history so we can check existing tags |
| 31 | + |
| 32 | + - name: Read VERSION |
| 33 | + id: version |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | + v="$(tr -d '[:space:]' < VERSION)" |
| 37 | + if ! printf '%s' "$v" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then |
| 38 | + echo "::error file=VERSION::not semver X.Y.Z: '$v'" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + echo " version=$v" |
| 42 | + echo "version=$v" >> "$GITHUB_OUTPUT" |
| 43 | + echo "tag=v$v" >> "$GITHUB_OUTPUT" |
| 44 | +
|
| 45 | + - name: Check existing tag |
| 46 | + id: tag_check |
| 47 | + run: | |
| 48 | + set -euo pipefail |
| 49 | + tag="${{ steps.version.outputs.tag }}" |
| 50 | + if git rev-parse "$tag" >/dev/null 2>&1; then |
| 51 | + echo " $tag already exists — skipping release." |
| 52 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 53 | + else |
| 54 | + echo " $tag is new — will create." |
| 55 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 56 | + fi |
| 57 | +
|
| 58 | + - name: Extract release notes from CHANGELOG |
| 59 | + if: steps.tag_check.outputs.exists == 'false' |
| 60 | + id: notes |
| 61 | + run: | |
| 62 | + set -euo pipefail |
| 63 | + v="${{ steps.version.outputs.version }}" |
| 64 | + awk -v ver="$v" ' |
| 65 | + $0 ~ "^## " ver "( |$)" { flag=1; next } |
| 66 | + flag && /^## / { exit } |
| 67 | + flag { print } |
| 68 | + ' CHANGELOG.md > /tmp/release-notes.md |
| 69 | + if [ ! -s /tmp/release-notes.md ]; then |
| 70 | + echo "::error file=CHANGELOG.md::no '## $v' section found" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + # Title = first `### ` heading inside the section, else fall back to vX.Y.Z. |
| 74 | + title=$(awk '/^### / { sub(/^### /, ""); print; exit }' /tmp/release-notes.md) |
| 75 | + [ -z "$title" ] && title="${{ steps.version.outputs.tag }}" |
| 76 | + echo " title=$title" |
| 77 | + # Multi-line outputs need the heredoc form. |
| 78 | + { |
| 79 | + echo "title<<EOT" |
| 80 | + echo "$title" |
| 81 | + echo "EOT" |
| 82 | + } >> "$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + - name: Tag + create GitHub Release |
| 85 | + if: steps.tag_check.outputs.exists == 'false' |
| 86 | + env: |
| 87 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | + tag="${{ steps.version.outputs.tag }}" |
| 91 | + title="${{ steps.notes.outputs.title }}" |
| 92 | + git config user.name "github-actions[bot]" |
| 93 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 94 | + git tag -a "$tag" -m "$tag — $title" |
| 95 | + git push origin "$tag" |
| 96 | + gh release create "$tag" \ |
| 97 | + --title "$tag — $title" \ |
| 98 | + --notes-file /tmp/release-notes.md |
| 99 | + echo " ✓ released $tag" |
0 commit comments