Skip to content

Commit 494487c

Browse files
ci: update publishing flows (#1866)
1 parent e51d057 commit 494487c

3 files changed

Lines changed: 172 additions & 35 deletions

File tree

‎.github/scripts/snapshot-version.mjs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
// `<base>-<tag>.<counter>` (or `<base>-pr.<num>.<counter>`). Run between
44
// `changeset version` and `changeset publish --tag <tag>`.
55
//
6-
// Usage: node .github/scripts/snapshot-version.mjs <beta|pr> [--pr <number>]
6+
// Usage: node .github/scripts/snapshot-version.mjs <tag> [--pr <number>]
7+
// <tag> lowercase npm dist-tag (e.g. beta, next). Becomes the suffix
8+
// before the counter: `<base>-<tag>.<N>`.
9+
// --pr <n> required only when <tag> is "pr"; produces `<base>-pr.<n>.<N>`.
710

811
import { readFile, writeFile, readdir, appendFile } from 'node:fs/promises';
912
import { execFileSync } from 'node:child_process';
1013

1114
const [tag, , prNumber] = process.argv.slice(2);
12-
if (!['beta', 'pr'].includes(tag) || (tag === 'pr' && !prNumber)) {
13-
console.error('Usage: snapshot-version.mjs <beta|pr> [--pr <number>]');
15+
const isWord = /^[a-z][a-z0-9]*$/.test(tag || '');
16+
if (!isWord || (tag === 'pr' && !prNumber)) {
17+
console.error('Usage: snapshot-version.mjs <tag> [--pr <number>] (tag must be a lowercase word; --pr is required when tag is "pr")');
1418
process.exit(1);
1519
}
1620

@@ -33,7 +37,7 @@ if (!rep) {
3337
process.exit(1);
3438
}
3539

36-
const prefix = tag === 'beta' ? `${rep.json.version}-beta.` : `${rep.json.version}-pr.${prNumber}.`;
40+
const prefix = tag === 'pr' ? `${rep.json.version}-pr.${prNumber}.` : `${rep.json.version}-${tag}.`;
3741
const version = `${prefix}${nextCounter(rep.json.name, prefix)}`;
3842
console.log(`Snapshot version: ${version} (base from ${rep.json.name})`);
3943

‎.github/workflows/publish.yml‎

Lines changed: 115 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@ name: publish
44
# config for every `@stacks/*` package points at `publish.yml` — renaming this
55
# file requires updating that config on npmjs.com per package.
66
#
7-
# release — push to main / workflow_dispatch.
8-
# changesets/action opens/updates the "chore: version packages"
9-
# PR. When that PR is merged, the same action publishes the
10-
# bumped packages to npm under `latest`. `commitMode: github-api`
11-
# gives commits a Verified badge without signing keys in CI.
7+
# The "chore: version packages" PR itself is opened/updated by `version.yml`;
8+
# that workflow never publishes. Only this file publishes to npm.
129
#
13-
# beta — push to main.
14-
# Snapshot-publishes every push to `@beta`, with versions of the
15-
# form `<next-release>-beta.<N>` (counter scoped to the base
16-
# version, queried from npm).
10+
# Triggers (the `target` dispatch input is classified by `validate-dispatch`):
1711
#
18-
# pr — pull_request.
19-
# Same as `beta` but `@pr` dist-tag and versions of the form
20-
# `<next-release>-pr.<PR#>.<N>`. Sticky-header'd into the PR
21-
# description so reviewers can install the canary build.
12+
# release — auto on push to main where the head commit is "chore: version
13+
# packages" (i.e. the version PR was just merged), OR
14+
# workflow_dispatch from main with `target` empty.
15+
# Publishes the bumped packages to npm under `latest`, pushes
16+
# per-package git tags, and creates a GitHub Release per package.
17+
# `changeset publish` only publishes versions that aren't already
18+
# on npm (npm itself forbids overwriting), so a manual dispatch
19+
# here is safe to re-run when a previous release left some
20+
# packages unpublished — it'll fill the gaps and no-op for the
21+
# rest.
22+
#
23+
# snapshot — workflow_dispatch with `target=main` (publishes from main as
24+
# the `@beta` dist-tag, by convention) or `target=<word>`
25+
# (publishes from branch `<word>` as the `@<word>` dist-tag).
26+
# Versions look like `<next-release>-<tag>.<N>` where `<tag>`
27+
# is the dist-tag and `<N>` is a per-tag counter queried from
28+
# npm. Use for ad-hoc previews from a long-lived release branch
29+
# (e.g. `next`).
30+
#
31+
# pr — workflow_dispatch with `target=<PR number>`.
32+
# Publishes a `@pr` canary built from that PR. Versions look
33+
# like `<next-release>-pr.<PR#>.<N>`. Sticky-header'd into the
34+
# PR description so reviewers can install the canary.
2235
#
2336
# Forks never reach the publish step (`is-not-fork` gate from pre-run).
2437

@@ -33,8 +46,13 @@ on:
3346

3447
workflow_dispatch:
3548
inputs:
36-
pr-number:
37-
description: 'PR number to publish a canary build for (leave blank for a normal release dispatch)'
49+
target:
50+
description: |
51+
What to publish.
52+
empty — production release to `latest` (only valid from main)
53+
main — `@beta` snapshot from main
54+
<word> — `@<word>` snapshot from branch `<word>` (lowercase letters/digits, must start with a letter)
55+
<PR number> — `@pr` canary for that PR
3856
type: string
3957
required: false
4058
default: ''
@@ -51,10 +69,55 @@ jobs:
5169
uses: ./.github/workflows/pre-run.yml
5270
secrets: inherit
5371

72+
# Classify the dispatch `target` input into a `kind`:
73+
# release — empty target (manual production publish)
74+
# pr — positive integer (PR canary)
75+
# snapshot — lowercase word (`main`, `next`, etc. — published as @<word>,
76+
# with `main` re-mapped to the `@beta` dist-tag in the snapshot
77+
# job itself)
78+
# Anything else fails the workflow up front so a publish job never starts
79+
# against a typo'd target.
80+
validate-dispatch:
81+
runs-on: ubuntu-latest
82+
if: github.event_name == 'workflow_dispatch'
83+
outputs:
84+
kind: ${{ steps.classify.outputs.kind }}
85+
steps:
86+
- id: classify
87+
env:
88+
TARGET: ${{ inputs.target }}
89+
run: |
90+
# `pr` and `latest` are reserved dist-tags managed by other code paths
91+
# (the `pr` job, and the `release` flow respectively); reject them as
92+
# snapshot targets so a typo can't bypass those flows.
93+
if [ -z "$TARGET" ]; then
94+
kind=release
95+
elif printf '%s' "$TARGET" | grep -Eq '^[1-9][0-9]*$'; then
96+
kind=pr
97+
elif [ "$TARGET" = "pr" ] || [ "$TARGET" = "latest" ]; then
98+
echo "::error::target '$TARGET' is a reserved dist-tag; use a PR number for a pr canary, or merge the version PR for a latest release"
99+
exit 1
100+
elif printf '%s' "$TARGET" | grep -Eq '^[a-z][a-z0-9]*$'; then
101+
kind=snapshot
102+
else
103+
echo "::error::target must be empty, a positive PR number, or a single-word lowercase branch name (e.g. 'next'); got '$TARGET'"
104+
exit 1
105+
fi
106+
if [ "$kind" = "release" ] && [ "$GITHUB_REF_NAME" != "main" ]; then
107+
echo "::error::release dispatch (empty target) must run from main; current ref is '$GITHUB_REF_NAME'"
108+
exit 1
109+
fi
110+
echo "Validated target='$TARGET' as kind=$kind"
111+
echo "kind=$kind" >> "$GITHUB_OUTPUT"
112+
54113
release:
55114
runs-on: ubuntu-latest
56-
needs: pre-run
57-
if: needs.pre-run.outputs.is-not-fork == 'true' && (github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && inputs.pr-number == ''))
115+
needs: [pre-run, validate-dispatch]
116+
# `!failure() && !cancelled()` lets this job run when `validate-dispatch`
117+
# is `skipped` (push trigger), but still blocks on a real failure of
118+
# `pre-run` or `validate-dispatch`. `ref_name == 'main'` keeps manual
119+
# dispatches from accidentally publishing `latest` from a stale branch.
120+
if: "!failure() && !cancelled() && needs.pre-run.outputs.is-not-fork == 'true' && github.ref_name == 'main' && ((github.event_name == 'push' && startsWith(github.event.head_commit.message, 'chore: version packages')) || (github.event_name == 'workflow_dispatch' && needs.validate-dispatch.outputs.kind == 'release'))"
58121
steps:
59122
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
60123
with:
@@ -67,24 +130,44 @@ jobs:
67130

68131
- run: npm ci
69132

70-
- name: Create release PR or publish
133+
# `npm run release` runs `npm run build && changeset publish`. We use
134+
# changesets/action only as the publish runner — it pushes the per-package
135+
# git tags that `changeset publish` creates locally and opens a GitHub
136+
# Release per package. No `version:` input: version bumps happen in
137+
# `version.yml`, never here. NPM_CONFIG_IGNORE_SCRIPTS skips each
138+
# package's prepublishOnly hook (avoids duplicate test/build work and the
139+
# partial-publish failure mode we hit on 2026-05-22).
140+
- name: Publish
71141
uses: changesets/action@63a615b9cd06ba9a3e6d13796c7fbcb080a60a0b # v1.8.0
72142
with:
73-
version: npm run version
74143
publish: npm run release
75-
title: 'chore: version packages'
76-
commit: 'chore: version packages'
77-
commitMode: github-api
78144
env:
79145
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
146+
NPM_CONFIG_IGNORE_SCRIPTS: 'true'
80147

81-
beta:
148+
snapshot:
82149
runs-on: ubuntu-latest
83-
needs: pre-run
84-
if: needs.pre-run.outputs.is-not-fork == 'true' && github.event_name == 'push'
150+
needs: [pre-run, validate-dispatch]
151+
if: "needs.pre-run.outputs.is-not-fork == 'true' && needs.validate-dispatch.outputs.kind == 'snapshot'"
85152
steps:
153+
# Map target → (ref to publish from, dist-tag to publish under).
154+
# `main` is the canonical beta channel, so it gets the `beta` tag.
155+
# Any other word doubles as both the branch and the dist-tag.
156+
- id: target
157+
env:
158+
TARGET: ${{ inputs.target }}
159+
run: |
160+
if [ "$TARGET" = "main" ]; then
161+
echo "ref=main" >> "$GITHUB_OUTPUT"
162+
echo "tag=beta" >> "$GITHUB_OUTPUT"
163+
else
164+
echo "ref=$TARGET" >> "$GITHUB_OUTPUT"
165+
echo "tag=$TARGET" >> "$GITHUB_OUTPUT"
166+
fi
167+
86168
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
87169
with:
170+
ref: ${{ steps.target.outputs.ref }}
88171
fetch-depth: 0
89172

90173
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
@@ -107,7 +190,7 @@ jobs:
107190
fi
108191
109192
- run: npx changeset version
110-
- run: node .github/scripts/snapshot-version.mjs beta
193+
- run: node .github/scripts/snapshot-version.mjs ${{ steps.target.outputs.tag }}
111194

112195
- name: Build packages
113196
run: |
@@ -118,18 +201,19 @@ jobs:
118201
119202
# NPM_CONFIG_IGNORE_SCRIPTS skips each package's prepublishOnly hook,
120203
# avoiding duplicate test/build work during publish.
121-
- run: npx changeset publish --tag beta --no-git-tag
204+
- run: npx changeset publish --tag ${{ steps.target.outputs.tag }} --no-git-tag
122205
env:
123206
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
124207
NPM_CONFIG_IGNORE_SCRIPTS: 'true'
125208

126209
pr:
127210
runs-on: ubuntu-latest
128-
needs: pre-run
129-
if: needs.pre-run.outputs.is-not-fork == 'true' && github.event_name == 'workflow_dispatch' && inputs.pr-number != ''
211+
needs: [pre-run, validate-dispatch]
212+
if: "needs.pre-run.outputs.is-not-fork == 'true' && needs.validate-dispatch.outputs.kind == 'pr'"
130213
steps:
131214
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
132215
with:
216+
ref: refs/pull/${{ inputs.target }}/merge
133217
fetch-depth: 0
134218

135219
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
@@ -153,7 +237,7 @@ jobs:
153237
154238
- run: npx changeset version
155239
- id: snapshot
156-
run: node .github/scripts/snapshot-version.mjs pr --pr ${{ inputs.pr-number }}
240+
run: node .github/scripts/snapshot-version.mjs pr --pr ${{ inputs.target }}
157241

158242
- name: Build packages
159243
run: |
@@ -173,5 +257,5 @@ jobs:
173257
header: |
174258
> This PR was published to npm with the version `${{ steps.snapshot.outputs.version }}` (dist-tag `pr`).
175259
> e.g. `npm install @stacks/common@${{ steps.snapshot.outputs.version }} --save-exact`
176-
pr-number: ${{ inputs.pr-number }}
260+
pr-number: ${{ inputs.target }}
177261
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.github/workflows/version.yml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: version
2+
3+
# Opens/updates the "chore: version packages" PR via changesets/action.
4+
# This workflow NEVER publishes — no `publish:` input is passed to the action,
5+
# so the action has no path to npm even if local versions drift. The actual
6+
# `latest` publish happens in `publish.yml` when this PR is merged (gated on
7+
# the head commit subject "chore: version packages").
8+
#
9+
# `commitMode: github-api` makes the PR commit show up as Verified without
10+
# shipping signing keys to CI.
11+
12+
on:
13+
push:
14+
branches:
15+
- main
16+
paths-ignore:
17+
- '**.md'
18+
19+
permissions:
20+
contents: write
21+
pull-requests: write
22+
23+
concurrency: ${{ github.workflow }}-${{ github.ref }}
24+
25+
jobs:
26+
version-pr:
27+
runs-on: ubuntu-latest
28+
# Don't try to open a version PR on top of the version commit itself.
29+
if: "!startsWith(github.event.head_commit.message, 'chore: version packages')"
30+
steps:
31+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
32+
with:
33+
fetch-depth: 0
34+
35+
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
36+
with:
37+
node-version: 24
38+
cache: npm
39+
40+
- run: npm ci
41+
42+
- uses: changesets/action@63a615b9cd06ba9a3e6d13796c7fbcb080a60a0b # v1.8.0
43+
with:
44+
version: npm run version
45+
title: 'chore: version packages'
46+
commit: 'chore: version packages'
47+
commitMode: github-api
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)