-
Notifications
You must be signed in to change notification settings - Fork 0
310 lines (264 loc) · 11 KB
/
publish.yml
File metadata and controls
310 lines (264 loc) · 11 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
name: Publish Package
on:
push:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
permissions:
contents: write
pull-requests: write
id-token: write
jobs:
resolve-merge-context:
name: Resolve Merge Context
runs-on: blacksmith-2vcpu-ubuntu-2404
permissions:
contents: read
pull-requests: read
outputs:
pr_number: ${{ steps.context.outputs.pr_number }}
pr_url: ${{ steps.context.outputs.pr_url }}
pr_title: ${{ steps.context.outputs.pr_title }}
pr_head_ref: ${{ steps.context.outputs.pr_head_ref }}
is_bump_pr: ${{ steps.context.outputs.is_bump_pr }}
steps:
- name: Ensure push commit came from merged PR
id: context
uses: actions/github-script@v7
with:
script: |
const query = `
query($owner: String!, $repo: String!, $oid: GitObjectID!) {
repository(owner: $owner, name: $repo) {
object(oid: $oid) {
... on Commit {
oid
associatedPullRequests(first: 10) {
nodes {
number
state
mergedAt
url
title
headRefName
}
}
}
}
}
}
`;
const maxAttempts = 6;
const retryDelayMs = 5000;
const findMergedPr = async () => {
const data = await github.graphql(query, {
owner: context.repo.owner,
repo: context.repo.repo,
oid: context.sha,
});
const commit = data.repository?.object;
if (!commit) {
throw new Error(`Could not load commit ${context.sha}`);
}
const pullRequests = commit.associatedPullRequests?.nodes ?? [];
return pullRequests.find(
(pr) =>
pr.state === "MERGED" &&
typeof pr.mergedAt === "string" &&
pr.mergedAt.length > 0,
);
};
let mergedPr;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
mergedPr = await findMergedPr();
if (mergedPr) {
break;
}
if (attempt < maxAttempts) {
core.info(
`No merged PR associated with ${context.sha} yet (attempt ${attempt}/${maxAttempts}); retrying in ${retryDelayMs / 1000}s.`,
);
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
}
}
if (!mergedPr) {
core.setFailed(
`Direct push detected on main at ${context.sha}. Publish flow requires merged PR commits only.`,
);
return;
}
core.info(
`Publish gate passed via merged PR #${mergedPr.number} (${mergedPr.url})`,
);
const isBumpPr =
(mergedPr.headRefName ?? "").startsWith("ci/version-bump-") ||
/^chore:\s*bump package version to 0\.0\.\d+$/i.test(
mergedPr.title ?? "",
);
core.setOutput("pr_number", String(mergedPr.number));
core.setOutput("pr_url", mergedPr.url ?? "");
core.setOutput("pr_title", mergedPr.title ?? "");
core.setOutput("pr_head_ref", mergedPr.headRefName ?? "");
core.setOutput("is_bump_pr", isBumpPr ? "true" : "false");
core.info(
`Resolved publish mode: ${isBumpPr ? "stable release (latest)" : "prerelease (next)"}`,
);
publish-and-manage-bump:
name: Publish Package + Manage Bump PR
runs-on: ubuntu-latest
needs: resolve-merge-context
permissions:
contents: write
pull-requests: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "22.14.0"
registry-url: "https://registry.npmjs.org"
- name: Upgrade npm for trusted publishing support
run: npm install -g npm@latest
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.10
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Validate package
run: |
bun run check
bun run typecheck
bun test
bun run build
- name: Report publish strategy
run: |
echo "Merged PR #${{ needs.resolve-merge-context.outputs.pr_number }} (${{ needs.resolve-merge-context.outputs.pr_url }})"
echo "Head ref: ${{ needs.resolve-merge-context.outputs.pr_head_ref }}"
echo "Title: ${{ needs.resolve-merge-context.outputs.pr_title }}"
echo "Bump PR: ${{ needs.resolve-merge-context.outputs.is_bump_pr }}"
- name: Resolve package metadata
id: meta
env:
IS_BUMP_PR: ${{ needs.resolve-merge-context.outputs.is_bump_pr }}
run: |
node <<'NODE'
const fs = require("node:fs");
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
const match = /^0\.0\.(\d+)$/.exec(pkg.version ?? "");
const isBumpPr = process.env.IS_BUMP_PR === "true";
const runNumber = process.env.GITHUB_RUN_NUMBER ?? "0";
const runAttempt = process.env.GITHUB_RUN_ATTEMPT ?? "1";
const shortSha = (process.env.GITHUB_SHA ?? "sha").slice(0, 7);
if (!pkg.name) {
console.error("Missing package name in package.json");
process.exit(1);
}
if (!match) {
console.error(
`package.json version must match 0.0.x for this release flow. Received: ${pkg.version}`,
);
process.exit(1);
}
const stableVersion = pkg.version;
const nextStableVersion = `0.0.${Number(match[1]) + 1}`;
const prereleaseVersion = `${nextStableVersion}-next.${runNumber}.${runAttempt}.${shortSha}`;
const publishVersion = isBumpPr ? stableVersion : prereleaseVersion;
const publishTag = isBumpPr ? "latest" : "next";
const createBumpPr = isBumpPr ? "false" : "true";
fs.appendFileSync(process.env.GITHUB_OUTPUT, `name=${pkg.name}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `stable_version=${stableVersion}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `next_stable_version=${nextStableVersion}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `publish_version=${publishVersion}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `publish_tag=${publishTag}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `create_bump_pr=${createBumpPr}\n`);
NODE
- name: Set package version for prerelease publish
if: steps.meta.outputs.create_bump_pr == 'true'
run: |
node <<'NODE'
const fs = require("node:fs");
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
pkg.version = "${{ steps.meta.outputs.publish_version }}";
fs.writeFileSync("package.json", `${JSON.stringify(pkg, null, 2)}\n`);
NODE
- name: Publish package
id: publish
run: |
PACKAGE_REF="${{ steps.meta.outputs.name }}@${{ steps.meta.outputs.publish_version }}"
if npm view "$PACKAGE_REF" version >/dev/null 2>&1; then
echo "Version $PACKAGE_REF already exists on npm; skipping publish."
echo "published=false" >> "$GITHUB_OUTPUT"
exit 0
fi
npm publish --tag "${{ steps.meta.outputs.publish_tag }}"
echo "published=true" >> "$GITHUB_OUTPUT"
- name: Verify published dist-tag
if: steps.publish.outputs.published == 'true'
run: |
PACKAGE_NAME="${{ steps.meta.outputs.name }}"
PUBLISH_TAG="${{ steps.meta.outputs.publish_tag }}"
EXPECTED_VERSION="${{ steps.meta.outputs.publish_version }}"
for attempt in 1 2 3 4 5 6; do
ACTUAL_VERSION="$(npm view "${PACKAGE_NAME}@${PUBLISH_TAG}" version 2>/dev/null || true)"
if [ "$ACTUAL_VERSION" = "$EXPECTED_VERSION" ]; then
echo "Verified dist-tag ${PUBLISH_TAG}: ${PACKAGE_NAME}@${ACTUAL_VERSION}"
exit 0
fi
if [ "$attempt" -eq 6 ]; then
echo "Dist-tag verification failed for ${PACKAGE_NAME}" >&2
echo "Expected ${PUBLISH_TAG} -> ${EXPECTED_VERSION}, found '${ACTUAL_VERSION}'" >&2
exit 1
fi
sleep 5
done
- name: Verify registry install
if: steps.publish.outputs.published == 'true'
run: |
PACKAGE_REF="${{ steps.meta.outputs.name }}@${{ steps.meta.outputs.publish_version }}"
mkdir -p e2e-install
cd e2e-install
npm init -y
for attempt in 1 2 3 4 5 6; do
if npm install "$PACKAGE_REF"; then
break
fi
if [ "$attempt" -eq 6 ]; then
echo "Failed to install $PACKAGE_REF after retries" >&2
exit 1
fi
sleep 10
done
./node_modules/.bin/sandcode --help
./node_modules/.bin/sandcode analyze --help
./node_modules/.bin/sandcode start --help
./node_modules/.bin/sandcode setup --help
- name: Prepare next patch bump
if: steps.meta.outputs.create_bump_pr == 'true'
run: |
node <<'NODE'
const fs = require("node:fs");
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
pkg.version = "${{ steps.meta.outputs.next_stable_version }}";
fs.writeFileSync("package.json", `${JSON.stringify(pkg, null, 2)}\n`);
NODE
- name: Open bump PR
if: steps.meta.outputs.create_bump_pr == 'true'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
branch: ci/version-bump-${{ steps.meta.outputs.next_stable_version }}
draft: true
delete-branch: true
commit-message: "chore: bump package version to ${{ steps.meta.outputs.next_stable_version }}"
title: "chore: bump package version to ${{ steps.meta.outputs.next_stable_version }}"
body: |
Automated post-publish bump.
- Merged PR: #${{ needs.resolve-merge-context.outputs.pr_number }}
- Published `${{ steps.meta.outputs.name }}@${{ steps.meta.outputs.publish_version }}` with npm tag `${{ steps.meta.outputs.publish_tag }}`
- Next stable version prepared: `${{ steps.meta.outputs.next_stable_version }}`
add-paths: |
package.json