Skip to content

Commit 9f1e5a9

Browse files
ci: beta pr releases (#1863)
* ci: fix broken actions * ci: switch to changesets * chore: update lock file * ci: update workflows * ci: update release scripts * ci: re-add sticky header on PR canary * ci: simplify header * ci: fix seed * ci: change publish location * ci: rename flows * chore: fix repository link * docs: fix shield * ci: update workflow dispatch * ci: simplify build
1 parent 6440bc5 commit 9f1e5a9

14 files changed

Lines changed: 235 additions & 173 deletions

File tree

‎.changeset/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

‎.changeset/config.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/config@2.3.1/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [["@stacks/*"]],
6+
"linked": [],
7+
"access": "public",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
11+
}

‎.github/DEVELOPMENT.md‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
## Adding dependencies
22

3-
This repo uses Lerna [hoisting](https://github.com/lerna/lerna/blob/main/doc/hoist.md) for package dependencies.
3+
This repo uses [npm workspaces](https://docs.npmjs.com/cli/v10/using-npm/workspaces) for package management.
44

5-
In order to install a new dependency to a package, the [`lerna add`](https://github.com/lerna/lerna/tree/main/commands/add) command must be used, rather than `npm install <package>`.
6-
7-
For example, the following command installs `lodash` as a dependency to the `@stacks/storage` package:
5+
To install a new dependency to a specific package, use the `-w` (workspace) flag:
86

97
```shell
108
# Run within the root directory
11-
npx lerna add lodash --scope @stacks/storage
9+
npm install lodash -w @stacks/storage
1210
```
1311

14-
Add `--dev` to install as a development dependency:
12+
Add `--save-dev` to install as a development dependency:
1513

1614
```shell
17-
npx lerna add lodash --scope @stacks/storage --dev
15+
npm install lodash -w @stacks/storage --save-dev
1816
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
// Rewrite packages/*/package.json to a snapshot version like
3+
// `<base>-<tag>.<counter>` (or `<base>-pr.<num>.<counter>`). Run between
4+
// `changeset version` and `changeset publish --tag <tag>`.
5+
//
6+
// Usage: node .github/scripts/snapshot-version.mjs <beta|pr> [--pr <number>]
7+
8+
import { readFile, writeFile, readdir, appendFile } from 'node:fs/promises';
9+
import { execFileSync } from 'node:child_process';
10+
11+
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>]');
14+
process.exit(1);
15+
}
16+
17+
const DEP_TYPES = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
18+
19+
const entries = await readdir('packages', { withFileTypes: true });
20+
const pkgs = (await Promise.all(
21+
entries
22+
.filter((e) => e.isDirectory())
23+
.map((e) => `packages/${e.name}/package.json`)
24+
.map(async (file) => {
25+
try { return { file, json: JSON.parse(await readFile(file, 'utf8')) }; }
26+
catch { return null; }
27+
}),
28+
)).filter(Boolean);
29+
30+
const rep = pkgs.find(({ json }) => json.name && !json.private && /^\d+\.\d+\.\d+$/.test(json.version));
31+
if (!rep) {
32+
console.error('No non-private workspace package with plain semver — did you run `changeset version` first?');
33+
process.exit(1);
34+
}
35+
36+
const prefix = tag === 'beta' ? `${rep.json.version}-beta.` : `${rep.json.version}-pr.${prNumber}.`;
37+
const version = `${prefix}${nextCounter(rep.json.name, prefix)}`;
38+
console.log(`Snapshot version: ${version} (base from ${rep.json.name})`);
39+
40+
const internals = new Set(pkgs.map(({ json }) => json.name).filter(Boolean));
41+
const named = pkgs.filter(({ json }) => json.name);
42+
43+
await Promise.all(named.map(({ file, json }) =>
44+
writeFile(file, JSON.stringify(rewrite(json, version, internals), null, 2) + '\n'),
45+
));
46+
named.forEach(({ json }) => console.log(` ${json.name} → ${version}`));
47+
48+
if (process.env.GITHUB_OUTPUT) await appendFile(process.env.GITHUB_OUTPUT, `version=${version}\n`);
49+
50+
function rewrite(json, version, internals) {
51+
const rewriteDeps = (deps) =>
52+
Object.fromEntries(Object.entries(deps).map(([n, v]) => [n, internals.has(n) ? version : v]));
53+
return DEP_TYPES.reduce(
54+
(acc, dt) => (acc[dt] ? { ...acc, [dt]: rewriteDeps(acc[dt]) } : acc),
55+
{ ...json, version },
56+
);
57+
}
58+
59+
function nextCounter(name, prefix) {
60+
let raw;
61+
try {
62+
raw = execFileSync('npm', ['view', name, 'versions', '--json'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
63+
} catch (e) {
64+
if ((e.stderr || '').includes('E404')) return 0;
65+
throw e;
66+
}
67+
const all = JSON.parse(raw);
68+
const counters = (Array.isArray(all) ? all : [all])
69+
.filter((v) => v.startsWith(prefix))
70+
.map((v) => Number(v.slice(prefix.length)))
71+
.filter(Number.isInteger);
72+
return counters.length ? Math.max(...counters) + 1 : 0;
73+
}

‎.github/workflows/publish.yml‎

Lines changed: 127 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,177 @@
11
name: publish
22

3+
# All npm-publishing flows live in this single file. npm's trusted-publisher
4+
# config for every `@stacks/*` package points at `publish.yml` — renaming this
5+
# file requires updating that config on npmjs.com per package.
6+
#
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.
12+
#
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).
17+
#
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.
22+
#
23+
# Forks never reach the publish step (`is-not-fork` gate from pre-run).
24+
325
on:
426
push:
527
branches:
628
- main
729
tags-ignore:
8-
- "**"
30+
- '**'
931
paths-ignore:
10-
- "**.md"
32+
- '**.md'
1133

1234
workflow_dispatch:
35+
inputs:
36+
pr-number:
37+
description: 'PR number to publish a canary build for (leave blank for a normal release dispatch)'
38+
type: string
39+
required: false
40+
default: ''
1341

1442
permissions:
15-
id-token: write # Required for OIDC
16-
contents: read
43+
contents: write
44+
pull-requests: write
45+
id-token: write # npm OIDC trusted publishing
46+
47+
concurrency: ${{ github.workflow }}-${{ github.ref }}
1748

1849
jobs:
1950
pre-run:
2051
uses: ./.github/workflows/pre-run.yml
2152
secrets: inherit
2253

23-
publish:
54+
release:
55+
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 == ''))
58+
steps:
59+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
60+
with:
61+
fetch-depth: 0
62+
63+
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
64+
with:
65+
node-version: 24
66+
cache: npm
67+
68+
- run: npm ci
69+
70+
- name: Create release PR or publish
71+
uses: changesets/action@63a615b9cd06ba9a3e6d13796c7fbcb080a60a0b # v1.8.0
72+
with:
73+
version: npm run version
74+
publish: npm run release
75+
title: 'chore: version packages'
76+
commit: 'chore: version packages'
77+
commitMode: github-api
78+
env:
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80+
81+
beta:
2482
runs-on: ubuntu-latest
2583
needs: pre-run
26-
if: "needs.pre-run.outputs.is-not-fork == 'true' && (github.event_name == 'workflow_dispatch' || contains(github.event.head_commit.message, 'chore: version packages'))"
84+
if: needs.pre-run.outputs.is-not-fork == 'true' && github.event_name == 'push'
2785
steps:
2886
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2987
with:
3088
fetch-depth: 0
89+
3190
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
3291
with:
3392
node-version: 24
3493
cache: npm
3594

3695
- run: npm ci
37-
- run: npm run build
3896

39-
- run: git config --local user.email 'action@github.com'
40-
- run: git config --local user.name 'GitHub Action'
97+
- name: Seed dummy changeset if none present
98+
run: |
99+
if ! ls .changeset/*.md 2>/dev/null | grep -vqi readme; then
100+
cat > .changeset/__snapshot.md <<'EOF'
101+
---
102+
"@stacks/common": patch
103+
---
104+
105+
snapshot
106+
EOF
107+
fi
41108
42-
- name: Publish
109+
- run: npx changeset version
110+
- run: node .github/scripts/snapshot-version.mjs beta
111+
112+
- name: Build packages
43113
run: |
44-
npx lerna publish from-package --no-verify-access --yes
45-
RELEASE=$(cat lerna.json | jq -r '.version')
46-
git tag -a v$RELEASE -m v$RELEASE
47-
git push origin v$RELEASE
114+
npm run clean --workspaces --if-present
115+
npm run build:cjs --workspaces --if-present
116+
npm run build:esm --workspaces --if-present
117+
npm run build:umd --workspaces --if-present
118+
119+
# NPM_CONFIG_IGNORE_SCRIPTS skips each package's prepublishOnly hook,
120+
# avoiding duplicate test/build work during publish.
121+
- run: npx changeset publish --tag beta --no-git-tag
122+
env:
123+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
124+
NPM_CONFIG_IGNORE_SCRIPTS: 'true'
48125

49-
publish-beta:
126+
pr:
50127
runs-on: ubuntu-latest
51128
needs: pre-run
52-
if: "needs.pre-run.outputs.is-not-fork == 'true' && !contains(github.event.head_commit.message, 'chore: version packages')"
129+
if: needs.pre-run.outputs.is-not-fork == 'true' && github.event_name == 'workflow_dispatch' && inputs.pr-number != ''
53130
steps:
54131
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
55132
with:
56133
fetch-depth: 0
57134

58-
# pulls all tags (needed for lerna to correctly version)
59-
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
60-
61135
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
62136
with:
63137
node-version: 24
64138
cache: npm
65139

66140
- run: npm ci
67141

68-
- run: git config --local user.email 'action@github.com'
69-
- run: git config --local user.name 'GitHub Action'
142+
- name: Seed dummy changeset if none present
143+
run: |
144+
if ! ls .changeset/*.md 2>/dev/null | grep -vqi readme; then
145+
cat > .changeset/__snapshot.md <<'EOF'
146+
---
147+
"@stacks/common": patch
148+
---
149+
150+
snapshot
151+
EOF
152+
fi
153+
154+
- run: npx changeset version
155+
- id: snapshot
156+
run: node .github/scripts/snapshot-version.mjs pr --pr ${{ inputs.pr-number }}
70157

71-
- name: Version & Publish
158+
- name: Build packages
72159
run: |
73-
LATEST=$(git describe --tags --abbrev=0)
74-
npx lerna version $LATEST --force-publish --no-changelog --no-push --no-git-tag-version --yes
75-
git commit -a -m 'DONT PUSH: fast-forward to latest npm release' --allow-empty --no-verify
76-
npx lerna version --force-publish --no-changelog --conventional-commits --conventional-prerelease --preid beta --no-push --no-git-tag-version --yes
77-
git commit -a -m 'DONT PUSH: version beta release' --allow-empty --no-verify
78-
RELEASE=$(cat lerna.json | jq -r '.version')
79-
npx lerna publish from-package --no-verify-access --pre-dist-tag beta --yes
80-
git reset HEAD~2 --hard
81-
git tag v$RELEASE
82-
git push origin v$RELEASE
160+
npm run clean --workspaces --if-present
161+
npm run build:cjs --workspaces --if-present
162+
npm run build:esm --workspaces --if-present
163+
npm run build:umd --workspaces --if-present
164+
165+
- run: npx changeset publish --tag pr --no-git-tag
166+
env:
167+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
168+
NPM_CONFIG_IGNORE_SCRIPTS: 'true'
169+
170+
- name: Update PR sticky header
171+
uses: janniks/pull-request-fixed-header@57bc082ecbd040df344747d5b8644af5fad02c95 # v1.2.0
172+
with:
173+
header: |
174+
> This PR was published to npm with the version `${{ steps.snapshot.outputs.version }}` (dist-tag `pr`).
175+
> e.g. `npm install @stacks/common@${{ steps.snapshot.outputs.version }} --save-exact`
176+
pr-number: ${{ inputs.pr-number }}
177+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.github/workflows/pull-request.yml‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,3 @@ jobs:
2626
needs: pre-run
2727
uses: ./.github/workflows/tests.yml
2828
secrets: inherit
29-
30-
# FOR NOW DISABLE PUBLISHING TO NPM ON EACH PULL-REQUEST PUSH
31-
# publish-pr:
32-
# runs-on: ubuntu-latest
33-
# needs: [pre-run, tests]
34-
# if: needs.pre-run.outputs.is-not-fork == 'true'
35-
# steps:
36-
# - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
37-
# if: github.event_name == 'pull_request'
38-
# with:
39-
# ref: ${{ github.event.pull_request.head.ref }}
40-
# fetch-depth: 0
41-
# - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
42-
# if: github.event_name == 'workflow_dispatch'
43-
# with:
44-
# fetch-depth: 0
45-
# - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
46-
# with:
47-
# node-version: 24
48-
# cache: npm
49-
# - run: npm install -g npm@latest && npm ci
50-
# - name: Set up npm auth token
51-
# run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
52-
# env:
53-
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
54-
# - name: Publish to npm (feature branch)
55-
# id: publish-pr
56-
# run: |
57-
# PUBLISH_OUTPUT_FILE=$(mktemp)
58-
# npx lerna publish --canary --force-publish --preid pr --dist-tag pr --no-verify-access --no-push --no-git-tag-version --yes | tee $PUBLISH_OUTPUT_FILE
59-
# VERSION=$(grep -oP '@stacks/common\s*=>\s*\K[^\s]+' $PUBLISH_OUTPUT_FILE | head -n 1)
60-
# echo "Canary version: $VERSION"
61-
# echo "::set-output name=version::$VERSION"
62-
# if: ${{ (github.head_ref || github.ref_name) != 'nakamoto' && (github.head_ref || github.ref_name) != 'next' }}
63-
# env:
64-
# SKIP_TESTS: true
65-
# - name: Publish to npm (next/nakamoto)
66-
# run: |
67-
# git reset --hard
68-
# npx lerna publish --canary major --force-publish --preid $BRANCH --dist-tag $BRANCH --no-verify-access --no-push --no-git-tag-version --yes
69-
# if: ${{ (github.head_ref || github.ref_name) == 'nakamoto' || (github.head_ref || github.ref_name) == 'next' }}
70-
# env:
71-
# BRANCH: ${{ github.head_ref || github.ref_name }}
72-
# SKIP_TESTS: true
73-
# - name: Edit pull-request description with published version
74-
# uses: janniks/pull-request-fixed-header@v1.0.1
75-
# with:
76-
# header: "> This PR was published to npm with the version `${{ steps.publish-pr.outputs.version || env.BRANCH }}`\n> e.g. `npm install @stacks/common@${{ steps.publish-pr.outputs.version || env.BRANCH }} --save-exact`"
77-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78-
# env:
79-
# BRANCH: ${{ github.head_ref || github.ref_name }}

‎.github/workflows/tests.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: npm run madge
4141

4242
- name: Run tests
43-
run: npm run lerna run test --stream --parallel -- -- --coverage
43+
run: npm run test --workspaces --if-present -- --coverage
4444

4545
- name: Upload test coverage
4646
uses: codecov/codecov-action@57e3a136b779b570ffcdbf80b3bdc90e7fab3de2

0 commit comments

Comments
 (0)