Skip to content

Commit 23410a4

Browse files
authored
feat: enable auto-publishing on NPM after a GitHub release (#1051)
* feat: enable auto-publishing on NPM after a GitHub release * chore: address review * chore: specify PNPM version and bump Node version in CI * chore: remove redundant version due to a CI error * chore: simplify publish workflow * temp: dry-run * temp: add mock tag for dry-run of NPM publish * Revert "temp: add mock tag for dry-run of NPM publish" This reverts commit 8a458fe. * Revert "temp: dry-run" This reverts commit 3629dc2.
1 parent d8f15f0 commit 23410a4

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

.github/workflows/ci.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ on:
77
branches: [main]
88

99
env:
10-
NODE_VER: 22.11
10+
NODE_VER: 22.18
1111
CI: true
1212

1313
jobs:
1414
test-module:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v4
18+
- uses: actions/checkout@v5
1919

2020
- uses: pnpm/action-setup@v4
2121
name: Install pnpm
@@ -26,7 +26,7 @@ jobs:
2626
uses: actions/setup-node@v4
2727
with:
2828
node-version: ${{ env.NODE_VER }}
29-
cache: "pnpm"
29+
cache: 'pnpm'
3030

3131
- name: Install deps and prepare types
3232
run: pnpm i && pnpm dev:prepare
@@ -37,7 +37,7 @@ jobs:
3737
# Check linting and typing
3838
- run: pnpm lint
3939
- run: pnpm typecheck
40-
40+
4141
# Run unit tests
4242
- run: pnpm test:unit
4343

@@ -48,7 +48,7 @@ jobs:
4848
runs-on: ubuntu-latest
4949

5050
steps:
51-
- uses: actions/checkout@v4
51+
- uses: actions/checkout@v5
5252

5353
- uses: pnpm/action-setup@v4
5454
name: Install pnpm
@@ -74,7 +74,7 @@ jobs:
7474
working-directory: ./playground-local
7575

7676
steps:
77-
- uses: actions/checkout@v4
77+
- uses: actions/checkout@v5
7878

7979
- uses: pnpm/action-setup@v4
8080
name: Install pnpm
@@ -110,7 +110,7 @@ jobs:
110110
run:
111111
working-directory: ./playground-authjs
112112
steps:
113-
- uses: actions/checkout@v4
113+
- uses: actions/checkout@v5
114114

115115
- uses: pnpm/action-setup@v4
116116
name: Install pnpm

.github/workflows/release.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Release on NPM
2+
3+
on:
4+
release:
5+
types: [published] # runs when a GitHub Release is published
6+
7+
permissions:
8+
contents: read
9+
id-token: write # required for npm provenance
10+
11+
env:
12+
NODE_VER: 22.18
13+
CI: true
14+
15+
jobs:
16+
publish:
17+
name: Publish package from release tag
18+
# Run only when tag is in the format `vX.Y.Z` produced by `npm version`
19+
if: startsWith(github.event.release.tag_name, 'v')
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Check out the tag referenced by this release
24+
uses: actions/checkout@v5
25+
with:
26+
ref: ${{ github.event.release.tag_name }}
27+
fetch-depth: 0
28+
29+
- name: Install pnpm
30+
uses: pnpm/action-setup@v4
31+
with:
32+
run_install: false
33+
34+
- name: Setup Node.js and pnpm
35+
uses: actions/setup-node@v4
36+
with:
37+
node-version: ${{ env.NODE_VER }}
38+
cache: 'pnpm'
39+
# This is required for `setup-node` to generate the registry URL into .npmrc
40+
# See https://github.com/actions/setup-node/blob/5e2628c959b9ade56971c0afcebbe5332d44b398/action.yml#L17-L18
41+
registry-url: 'https://registry.npmjs.org/'
42+
43+
- name: Verify tag matches package.json version
44+
run: |
45+
TAG="${{ github.event.release.tag_name }}"
46+
PKG_VERSION=$(node -p "require('./package.json').version")
47+
if [ "v$PKG_VERSION" != "$TAG" ]; then
48+
echo "::error ::Tag ($TAG) does not match package.json version (v$PKG_VERSION)"
49+
exit 1
50+
fi
51+
52+
- name: Install deps
53+
run: |
54+
pnpm --version
55+
pnpm install --frozen-lockfile
56+
pnpm dev:prepare
57+
58+
# Note: no build step because npm publish would run `prepack` script which builds the module
59+
60+
- name: Publish to npm with provenance
61+
env:
62+
# Environment variable used by `setup-node` action
63+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
64+
run: |
65+
TAG="${{ github.event.release.tag_name }}"
66+
67+
# Stable release (vX.Y.Z)
68+
if echo "$TAG" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
69+
npm publish --provenance --access public
70+
71+
# Pre-release (vX.Y.Z-*)
72+
elif echo "$TAG" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+-'; then
73+
npm publish --provenance --access public --tag next
74+
75+
else
76+
echo "Not a valid release tag ($TAG), skipping publish."
77+
fi
78+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@
6464
"vitest": "^3.2.4",
6565
"vue-tsc": "^2.2.12"
6666
},
67-
"packageManager": "pnpm@9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
67+
"packageManager": "pnpm@10.15.0+sha512.486ebc259d3e999a4e8691ce03b5cac4a71cbeca39372a9b762cb500cfdf0873e2cb16abe3d951b1ee2cf012503f027b98b6584e4df22524e0c7450d9ec7aa7b"
6868
}

0 commit comments

Comments
 (0)