返回 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 steps:
30 - uses: actions/checkout@v7
31 with:
32 fetch-depth: 0
33 ref: ${{ github.sha }}
34 # Prefer PAT so the resulting tag push triggers release.yml.
35 # Falls back to GITHUB_TOKEN, which will tag but NOT trigger.
36 token: ${{ secrets.RELEASE_TAG_PAT || github.token }}
37
38 - name: Require main dispatch
39 id: source
40 run: |
41 set -euo pipefail
42 if [ "${GITHUB_REF_NAME}" != "main" ]; then
43 echo "::error::Create release tags from main only; rerun this workflow with ref 'main'." >&2
44 exit 1
45 fi
46 intended_sha="$(git rev-parse 'HEAD^{commit}')"
47 event_sha="$(git rev-parse "${GITHUB_SHA}^{commit}")"
48 if [ "${intended_sha}" != "${event_sha}" ]; then
49 echo "::error::Checkout ${intended_sha} does not match dispatch SHA ${event_sha}." >&2
50 exit 1
51 fi
52 git fetch --no-tags origin "+refs/heads/main:refs/remotes/origin/main"
53 live_main="$(git rev-parse 'refs/remotes/origin/main^{commit}')"
54 if [ "${intended_sha}" != "${live_main}" ]; then
55 echo "::error::Dispatch SHA ${intended_sha} is stale; live origin/main is ${live_main}. Rerun from the current main ref." >&2
56 exit 1
57 fi
58 echo "sha=${intended_sha}" >> "${GITHUB_OUTPUT}"
59
60 - name: Read workspace version
61 id: ver
62 run: |
63 v="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
64 if [ -z "$v" ]; then
65 echo "::error::Could not parse workspace version from Cargo.toml" >&2
66 exit 1
67 fi
68 if ! echo "$v" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
69 echo "::error::Workspace version '$v' is not valid semver (expected X.Y.Z)" >&2
70 exit 1
71 fi
72 echo "version=$v" >> "$GITHUB_OUTPUT"
73 echo "tag=v$v" >> "$GITHUB_OUTPUT"
74 echo "Workspace version: $v"
75
76 - name: Check whether tag already exists
77 id: check
78 env:
79 EXPECTED_SHA: ${{ steps.source.outputs.sha }}
80 TAG: ${{ steps.ver.outputs.tag }}
81 run: |
82 set -euo pipefail
83 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
84 if [ -z "${remote_tag_sha}" ]; then
85 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
86 fi
87 if [ -n "${remote_tag_sha}" ]; then
88 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
89 echo "::error::Tag ${TAG} already exists at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
90 exit 1
91 fi
92 echo "exists=true" >> "$GITHUB_OUTPUT"
93 echo "Tag ${TAG} already exists at the intended SHA; nothing to do."
94 else
95 echo "exists=false" >> "$GITHUB_OUTPUT"
96 echo "Tag ${TAG} does not exist; will create."
97 fi
98
99 - name: Verify version consistency
100 if: steps.check.outputs.exists == 'false'
101 run: |
102 ./scripts/release/check-versions.sh --require-dated-release || {
103 echo "::error::Version consistency check failed. Aborting tag creation." >&2
104 exit 1
105 }
106
107 - name: Create and push tag
108 id: create
109 if: steps.check.outputs.exists == 'false'
110 env:
111 EXPECTED_SHA: ${{ steps.source.outputs.sha }}
112 TAG: ${{ steps.ver.outputs.tag }}
113 run: |
114 set -euo pipefail
115 git config user.name "github-actions[bot]"
116 git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
117 git fetch --no-tags origin "+refs/heads/main:refs/remotes/origin/main"
118 live_main="$(git rev-parse 'refs/remotes/origin/main^{commit}')"
119 if [ "${live_main}" != "${EXPECTED_SHA}" ]; then
120 echo "::error::origin/main advanced to ${live_main}; refusing to tag stale ${EXPECTED_SHA}." >&2
121 exit 1
122 fi
123 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
124 if [ -z "${remote_tag_sha}" ]; then
125 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
126 fi
127 if [ -n "${remote_tag_sha}" ]; then
128 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
129 echo "::error::Tag ${TAG} appeared at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
130 exit 1
131 fi
132 echo "pushed=false" >> "$GITHUB_OUTPUT"
133 echo "Tag ${TAG} already exists at the intended SHA after refresh; nothing to do."
134 exit 0
135 fi
136 git tag -f "${TAG}" "${EXPECTED_SHA}"
137 max_retries=3
138 retry_count=0
139 while [ "${retry_count}" -lt "${max_retries}" ]; do
140 if git push origin "${TAG}"; then
141 echo "pushed=true" >> "$GITHUB_OUTPUT"
142 echo "Pushed ${TAG}. release.yml should now run (requires RELEASE_TAG_PAT for trigger)."
143 exit 0
144 fi
145 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}^{}" | awk 'NR == 1 {print $1}')"
146 if [ -z "${remote_tag_sha}" ]; then
147 remote_tag_sha="$(git ls-remote --tags origin "refs/tags/${TAG}" | awk 'NR == 1 {print $1}')"
148 fi
149 if [ -n "${remote_tag_sha}" ]; then
150 if [ "${remote_tag_sha}" != "${EXPECTED_SHA}" ]; then
151 echo "::error::Tag ${TAG} appeared during push at ${remote_tag_sha}, expected ${EXPECTED_SHA}." >&2
152 exit 1
153 fi
154 echo "pushed=false" >> "$GITHUB_OUTPUT"
155 echo "Tag ${TAG} appeared at the intended SHA during push; treating as already handled."
156 exit 0
157 fi
158 retry_count=$((retry_count + 1))
159 if [ "${retry_count}" -lt "${max_retries}" ]; then
160 echo "Push attempt ${retry_count} failed; retrying in 10s..."
161 sleep 10
162 fi
163 done
164
165 echo "::error::Failed to push tag ${TAG} after ${max_retries} attempts." >&2
166 exit 1
167
168 - name: Warn if PAT missing
169 if: steps.create.outputs.pushed == 'true'
170 env:
171 HAS_PAT: ${{ secrets.RELEASE_TAG_PAT != '' }}
172 run: |
173 if [ "${HAS_PAT}" != "true" ]; then
174 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 }}'."
175 fi
176
176 lines YAML