|
| 1 | +name: Release prepare |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump_type: |
| 7 | + description: 'Version bump type (ignored when version is set)' |
| 8 | + required: false |
| 9 | + default: patch |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | + version: |
| 16 | + description: 'Explicit version override, e.g. 1.2.3 or 1.2.3-rc1 (ignores bump_type)' |
| 17 | + required: false |
| 18 | + default: '' |
| 19 | + type: string |
| 20 | + expected_current: |
| 21 | + description: 'Safety check: fail if gradle.properties version does not match this value' |
| 22 | + required: false |
| 23 | + default: '' |
| 24 | + type: string |
| 25 | + dry_run: |
| 26 | + description: 'Dry run: validate and print plan only, skip commit/push' |
| 27 | + required: false |
| 28 | + default: true |
| 29 | + type: boolean |
| 30 | + |
| 31 | +permissions: |
| 32 | + contents: read |
| 33 | + |
| 34 | +concurrency: |
| 35 | + group: release-prepare-main |
| 36 | + cancel-in-progress: false |
| 37 | + |
| 38 | +jobs: |
| 39 | + compute-and-validate: |
| 40 | + name: Compute and validate |
| 41 | + permissions: |
| 42 | + contents: read |
| 43 | + runs-on: ubuntu-24.04 |
| 44 | + outputs: |
| 45 | + current_name: ${{ steps.read-version.outputs.current }} |
| 46 | + new_name: ${{ steps.compute-version.outputs.new }} |
| 47 | + |
| 48 | + steps: |
| 49 | + - name: Guard — must run on main |
| 50 | + env: |
| 51 | + REF: ${{ github.ref }} |
| 52 | + run: | |
| 53 | + set -euo pipefail |
| 54 | + if [[ "$REF" != "refs/heads/main" ]]; then |
| 55 | + echo "::error::This workflow must be dispatched from main (got $REF)" |
| 56 | + exit 1 |
| 57 | + fi |
| 58 | +
|
| 59 | + - name: Checkout main |
| 60 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2 |
| 61 | + with: |
| 62 | + ref: main |
| 63 | + fetch-depth: 0 |
| 64 | + persist-credentials: false |
| 65 | + |
| 66 | + - name: Read current version |
| 67 | + id: read-version |
| 68 | + env: |
| 69 | + EXPECTED_CURRENT: ${{ inputs.expected_current }} |
| 70 | + run: | |
| 71 | + set -euo pipefail |
| 72 | + count=$(grep -cE '^version=' gradle.properties || true) |
| 73 | + [[ "$count" == "1" ]] || { echo "::error::expected exactly one 'version=' line in gradle.properties, found $count"; exit 1; } |
| 74 | + current=$(awk -F= '$1=="version"{print $2}' gradle.properties) |
| 75 | + echo "$current" | grep -qE '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(rc|beta)(0|[1-9][0-9]*))?$' || { |
| 76 | + echo "::error::current version '$current' does not match semver format" |
| 77 | + exit 1 |
| 78 | + } |
| 79 | + if [[ -n "$EXPECTED_CURRENT" && "$current" != "$EXPECTED_CURRENT" ]]; then |
| 80 | + echo "::error::current version '$current' does not match expected_current '$EXPECTED_CURRENT'" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + echo "current=$current" >> "$GITHUB_OUTPUT" |
| 84 | +
|
| 85 | + - name: Compute next version |
| 86 | + id: compute-version |
| 87 | + env: |
| 88 | + CURRENT: ${{ steps.read-version.outputs.current }} |
| 89 | + BUMP_TYPE: ${{ inputs.bump_type }} |
| 90 | + OVERRIDE: ${{ inputs.version }} |
| 91 | + run: | |
| 92 | + set -euo pipefail |
| 93 | + new=$(python3 - <<'PYEOF' |
| 94 | + import re, sys, os |
| 95 | + semver = re.compile(r'^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-(rc|beta)(0|[1-9][0-9]*))?$') |
| 96 | + def parse(v): |
| 97 | + m = semver.match(v) |
| 98 | + if not m: sys.exit(f"invalid version: {v}") |
| 99 | + return (int(m[1]), int(m[2]), int(m[3]), m[4], int(m[5]) if m[5] else None) |
| 100 | + def precedence(p): |
| 101 | + return (p[0], p[1], p[2], 1 if p[3] is None else 0, p[3] or '', p[4] or 0) |
| 102 | + cur = parse(os.environ['CURRENT']) |
| 103 | + override = os.environ.get('OVERRIDE') or None |
| 104 | + if override: |
| 105 | + nxt = parse(override) |
| 106 | + else: |
| 107 | + if cur[3] is not None: |
| 108 | + sys.exit("bump_type rejected on prerelease current; use explicit version override") |
| 109 | + x, y, z = cur[:3] |
| 110 | + b = os.environ['BUMP_TYPE'] |
| 111 | + if b == 'patch': z += 1 |
| 112 | + elif b == 'minor': y, z = y + 1, 0 |
| 113 | + elif b == 'major': x, y, z = x + 1, 0, 0 |
| 114 | + else: sys.exit(f"invalid bump_type: {b}") |
| 115 | + nxt = (x, y, z, None, None) |
| 116 | + if precedence(nxt) <= precedence(cur): |
| 117 | + sys.exit(f"refusing downgrade: {nxt} not > {cur}") |
| 118 | + pre = f"-{nxt[3]}{nxt[4]}" if nxt[3] else '' |
| 119 | + print(f"{nxt[0]}.{nxt[1]}.{nxt[2]}{pre}") |
| 120 | + PYEOF |
| 121 | + ) |
| 122 | + echo "new=$new" >> "$GITHUB_OUTPUT" |
| 123 | +
|
| 124 | + - name: Check tag collision |
| 125 | + env: |
| 126 | + NEW: ${{ steps.compute-version.outputs.new }} |
| 127 | + run: | |
| 128 | + set -euo pipefail |
| 129 | + if git rev-parse --verify "refs/tags/v${NEW}" >/dev/null 2>&1; then |
| 130 | + echo "::error::Local tag v${NEW} already exists" |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + if git ls-remote --exit-code --tags origin "refs/tags/v${NEW}" >/dev/null; then |
| 134 | + echo "::error::Remote tag v${NEW} already exists" |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | +
|
| 138 | + - name: Write job summary |
| 139 | + env: |
| 140 | + CURRENT: ${{ steps.read-version.outputs.current }} |
| 141 | + NEW: ${{ steps.compute-version.outputs.new }} |
| 142 | + DRY_RUN: ${{ inputs.dry_run }} |
| 143 | + run: | |
| 144 | + set -euo pipefail |
| 145 | + echo "| Field | Value |" >> "$GITHUB_STEP_SUMMARY" |
| 146 | + echo "|----------|-------|" >> "$GITHUB_STEP_SUMMARY" |
| 147 | + echo "| Current | \`$CURRENT\` |" >> "$GITHUB_STEP_SUMMARY" |
| 148 | + echo "| New | \`$NEW\` |" >> "$GITHUB_STEP_SUMMARY" |
| 149 | + echo "| Tag | \`v$NEW\` |" >> "$GITHUB_STEP_SUMMARY" |
| 150 | + echo "| Dry run | \`$DRY_RUN\` |" >> "$GITHUB_STEP_SUMMARY" |
| 151 | +
|
| 152 | + push-and-dispatch: |
| 153 | + name: Push bump and tag |
| 154 | + needs: compute-and-validate |
| 155 | + if: ${{ !inputs.dry_run }} |
| 156 | + permissions: |
| 157 | + contents: read |
| 158 | + runs-on: ubuntu-24.04 |
| 159 | + |
| 160 | + steps: |
| 161 | + - name: Mint GitHub App token |
| 162 | + id: app-token |
| 163 | + uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 #v3.1.1 |
| 164 | + with: |
| 165 | + client-id: ${{ secrets.RELEASE_APP_CLIENT_ID }} |
| 166 | + private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} |
| 167 | + |
| 168 | + - name: Resolve bot identity |
| 169 | + id: bot |
| 170 | + env: |
| 171 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 172 | + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} |
| 173 | + run: | |
| 174 | + set -euo pipefail |
| 175 | + user_id=$(gh api "/users/${APP_SLUG}%5Bbot%5D" --jq .id) |
| 176 | + echo "user_name=${APP_SLUG}[bot]" >> "$GITHUB_OUTPUT" |
| 177 | + echo "user_email=${user_id}+${APP_SLUG}[bot]@users.noreply.github.com" >> "$GITHUB_OUTPUT" |
| 178 | +
|
| 179 | + - name: Checkout main with App token |
| 180 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2 |
| 181 | + with: |
| 182 | + ref: main |
| 183 | + fetch-depth: 0 |
| 184 | + persist-credentials: true |
| 185 | + token: ${{ steps.app-token.outputs.token }} |
| 186 | + |
| 187 | + - name: Re-validate current version |
| 188 | + env: |
| 189 | + EXPECTED: ${{ needs.compute-and-validate.outputs.current_name }} |
| 190 | + run: | |
| 191 | + set -euo pipefail |
| 192 | + count=$(grep -cE '^version=' gradle.properties || true) |
| 193 | + [[ "$count" == "1" ]] || { echo "::error::expected exactly one 'version=' line, found $count"; exit 1; } |
| 194 | + current=$(awk -F= '$1=="version"{print $2}' gradle.properties) |
| 195 | + [[ "$current" == "$EXPECTED" ]] || { |
| 196 | + echo "::error::current version '$current' does not match expected '$EXPECTED' (main moved during job gap?)" |
| 197 | + exit 1 |
| 198 | + } |
| 199 | +
|
| 200 | + - name: Re-check tag collision |
| 201 | + env: |
| 202 | + NEW: ${{ needs.compute-and-validate.outputs.new_name }} |
| 203 | + run: | |
| 204 | + set -euo pipefail |
| 205 | + if git rev-parse --verify "refs/tags/v${NEW}" >/dev/null 2>&1; then |
| 206 | + echo "::error::Local tag v${NEW} already exists" |
| 207 | + exit 1 |
| 208 | + fi |
| 209 | + if git ls-remote --exit-code --tags origin "refs/tags/v${NEW}" >/dev/null; then |
| 210 | + echo "::error::Remote tag v${NEW} already exists" |
| 211 | + exit 1 |
| 212 | + fi |
| 213 | +
|
| 214 | + - name: Apply version bump |
| 215 | + env: |
| 216 | + NEW: ${{ needs.compute-and-validate.outputs.new_name }} |
| 217 | + run: | |
| 218 | + set -euo pipefail |
| 219 | + sed -i "s/^version=.*/version=${NEW}/" gradle.properties |
| 220 | + count=$(grep -cE '^version=' gradle.properties || true) |
| 221 | + [[ "$count" == "1" ]] || { echo "::error::expected exactly one version= line after bump, found $count"; exit 1; } |
| 222 | + got=$(awk -F= '$1=="version"{print $2}' gradle.properties) |
| 223 | + [[ "$got" == "$NEW" ]] || { echo "::error::bump verification failed: expected '$NEW' got '$got'"; exit 1; } |
| 224 | +
|
| 225 | + - name: Configure git identity |
| 226 | + env: |
| 227 | + GIT_USER_NAME: ${{ steps.bot.outputs.user_name }} |
| 228 | + GIT_USER_EMAIL: ${{ steps.bot.outputs.user_email }} |
| 229 | + run: | |
| 230 | + set -euo pipefail |
| 231 | + git config user.name "$GIT_USER_NAME" |
| 232 | + git config user.email "$GIT_USER_EMAIL" |
| 233 | +
|
| 234 | + - name: Commit and tag |
| 235 | + env: |
| 236 | + NEW: ${{ needs.compute-and-validate.outputs.new_name }} |
| 237 | + run: | |
| 238 | + set -euo pipefail |
| 239 | + git add gradle.properties |
| 240 | + git commit -m "Release: v${NEW}" |
| 241 | + git tag -a "v${NEW}" -m "Release v${NEW}" |
| 242 | +
|
| 243 | + - name: Atomic push |
| 244 | + env: |
| 245 | + NEW: ${{ needs.compute-and-validate.outputs.new_name }} |
| 246 | + run: | |
| 247 | + set -euo pipefail |
| 248 | + git push --atomic origin HEAD:refs/heads/main "refs/tags/v${NEW}" |
| 249 | +
|
| 250 | + - name: Write final summary |
| 251 | + env: |
| 252 | + NEW: ${{ needs.compute-and-validate.outputs.new_name }} |
| 253 | + REPO: ${{ github.repository }} |
| 254 | + run: | |
| 255 | + set -euo pipefail |
| 256 | + echo "Pushed commit and tag \`v${NEW}\` to main." >> "$GITHUB_STEP_SUMMARY" |
| 257 | + echo "The \`release-tag.yml\` workflow fired automatically — track it at:" >> "$GITHUB_STEP_SUMMARY" |
| 258 | + echo "https://github.com/${REPO}/actions/workflows/release-tag.yml" >> "$GITHUB_STEP_SUMMARY" |
0 commit comments