返回 CodeWhale
auto-tag.yml
根目录 / .github / workflows / auto-tag.yml
1 name: Create release tag
2
3 # Manual helper that creates `vX.Y.Z` from the current `main` workspace
4 # version after the release source is frozen. This is intentionally not
5 # triggered by every version bump on `main`: release prep can land before the
6 # same-version issue/PR queue is truly empty, and an eager tag leaves later
7 # same-version work outside the public release anchor.
8 #
9 # IMPORTANT: tag pushes signed by the default `GITHUB_TOKEN` do NOT trigger
10 # downstream `on: push: tags` workflows (GitHub Actions safety rule). For
11 # this auto-tag flow to actually fire `release.yml`, store a PAT (or
12 # fine-grained token) with `contents: write` on this repo as the
13 # `RELEASE_TAG_PAT` secret. Without it, the tag is created but `release.yml`
14 # does NOT run automatically; run `release.yml` manually for the version.
15
16 on:
17 workflow_dispatch:
18
19 permissions:
20 contents: write
21
22 concurrency:
23 group: auto-tag-${{ github.ref_name }}
24 cancel-in-progress: false
25
26 jobs:
27 tag:
28 runs-on: ubuntu-latest
29 timeout-minutes: 15
30 steps:
31 - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
32 with:
33 fetch-depth: 0
34 ref: ${{ github.sha }}
35 # Prefer PAT so the resulting tag push triggers release.yml.
36 # Falls back to GITHUB_TOKEN, which will tag but NOT trigger.
37 token: ${{ secrets.RELEASE_TAG_PAT || github.token }}
38
39 - name: Require main dispatch
40 id: source
41 run: |
42 set -euo pipefail
43 if [ "${GITHUB_REF_NAME}" != "main" ]; then
44 echo "::error::Create release tags from main only; rerun this workflow with ref 'main'." >&2
45 exit 1
46 fi
47 intended_sha="$(git rev-parse 'HEAD^{commit}')"
48 event_sha="$(git rev-parse "${GITHUB_SHA}^{commit}")"
49 if [ "${intended_sha}" != "${event_sha}" ]; then
50 echo "::error::Checkout ${intended_sha} does not match dispatch SHA ${event_sha}." >&2
51 exit 1
52 fi
53 git fetch --no-tags origin "+refs/heads/main:refs/remotes/origin/main"
54 live_main="$(git rev-parse 'refs/remotes/origin/main^{commit}')"
55 if [ "${intended_sha}" != "${live_main}" ]; then
56 echo "::error::Dispatch SHA ${intended_sha} is stale; live origin/main is ${live_main}. Rerun from the current main ref." >&2
57 exit 1
58 fi
59 echo "sha=${intended_sha}" >> "${GITHUB_OUTPUT}"
60
61 - name: Read workspace version
62 id: ver
63 run: |
64 v="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
65 if [ -z "$v" ]; then
66 echo "::error::Could not parse workspace version from Cargo.toml" >&2
67 exit 1
68 fi
69 if ! echo "$v" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
70 echo "::error::Workspace version '$v' is not valid semver (expected X.Y.Z)" >&2
71 exit 1
72 fi
73 echo "version=$v" >> "$GITHUB_OUTPUT"
74 echo "tag=v$v" >> "$GITHUB_OUTPUT"
75 echo "Workspace version: $v"
76
77 - name: Check whether tag already exists
78 id: check
79 env:
80 EXPECTED_SHA: ${{ steps.source.outputs.sha }}
81 TAG: ${{ steps.ver.outputs.tag }}
82 run: |
83 set -euo pipefail
84 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
85 if [ -z "${remote_tag_sha}" ]; then
86 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
87 fi
88 if [ -n "${remote_tag_sha}" ]; then
89 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
90 echo "::error::Tag ${TAG} already exists at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
91 exit 1
92 fi
93 echo "exists=true" >> "$GITHUB_OUTPUT"
94 echo "Tag ${TAG} already exists at the intended SHA; nothing to do."
95 else
96 echo "exists=false" >> "$GITHUB_OUTPUT"
97 echo "Tag ${TAG} does not exist; will create."
98 fi
99
100 - name: Verify version consistency
101 if: steps.check.outputs.exists == 'false'
102 run: |
103 ./scripts/release/check-versions.sh --require-dated-release || {
104 echo "::error::Version consistency check failed. Aborting tag creation." >&2
105 exit 1
106 }
107
108 - name: Create and push tag
109 id: create
110 if: steps.check.outputs.exists == 'false'
111 env:
112 EXPECTED_SHA: ${{ steps.source.outputs.sha }}
113 TAG: ${{ steps.ver.outputs.tag }}
114 run: |
115 set -euo pipefail
116 git config user.name "github-actions[bot]"
117 git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
118 git fetch --no-tags origin "+refs/heads/main:refs/remotes/origin/main"
119 live_main="$(git rev-parse 'refs/remotes/origin/main^{commit}')"
120 if [ "${live_main}" != "${EXPECTED_SHA}" ]; then
121 echo "::error::origin/main advanced to ${live_main}; refusing to tag stale ${EXPECTED_SHA}." >&2
122 exit 1
123 fi
124 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
125 if [ -z "${remote_tag_sha}" ]; then
126 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
127 fi
128 if [ -n "${remote_tag_sha}" ]; then
129 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
130 echo "::error::Tag ${TAG} appeared at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
131 exit 1
132 fi
133 echo "pushed=false" >> "$GITHUB_OUTPUT"
134 echo "Tag ${TAG} already exists at the intended SHA after refresh; nothing to do."
135 exit 0
136 fi
137 git tag -f "${TAG}" "${EXPECTED_SHA}"
138 max_retries=3
139 retry_count=0
140 while [ "${retry_count}" -lt "${max_retries}" ]; do
141 if git push origin "${TAG}"; then
142 echo "pushed=true" >> "$GITHUB_OUTPUT"
143 echo "Pushed ${TAG}. release.yml should now run (requires RELEASE_TAG_PAT for trigger)."
144 exit 0
145 fi
146 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
147 if [ -z "${remote_tag_sha}" ]; then
148 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
149 fi
150 if [ -n "${remote_tag_sha}" ]; then
151 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
152 echo "::error::Tag ${TAG} appeared during push at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
153 exit 1
154 fi
155 echo "pushed=false" >> "$GITHUB_OUTPUT"
156 echo "Tag ${TAG} appeared at the intended SHA during push; treating as already handled."
157 exit 0
158 fi
159 retry_count=$((retry_count + 1))
160 if [ "${retry_count}" -lt "${max_retries}" ]; then
161 echo "Push attempt ${retry_count} failed; retrying in 10s..."
162 sleep 10
163 fi
164 done
165
166 echo "::error::Failed to push tag ${TAG} after ${max_retries} attempts." >&2
167 exit 1
168
169 - name: Warn if PAT missing
170 if: steps.create.outputs.pushed == 'true'
171 env:
172 HAS_PAT: ${{ secrets.RELEASE_TAG_PAT != '' }}
173 run: |
174 if [ "${HAS_PAT}" != "true" ]; then
175 echo "::warning::RELEASE_TAG_PAT secret is not set. The tag was pushed using GITHUB_TOKEN, which does NOT trigger release.yml. First confirm no tag-triggered Release run is queued or active, then run 'gh workflow run release.yml --ref ${{ steps.ver.outputs.tag }} -f version=${{ steps.ver.outputs.version }}'."
176 fi
177
177 lines YAML