-
Notifications
You must be signed in to change notification settings - Fork 1
260 lines (224 loc) · 9.94 KB
/
Copy pathrelease.yaml
File metadata and controls
260 lines (224 loc) · 9.94 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
name: Release
on:
pull_request_target:
types: [closed]
branches: [main]
workflow_dispatch:
# Manual trigger — use when a publish needs to be retried without re-merging
# the release PR (e.g. after a partial failure where tags were already pushed).
# cancel-in-progress: false ensures an in-flight publish is never interrupted.
concurrency:
group: release
cancel-in-progress: false
env:
NX_DAEMON: false
NX_REJECT_UNKNOWN_LOCAL_CACHE: 0
LEFTHOOK: "0"
jobs:
# ─── BUILD AND PUBLISH ────────────────────────────────────────────────────────
# First job — runs when the release/next PR is merged into main.
# Uses pull_request_target so the trigger is based on the branch name
# (head.ref == 'release/next'), not a commit message — works correctly for
# regular merge commits, squash merges, and rebase merges alike.
#
# Authenticates to npm via OIDC (Trusted Publishing — no stored token needed).
# Builds all packages, publishes to npm with provenance, and sets the `latest`
# dist-tag on each package.
#
# Tags are intentionally NOT created here — they are pushed in the `tag` job
# only after a successful publish, so a failed publish never leaves orphaned
# git tags pointing at versions that don't exist in the registry.
#
# Downstream jobs are skipped automatically when this job is skipped or fails.
build-and-publish:
name: Build and Publish
runs-on: ubuntu-latest
timeout-minutes: 25
if: |
(github.event.pull_request.merged == true && github.event.pull_request.head.ref == 'release/next') ||
github.event_name == 'workflow_dispatch'
permissions:
contents: read
id-token: write
steps:
- name: 📥 Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 1
- name: 💾 Cache git objects
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: .git/objects
key: git-${{ runner.os }}-${{ github.sha }}
restore-keys: |
git-${{ runner.os }}-
- name: 🔧 Setup Node and install dependencies
uses: ./.github/actions/node-setup-and-install
- name: 🔐 Configure npm registry for OIDC
run: |
# Write ONLY the registry URL — no _authToken line so that npm uses
# the OIDC token (from id-token: write) instead of a static token.
cat >> .npmrc << 'EOF'
registry=https://registry.npmjs.org
EOF
- name: 💾 Cache build outputs
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
packages/*/dist
configs/*/dist
key: build-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.run_id }}
restore-keys: |
build-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}-
build-${{ runner.os }}-
- name: 📤 Publish to npm
run: |
# Build packages first
pnpm nx run-many -t build
# Track failures
FAILED=""
# Publish each public package with provenance and public access.
# Already-published versions are skipped so workflow_dispatch retries
# don't fail on packages that succeeded in a previous run.
for pkg_json in packages/*/package.json configs/*/package.json; do
private=$(jq -r '.private // false' "$pkg_json")
[ "$private" = "false" ] || continue
pkg_dir=$(dirname "$pkg_json")
name=$(jq -r '.name' "$pkg_json")
version=$(jq -r '.version' "$pkg_json")
if npm view "${name}@${version}" version 2>/dev/null | grep -qx "${version}"; then
echo "⏭️ Already published ${name}@${version} — skipping"
continue
fi
echo "Publishing ${name}@${version}..."
cd "$pkg_dir"
if ! npm publish --provenance --access public; then
echo "❌ Failed to publish $pkg_dir"
FAILED="$FAILED $pkg_dir"
else
echo "✅ Successfully published $pkg_dir"
fi
cd - > /dev/null
done
# Fail if any package failed to publish
if [ -n "$FAILED" ]; then
echo "::error::The following packages failed to publish:$FAILED"
exit 1
fi
# Runs after publish so dist-tags are only updated once the new versions
# are confirmed live in the registry. Moving this before publish would
# point `latest` at a version that may not exist if publish fails.
- name: 🏷️ Ensure latest dist-tag is set for all packages
run: |
for pkg_json in packages/*/package.json configs/*/package.json; do
private=$(jq -r '.private // false' "$pkg_json")
[ "$private" = "false" ] || continue
name=$(jq -r '.name' "$pkg_json")
version=$(jq -r '.version' "$pkg_json")
npm dist-tag add "${name}@${version}" latest 2>/dev/null \
&& echo "✅ Set latest tag: ${name}@${version}" \
|| echo "⚠️ Skipped (not yet published): ${name}@${version}"
done
# ─── TAG ──────────────────────────────────────────────────────────────────────
# Runs only after build-and-publish succeeds — guarantees that every pushed
# tag has a corresponding published version in the registry. A failed publish
# therefore never leaves orphaned tags that would corrupt future release windows
# or confuse `nx release` version anchoring.
#
# Safe to re-run: the tagging step skips tags that already exist.
tag:
name: Create and Push Git Tags
needs: build-and-publish
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: write
steps:
- name: 📥 Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 0
- name: 💾 Cache git objects
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: .git/objects
key: git-${{ runner.os }}-${{ github.sha }}
restore-keys: |
git-${{ runner.os }}-
- name: 🏷️ Fetch all tags
run: git fetch --tags --force
- name: 🎯 Create and push git tags
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
for pkg_json in packages/*/package.json configs/*/package.json; do
private=$(jq -r '.private // false' "$pkg_json")
[ "$private" = "false" ] || continue
name=$(jq -r '.name' "$pkg_json")
version=$(jq -r '.version' "$pkg_json")
tag="${name}@${version}"
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "Tag already exists: $tag (skipping)"
else
git tag -a "$tag" -m "$tag"
echo "Created tag: $tag"
fi
done
git push origin --tags
# ─── GITHUB RELEASES ──────────────────────────────────────────────────────────
# Creates one GitHub Release per package using the CHANGELOG.md section for
# the current version as release notes.
#
# Runs on ubuntu-latest — only needs gh CLI and the repo checkout, no fe-runner.
# Safe to re-run after a partial failure: skips releases that already exist.
# Skipped automatically if tag was skipped or failed.
github-releases:
name: Create GitHub Releases
needs: tag
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
steps:
- name: 📥 Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
fetch-depth: 1
# gh is pre-installed on ubuntu-latest — no setup step needed.
# Creates a GitHub Release per package, using the CHANGELOG.md section for
# the current version as the release notes. Skipped for packages whose
# release already exists (safe to re-run after a partial failure).
- name: 🎉 Create GitHub Releases
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
run: |
for pkg_json in packages/*/package.json configs/*/package.json; do
private=$(jq -r '.private // false' "$pkg_json")
[ "$private" = "false" ] || continue
name=$(jq -r '.name' "$pkg_json")
version=$(jq -r '.version' "$pkg_json")
tag="${name}@${version}"
pkg_dir=$(dirname "$pkg_json")
if gh release view "$tag" --repo "$REPO" >/dev/null 2>&1; then
echo "GitHub Release already exists for $tag — skipping"
continue
fi
# Extract the changelog section for this version: everything between
# "## {version}" and the next "## " header.
changelog_file="${pkg_dir}/CHANGELOG.md"
notes=""
if [ -f "$changelog_file" ]; then
notes=$(awk -v ver="${version}" \
'substr($0, 1, length("## " ver)) == ("## " ver) { flag=1; next }
/^## / { if (flag) exit }
flag { print }' \
"$changelog_file")
fi
printf '%s\n' "${notes:-"No changelog available."}" > /tmp/release-notes.txt
gh release create "$tag" \
--repo "$REPO" \
--title "$tag" \
--notes-file /tmp/release-notes.txt
echo "Created GitHub Release: $tag"
done