|
| 1 | +name: Cut Pre-release Tag |
| 2 | + |
| 3 | +# Cut a pre-release tag (vX.Y.Z-alpha.N / -beta.N / -rc.N) as the CozyStack CI |
| 4 | +# app, so that no human ever pushes a v* tag by hand. |
| 5 | +# |
| 6 | +# This is the single entry point for creating a pre-release. Stable vX.Y.Z tags |
| 7 | +# are never cut here (or by hand) — they are created write-once by |
| 8 | +# pull-requests-release.yaml finalize at a promote PR's merge commit. See |
| 9 | +# docs/release.md and #2677. |
| 10 | +# |
| 11 | +# Dispatch from the branch the tag belongs to: `main` for a new minor's |
| 12 | +# pre-release (vX.Y.0-rc.N), or `release-X.Y` for a patch line (vX.Y.Z-rc.N). |
| 13 | +# The workflow tags that branch's HEAD. |
| 14 | +# |
| 15 | +# Why a workflow instead of a manual `git push`: |
| 16 | +# 1. The tag is pushed with the CI app token. A tag pushed with the default |
| 17 | +# GITHUB_TOKEN would NOT trigger tags.yaml (GitHub suppresses workflow |
| 18 | +# recursion from GITHUB_TOKEN); the app token is a distinct identity, so |
| 19 | +# tags.yaml fires exactly as it does for a maintainer's manual push. |
| 20 | +# 2. It pushes `HEAD:refs/tags/<tag>` from the checked-out branch tip, so the |
| 21 | +# push event's base_ref is populated — tags.yaml's "Get base branch" step |
| 22 | +# needs it to detect main vs release-X.Y. |
| 23 | +# 3. It makes CI the sole creator of pre-release tags. Repo rulesets can |
| 24 | +# already restrict *stable* tag creation to CI (a "Restrict creations" |
| 25 | +# ruleset targeting v* while excluding v*-*), but pre-releases were |
| 26 | +# hand-pushed, so they could not be CI-locked. With this workflow as their |
| 27 | +# only entry point, admins can extend "Restrict creations" (CI app the sole |
| 28 | +# bypass) to *all* of v* — pre-release and stable alike. |
| 29 | + |
| 30 | +on: |
| 31 | + workflow_dispatch: |
| 32 | + inputs: |
| 33 | + tag: |
| 34 | + description: "Pre-release tag to cut, e.g. v1.6.0-rc.1 (must be -alpha.N / -beta.N / -rc.N)" |
| 35 | + required: true |
| 36 | + type: string |
| 37 | + |
| 38 | +concurrency: |
| 39 | + # One global lane: two dispatches racing to push overlapping tags would |
| 40 | + # confuse the write-once guard. Pre-release cuts are rare, so serialize. |
| 41 | + group: cut-prerelease |
| 42 | + cancel-in-progress: false |
| 43 | + |
| 44 | +# The tag write happens via the app token, not GITHUB_TOKEN, so the job needs |
| 45 | +# only read scope on the default token. |
| 46 | +permissions: |
| 47 | + contents: read |
| 48 | + |
| 49 | +jobs: |
| 50 | + cut: |
| 51 | + name: Cut ${{ inputs.tag }} |
| 52 | + runs-on: ubuntu-latest |
| 53 | + timeout-minutes: 10 |
| 54 | + |
| 55 | + steps: |
| 56 | + # Pure-input validation first — no token minted, no checkout, so a bad tag |
| 57 | + # or wrong dispatch branch fails in seconds. |
| 58 | + |
| 59 | + # Validate the dispatch input against a strict pre-release pattern. A |
| 60 | + # stable vX.Y.Z is rejected on purpose: stable tags are cut only by the |
| 61 | + # promote flow, at a PR merge commit. |
| 62 | + - name: Parse and validate tag |
| 63 | + id: parse |
| 64 | + env: |
| 65 | + TAG: ${{ inputs.tag }} |
| 66 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 67 | + with: |
| 68 | + script: | |
| 69 | + const tag = process.env.TAG.trim(); |
| 70 | + const m = tag.match(/^v(\d+)\.(\d+)\.(\d+)-(alpha|beta|rc)\.(\d+)$/); |
| 71 | + if (!m) { |
| 72 | + core.setFailed( |
| 73 | + `tag '${tag}' must match 'vX.Y.Z-(alpha|beta|rc).N'. ` + |
| 74 | + `Stable vX.Y.Z tags are cut by the promote flow, not here.` |
| 75 | + ); |
| 76 | + return; |
| 77 | + } |
| 78 | + core.setOutput('tag', tag); |
| 79 | + core.setOutput('line', `${m[1]}.${m[2]}`); // 1.6 |
| 80 | + core.setOutput('patch', m[3]); // 0 |
| 81 | +
|
| 82 | + # Refuse to cut from anything but main or the *matching* release-X.Y line. |
| 83 | + # The pushed tag's base_ref (= the dispatch branch) must be one tags.yaml |
| 84 | + # accepts, and the branch must actually contain the version being cut. |
| 85 | + - name: Validate dispatch branch |
| 86 | + env: |
| 87 | + REF_NAME: ${{ github.ref_name }} |
| 88 | + REF_TYPE: ${{ github.ref_type }} |
| 89 | + LINE: ${{ steps.parse.outputs.line }} |
| 90 | + PATCH: ${{ steps.parse.outputs.patch }} |
| 91 | + run: | |
| 92 | + if [ "$REF_TYPE" != "branch" ]; then |
| 93 | + echo "::error::Dispatch this workflow from a branch (main or release-X.Y), not a tag or SHA." |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + case "$REF_NAME" in |
| 97 | + main) |
| 98 | + # From main only a *new minor's* pre-release (vX.Y.0-*) is valid. |
| 99 | + # A patch-line pre-release (Z>0) built from main content would land |
| 100 | + # on a wrong-tree release-X.Y.Z-rc.N staging branch — it must be cut |
| 101 | + # from its own release-X.Y line instead. |
| 102 | + if [ "$PATCH" != "0" ]; then |
| 103 | + echo "::error::From 'main' only vX.Y.0-* pre-releases may be cut (got patch .$PATCH). Cut v$LINE.$PATCH-* from 'release-$LINE'." |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + ;; |
| 107 | + release-*) |
| 108 | + if [ "release-$LINE" != "$REF_NAME" ]; then |
| 109 | + echo "::error::Tag line $LINE does not match dispatch branch '$REF_NAME'. Cut v$LINE.* from 'main' or 'release-$LINE'." |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + ;; |
| 113 | + *) |
| 114 | + echo "::error::Dispatch from 'main' or 'release-X.Y' only. Got '$REF_NAME'." |
| 115 | + exit 1 |
| 116 | + ;; |
| 117 | + esac |
| 118 | +
|
| 119 | + - name: Generate GitHub App token |
| 120 | + id: app-token |
| 121 | + uses: actions/create-github-app-token@d72941d797fd3113feb6b93fd0dec494b13a2547 # v1 |
| 122 | + with: |
| 123 | + app-id: ${{ secrets.COZYSTACK_CI_APP_ID }} |
| 124 | + private-key: ${{ secrets.COZYSTACK_CI_PRIVATE_KEY }} |
| 125 | + owner: cozystack |
| 126 | + # Scope the token to this repo only — it never touches other repos. |
| 127 | + repositories: ${{ github.event.repository.name }} |
| 128 | + |
| 129 | + # Cutting a release is privileged. workflow_dispatch already requires write |
| 130 | + # access to trigger, so this is defense-in-depth: it makes the requirement |
| 131 | + # explicit and auditable, and fails closed if that platform default ever |
| 132 | + # changes or the job is reached by another path. Reads permission via the |
| 133 | + # app token (fails the step if the API errors — fail closed). |
| 134 | + - name: Require write access |
| 135 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 136 | + with: |
| 137 | + github-token: ${{ steps.app-token.outputs.token }} |
| 138 | + script: | |
| 139 | + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + username: context.actor, |
| 143 | + }); |
| 144 | + // getCollaboratorPermissionLevel's `permission` field returns only |
| 145 | + // the legacy roles admin/write/read/none (maintain collapses to |
| 146 | + // write, triage to read), so admin+write is the whole "can push" set. |
| 147 | + const allowed = ['admin', 'write']; |
| 148 | + if (!allowed.includes(data.permission)) { |
| 149 | + core.setFailed( |
| 150 | + `@${context.actor} has '${data.permission}' access — cutting a pre-release tag ` + |
| 151 | + `requires write access to ${context.repo.owner}/${context.repo.repo}.` |
| 152 | + ); |
| 153 | + return; |
| 154 | + } |
| 155 | + core.info(`@${context.actor}: '${data.permission}' access — authorized.`); |
| 156 | +
|
| 157 | + # Minimal checkout — just enough to configure origin with the app-token |
| 158 | + # credential (persist-credentials, default on) so the bare push below is |
| 159 | + # app-authenticated, and to resolve HEAD. One ref, shallow, no tags. |
| 160 | + - name: Check out the dispatched branch |
| 161 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 |
| 162 | + with: |
| 163 | + fetch-depth: 1 |
| 164 | + token: ${{ steps.app-token.outputs.token }} |
| 165 | + |
| 166 | + - name: Cut and push the pre-release tag |
| 167 | + env: |
| 168 | + TAG: ${{ steps.parse.outputs.tag }} |
| 169 | + REF_NAME: ${{ github.ref_name }} |
| 170 | + run: | |
| 171 | + # Write-once: the tag must not already exist on origin. Distinguish |
| 172 | + # "absent" (git ls-remote --exit-code exits 2) from a transport/auth |
| 173 | + # failure (any other non-zero) so an infra blip cannot masquerade as |
| 174 | + # "free to create". |
| 175 | + set +e |
| 176 | + git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1 |
| 177 | + rc=$? |
| 178 | + set -e |
| 179 | + if [ "$rc" -eq 0 ]; then |
| 180 | + echo "::error::Tag $TAG already exists on origin — pre-release tags are write-once." |
| 181 | + exit 1 |
| 182 | + elif [ "$rc" -ne 2 ]; then |
| 183 | + echo "::error::Could not query origin for $TAG (git ls-remote exit $rc) — refusing to proceed." |
| 184 | + exit 1 |
| 185 | + fi |
| 186 | +
|
| 187 | + # Stale-tip guard: workflow_dispatch checks out the dispatch-time commit, |
| 188 | + # not the branch tip at push time (the gap includes any wait in the |
| 189 | + # cut-prerelease concurrency queue). If the branch advanced meanwhile, |
| 190 | + # pushing HEAD would tag a stale commit and yield an empty base_ref — |
| 191 | + # tags.yaml would then fail, having already burned this write-once name. |
| 192 | + # Refuse and ask for a re-run instead. |
| 193 | + local_head="$(git rev-parse HEAD)" |
| 194 | + # Resolve the remote tip without a pipe masking ls-remote's exit code |
| 195 | + # (mirrors the write-once query above): a transport/auth failure must |
| 196 | + # not read as "branch not found". |
| 197 | + set +e |
| 198 | + remote_ls="$(git ls-remote origin "refs/heads/$REF_NAME")" |
| 199 | + rc=$? |
| 200 | + set -e |
| 201 | + if [ "$rc" -ne 0 ]; then |
| 202 | + echo "::error::Could not query origin for the tip of '$REF_NAME' (git ls-remote exit $rc) — refusing to proceed." |
| 203 | + exit 1 |
| 204 | + fi |
| 205 | + remote_head="$(printf '%s' "$remote_ls" | cut -f1)" |
| 206 | + if [ -z "$remote_head" ]; then |
| 207 | + echo "::error::Could not resolve the tip of '$REF_NAME' on origin (no such branch?)." |
| 208 | + exit 1 |
| 209 | + fi |
| 210 | + if [ "$local_head" != "$remote_head" ]; then |
| 211 | + echo "::error::'$REF_NAME' advanced since dispatch ($local_head -> $remote_head). Re-run the workflow to tag the current tip." |
| 212 | + exit 1 |
| 213 | + fi |
| 214 | +
|
| 215 | + # Push HEAD straight to a tag ref: no local tag object, non-forced. The |
| 216 | + # branch-tip anchoring makes GitHub populate the push event's base_ref |
| 217 | + # (tags.yaml's Get-base-branch step requires it); non-forced makes the |
| 218 | + # tag write-once at the git layer. tags.yaml takes it from here (build + |
| 219 | + # e2e + draft pre-release + staging branch). |
| 220 | + git push origin "HEAD:refs/tags/$TAG" |
| 221 | + echo "Pushed $TAG at $local_head from '$REF_NAME'." |
| 222 | + echo "tags.yaml will now build, e2e, and draft the pre-release." |
0 commit comments