Repository navigation
338 lines (301 loc) · 12.3 KB
/
Copy pathsecure_nx_release.yml
File metadata and controls
338 lines (301 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
name: Release Workflow
on:
workflow_dispatch:
inputs:
version:
description: "Exact version to release (e.g. 0.3.0-rc.0). Overrides release-type/preid; npm dist-tag is derived from it (0.3.0-rc.0 -> rc, 0.3.0 -> latest)"
required: false
type: string
default: ""
release-type:
description: "Version bump when 'version' is empty (patch/minor/major publish to npm 'latest'; prerelease uses 'preid')"
required: false
type: choice
options:
- patch
- minor
- major
- prerelease
default: patch
preid:
description: "Prerelease identifier (used only when release-type=prerelease and 'version' is empty; also becomes the npm dist-tag, e.g. next | canary)"
required: false
type: string
default: next
publish-only:
description: "Publish the version already committed in packages/tanstack-router/package.json (with its CHANGELOG entry). Skips version bump and changelog; tags, publishes and creates the GitHub release. Ignores version/release-type/preid."
required: false
type: boolean
default: false
dry-run:
description: "Run release steps without making changes (no git push, no publish)"
required: false
type: boolean
default: false
concurrency:
# Avoid overlapping publishes on the same ref/branch
group: nx-release-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: write # needed to push version commits and tags
id-token: write # required for npm provenance / trusted publishing (OIDC)
env:
RELEASE_PROJECT: tanstack-router
PACKAGE_DIR: packages/tanstack-router
jobs:
release:
name: Version and Publish (gated by environment)
runs-on: ubuntu-latest
environment:
name: ${{ inputs.dry-run && 'npm-publish-dry-run' || 'npm-publish' }}
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- name: Checkout repository (full history for tagging)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '24'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
- name: Update npm (required for OIDC trusted publishing)
run: |
npm install -g npm@^11.5.1
npm --version
- name: Install dependencies
run: npm ci
- name: Configure git user for automated commits
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# The checkout above is pinned to the SHA resolved at dispatch time; re-sync to
# the branch tip so the release commit/tag push is a fast-forward.
- name: Sync to latest branch tip
if: ${{ !inputs.dry-run }}
run: |
git fetch origin "$GITHUB_REF_NAME"
git reset --hard "origin/$GITHUB_REF_NAME"
- name: Resolve release context
id: ctx
shell: bash
env:
INPUT_VERSION: ${{ inputs.version }}
INPUT_RELEASE_TYPE: ${{ inputs['release-type'] }}
INPUT_PREID: ${{ inputs.preid }}
INPUT_PUBLISH_ONLY: ${{ inputs['publish-only'] }}
INPUT_DRY_RUN: ${{ inputs['dry-run'] }}
run: |
set -euo pipefail
dist_tag_for() {
# npm dist-tag is the alphabetic prerelease label (0.3.0-rc.0 -> rc); purely numeric labels fall back to next.
local v="$1"
if [[ "$v" == *-* ]]; then
local prerelease="${v#*-}"
local tag="${prerelease%%.*}"
if [[ "$tag" =~ ^[0-9]+$ ]]; then tag="next"; fi
echo "$tag"
else
echo "latest"
fi
}
specifier=""
preid=""
version=""
if [[ "$INPUT_PUBLISH_ONLY" == "true" ]]; then
version=$(node -p "require('./${PACKAGE_DIR}/package.json').version")
dist_tag=$(dist_tag_for "$version")
elif [[ -n "$INPUT_VERSION" ]]; then
specifier="$INPUT_VERSION"
dist_tag=$(dist_tag_for "$INPUT_VERSION")
else
specifier="$INPUT_RELEASE_TYPE"
# npm dist-tag follows release type: prerelease -> preid, stable -> latest
if [[ "$INPUT_RELEASE_TYPE" == "prerelease" ]]; then
preid="$INPUT_PREID"
dist_tag="$INPUT_PREID"
else
dist_tag="latest"
fi
fi
{
echo "publish_only=${INPUT_PUBLISH_ONLY}"
echo "specifier=${specifier}"
echo "preid=${preid}"
echo "version=${version}"
echo "dist_tag=${dist_tag}"
echo "dry_run=${INPUT_DRY_RUN}"
} >> "$GITHUB_OUTPUT"
# PUBLISH-ONLY: the version and CHANGELOG entry are already committed. Refuse a
# version npm already has, then tag HEAD locally (nx.json releaseTagPattern) so the
# GitHub release and future changelogs have their anchor.
- name: Check committed version (publish-only)
if: ${{ inputs['publish-only'] }}
shell: bash
env:
VERSION: ${{ steps.ctx.outputs.version }}
run: |
set -euo pipefail
pkg=$(node -p "require('./${PACKAGE_DIR}/package.json').name")
if npm view "${pkg}@${VERSION}" version >/dev/null 2>&1; then
echo "::error::${pkg}@${VERSION} is already on npm. Bump the version (or dispatch without publish-only)."
exit 1
fi
if ! grep -q "^## ${VERSION} (" "${PACKAGE_DIR}/CHANGELOG.md"; then
echo "::warning::No '## ${VERSION} (' entry in ${PACKAGE_DIR}/CHANGELOG.md; the GitHub release will use generated notes."
fi
tag="${VERSION}-${RELEASE_PROJECT}"
if git rev-parse -q --verify "refs/tags/${tag}" >/dev/null; then
if [[ "$(git rev-list -n1 "$tag")" != "$(git rev-parse HEAD)" ]]; then
echo "::error::Tag ${tag} already exists on another commit."
exit 1
fi
fi
# VERSION: bumps the version, generates the changelog, commits + tags locally in one shot.
# --skip-publish keeps publishing as a separate step below so the OIDC token-clearing logic still runs.
- name: nx release version + changelog
if: ${{ !inputs['publish-only'] }}
shell: bash
env:
SPECIFIER: ${{ steps.ctx.outputs.specifier }}
PREID: ${{ steps.ctx.outputs.preid }}
DRY_RUN: ${{ inputs['dry-run'] }}
run: |
set -euo pipefail
preid_arg=()
if [[ -n "$PREID" ]]; then
preid_arg=(--preid "$PREID")
fi
dry_run_arg=()
if [[ "$DRY_RUN" == "true" ]]; then
dry_run_arg=(--dry-run)
fi
npx nx release "$SPECIFIER" \
"${preid_arg[@]}" \
--projects "$RELEASE_PROJECT" \
--skip-publish \
--verbose \
"${dry_run_arg[@]}"
- name: Tag committed version (publish-only)
if: ${{ inputs['publish-only'] }}
shell: bash
env:
VERSION: ${{ steps.ctx.outputs.version }}
run: |
set -euo pipefail
tag="${VERSION}-${RELEASE_PROJECT}"
if ! git rev-parse -q --verify "refs/tags/${tag}" >/dev/null; then
git tag -a "$tag" -m "$tag"
fi
# BUILD + TEST: the build copies the (bumped) package.json into dist/, which is what gets published.
- name: Build
run: npx nx build "$RELEASE_PROJECT" --verbose
- name: Test
run: npx nx test "$RELEASE_PROJECT"
- name: Resolve released version
id: released
shell: bash
run: |
set -euo pipefail
echo "version=$(node -p "require('./dist/${PACKAGE_DIR}/package.json').version")" >> "$GITHUB_OUTPUT"
# Nothing leaves the runner until build and tests pass. --atomic: if the branch push
# is rejected, the release tag is rejected with it, so a failed run leaves no
# orphaned tag behind and a plain re-dispatch is enough to retry.
- name: Push release commit and tag
if: ${{ !inputs['publish-only'] && !inputs.dry-run }}
run: git push --atomic --follow-tags origin HEAD
- name: Push release tag (publish-only)
if: ${{ inputs['publish-only'] && !inputs.dry-run }}
shell: bash
env:
VERSION: ${{ steps.ctx.outputs.version }}
run: git push origin "refs/tags/${VERSION}-${RELEASE_PROJECT}"
# PUBLISH: OIDC trusted publishing (default). Avoid any lingering token auth.
- name: nx release publish (OIDC)
if: ${{ vars.USE_NPM_TOKEN != 'true' }}
shell: bash
env:
NPM_CONFIG_PROVENANCE: true
NODE_AUTH_TOKEN: ""
DIST_TAG: ${{ steps.ctx.outputs.dist_tag }}
DRY_RUN: ${{ inputs['dry-run'] }}
run: |
set -euo pipefail
unset NODE_AUTH_TOKEN
rm -f ~/.npmrc || true
if [[ -n "${NPM_CONFIG_USERCONFIG:-}" ]]; then
rm -f "$NPM_CONFIG_USERCONFIG" || true
fi
dry_run_arg=()
if [[ "$DRY_RUN" == "true" ]]; then
dry_run_arg=(--dry-run)
fi
npx nx release publish \
--projects "$RELEASE_PROJECT" \
--tag "$DIST_TAG" \
--access public \
--verbose \
"${dry_run_arg[@]}"
# PUBLISH: token fallback (only when explicitly enabled via repo/environment variable USE_NPM_TOKEN=true).
- name: nx release publish (token)
if: ${{ vars.USE_NPM_TOKEN == 'true' }}
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
NPM_CONFIG_PROVENANCE: true
DIST_TAG: ${{ steps.ctx.outputs.dist_tag }}
DRY_RUN: ${{ inputs['dry-run'] }}
run: |
set -euo pipefail
dry_run_arg=()
if [[ "$DRY_RUN" == "true" ]]; then
dry_run_arg=(--dry-run)
fi
npx nx release publish --projects "$RELEASE_PROJECT" --tag "$DIST_TAG" --access public --verbose "${dry_run_arg[@]}"
# Nx only writes CHANGELOG.md, so the GitHub release is posted here, after the tag
# push (so it is not auto-created from the wrong commit) and after npm publish (so
# a GitHub API failure cannot block publishing).
- name: Create GitHub release
if: ${{ !inputs.dry-run }}
shell: bash
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ steps.released.outputs.version }}
run: |
set -euo pipefail
tag="${VERSION}-${RELEASE_PROJECT}"
pkg=$(node -p "require('./${PACKAGE_DIR}/package.json').name")
if gh release view "$tag" --json id --jq .id >/dev/null 2>&1; then
echo "Release for '$tag' already exists; skipping."
exit 0
fi
notes_file="$(mktemp)"
awk -v h="## ${VERSION} (" 'index($0, h) == 1 {found=1; next} /^## / {if (found) exit} found' "${PACKAGE_DIR}/CHANGELOG.md" > "$notes_file"
flags=(--verify-tag --title "${pkg}@${VERSION}")
if [[ "$VERSION" == *-* ]]; then
flags+=(--prerelease)
else
flags+=(--latest)
fi
if [[ -s "$notes_file" ]]; then
flags+=(--notes-file "$notes_file")
else
flags+=(--generate-notes)
fi
gh release create "$tag" "${flags[@]}"
- name: Summary
if: always()
run: |
echo "Nx Release completed."
echo "- project: ${RELEASE_PROJECT}"
echo "- publish-only: ${{ steps.ctx.outputs.publish_only }}"
echo "- specifier: '${{ steps.ctx.outputs.specifier }}'"
echo "- preid: '${{ steps.ctx.outputs.preid }}'"
echo "- version: ${{ steps.released.outputs.version }}"
echo "- dist-tag: ${{ steps.ctx.outputs.dist_tag }}"
echo "- dry-run: ${{ steps.ctx.outputs.dry_run }}"
echo "- use-token: ${{ vars.USE_NPM_TOKEN == 'true' }}"