|
| 1 | +# After a Renovate PR merges into `main`, promote the `[Unreleased]` section |
| 2 | +# of CHANGELOG.md to the next patch version, commit, tag, push, and back-merge |
| 3 | +# main into develop so the branches stay in sync. |
| 4 | +# |
| 5 | +# Required secrets: |
| 6 | +# RELEASE_TOKEN PAT or GitHub App token with contents:write + workflows. |
| 7 | +# Needed for the tag push to trigger github_build_release.yml |
| 8 | +# (the default GITHUB_TOKEN cannot trigger other workflows) |
| 9 | +# and for the back-merge push to `develop`. |
| 10 | +# |
| 11 | +# Gating: the existing pr.yaml is required by branch protection, so by the time |
| 12 | +# a PR is merged into main, CI has already passed. We do not re-wait for checks |
| 13 | +# on the merge commit (it has no associated check runs after a squash-merge). |
| 14 | + |
| 15 | +name: Auto Release on Renovate Merge |
| 16 | + |
| 17 | +on: |
| 18 | + pull_request: |
| 19 | + types: [closed] |
| 20 | + branches: [main] |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +jobs: |
| 26 | + release: |
| 27 | + if: >- |
| 28 | + github.event.pull_request.merged == true && |
| 29 | + github.event.pull_request.user.login == 'renovate[bot]' |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - name: Checkout main with full history |
| 33 | + uses: actions/checkout@v6 |
| 34 | + with: |
| 35 | + ref: main |
| 36 | + fetch-depth: 0 |
| 37 | + token: ${{ secrets.RELEASE_TOKEN }} |
| 38 | + |
| 39 | + - name: Compute next patch version |
| 40 | + id: version |
| 41 | + run: | |
| 42 | + latest=$(git tag -l '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -1) |
| 43 | + : "${latest:=0.0.0}" |
| 44 | + IFS=. read -r maj min pat <<<"$latest" |
| 45 | + next="${maj}.${min}.$((pat + 1))" |
| 46 | + { |
| 47 | + echo "current=$latest" |
| 48 | + echo "next=$next" |
| 49 | + echo "date=$(date -u +%Y-%m-%d)" |
| 50 | + } >>"$GITHUB_OUTPUT" |
| 51 | + echo "Will release: $latest -> $next" |
| 52 | +
|
| 53 | + - name: Bail if [Unreleased] is empty |
| 54 | + id: gate |
| 55 | + run: | |
| 56 | + body=$(awk '/^## \[Unreleased\]/{f=1;next} /^## \[/{f=0} f' CHANGELOG.md \ |
| 57 | + | grep -v '^[[:space:]]*$' || true) |
| 58 | + if [ -z "$body" ]; then |
| 59 | + echo "Nothing under [Unreleased]; skipping release." |
| 60 | + echo "skip=true" >>"$GITHUB_OUTPUT" |
| 61 | + else |
| 62 | + echo "skip=false" >>"$GITHUB_OUTPUT" |
| 63 | + fi |
| 64 | +
|
| 65 | + - name: Promote [Unreleased] -> ${{ steps.version.outputs.next }} |
| 66 | + if: steps.gate.outputs.skip != 'true' |
| 67 | + env: |
| 68 | + VER: ${{ steps.version.outputs.next }} |
| 69 | + DATE: ${{ steps.version.outputs.date }} |
| 70 | + run: | |
| 71 | + python3 - <<'PY' |
| 72 | + import os, re, pathlib |
| 73 | + ver = os.environ["VER"] |
| 74 | + date = os.environ["DATE"] |
| 75 | + p = pathlib.Path("CHANGELOG.md") |
| 76 | + text = p.read_text(encoding="utf-8") |
| 77 | + text = re.sub( |
| 78 | + r"## \[Unreleased\]\n", |
| 79 | + f"## [Unreleased]\n\n## [{ver}] - {date}\n", |
| 80 | + text, count=1, |
| 81 | + ) |
| 82 | + p.write_text(text, encoding="utf-8") |
| 83 | + PY |
| 84 | +
|
| 85 | + - name: Commit on main, tag, push |
| 86 | + if: steps.gate.outputs.skip != 'true' |
| 87 | + env: |
| 88 | + VER: ${{ steps.version.outputs.next }} |
| 89 | + run: | |
| 90 | + git config user.name "github-actions[bot]" |
| 91 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 92 | + git add CHANGELOG.md |
| 93 | + git commit -m "release: ${VER}" |
| 94 | + git tag -a "${VER}" -m "Release ${VER}" |
| 95 | + git push origin main |
| 96 | + git push origin "${VER}" |
| 97 | +
|
| 98 | + - name: Back-merge main into develop |
| 99 | + if: steps.gate.outputs.skip != 'true' |
| 100 | + env: |
| 101 | + VER: ${{ steps.version.outputs.next }} |
| 102 | + GH_TOKEN: ${{ secrets.RELEASE_TOKEN }} |
| 103 | + run: | |
| 104 | + git fetch origin develop |
| 105 | + git checkout -B develop origin/develop |
| 106 | + if git merge --no-ff main -m "chore: back-merge hotfix ${VER} into develop"; then |
| 107 | + git push origin develop |
| 108 | + echo "Back-merged ${VER} to develop." |
| 109 | + else |
| 110 | + git merge --abort || true |
| 111 | + gh pr create \ |
| 112 | + --repo "${GITHUB_REPOSITORY}" \ |
| 113 | + --base develop \ |
| 114 | + --head main \ |
| 115 | + --title "Back-merge hotfix ${VER} into develop" \ |
| 116 | + --body "Automated back-merge from \`main\` conflicted with \`develop\`. Resolve and merge to keep branches in sync after release ${VER}." |
| 117 | + fi |
0 commit comments