|
| 1 | +name: Release on Tag Push |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Extract version from tag |
| 19 | + id: version |
| 20 | + run: echo "tag=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT" |
| 21 | + |
| 22 | + - name: Extract changelog for this version |
| 23 | + id: changelog |
| 24 | + run: | |
| 25 | + TAG="${{ steps.version.outputs.tag }}" |
| 26 | + # Extract the section for this tag from changelog |
| 27 | + # Matches from "## vX.Y.Z" (with optional suffix) to the next "## " or EOF |
| 28 | + BODY=$(awk -v tag="$TAG" ' |
| 29 | + BEGIN { found=0 } |
| 30 | + /^## / { |
| 31 | + if (found) exit |
| 32 | + # Match the tag, allowing optional suffix after version (e.g. "- Breaking change") |
| 33 | + header=$2 |
| 34 | + # Remove trailing characters after version pattern |
| 35 | + gsub(/[^v0-9.].*/, "", header) |
| 36 | + if (header == tag) { found=1; next } |
| 37 | + } |
| 38 | + found { print } |
| 39 | + ' docs/changelog.md) |
| 40 | +
|
| 41 | + # Fail if no changelog found |
| 42 | + if [ -z "$BODY" ]; then |
| 43 | + echo "::warning::No changelog entry found for $TAG, using default message" |
| 44 | + BODY="Release $TAG" |
| 45 | + fi |
| 46 | +
|
| 47 | + # Write to file to preserve multiline content |
| 48 | + echo "$BODY" > /tmp/release-body.md |
| 49 | +
|
| 50 | + - name: Delete existing release if present |
| 51 | + run: | |
| 52 | + TAG="${{ steps.version.outputs.tag }}" |
| 53 | + if gh release view "$TAG" &>/dev/null; then |
| 54 | + echo "Deleting existing release for $TAG" |
| 55 | + gh release delete "$TAG" --yes |
| 56 | + fi |
| 57 | + env: |
| 58 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 59 | + |
| 60 | + - name: Create GitHub Release |
| 61 | + run: | |
| 62 | + TAG="${{ steps.version.outputs.tag }}" |
| 63 | + gh release create "$TAG" \ |
| 64 | + --title "$TAG" \ |
| 65 | + --notes-file /tmp/release-body.md |
| 66 | + env: |
| 67 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments