Skip to content

Commit 545107f

Browse files
committed
Publish to npm from CI with trusted publishing
1 parent 3e41894 commit 545107f

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish to npm
2+
3+
# Publishes when a version bump lands on main. Authentication is npm trusted
4+
# publishing (OIDC), so the repo holds no npm token; the package's trusted
5+
# publisher on npmjs.com must name this repo and this workflow file.
6+
7+
on:
8+
push:
9+
branches: [main]
10+
paths:
11+
- package.json
12+
- .github/workflows/publish.yml
13+
workflow_dispatch:
14+
15+
permissions:
16+
id-token: write
17+
contents: read
18+
19+
jobs:
20+
publish:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v6
24+
25+
- uses: actions/setup-node@v6
26+
with:
27+
node-version: '24'
28+
registry-url: 'https://registry.npmjs.org'
29+
30+
- run: npm ci
31+
32+
- run: npm run check
33+
34+
- name: Skip a version that is already published
35+
id: version
36+
run: |
37+
NAME=$(node -p "require('./package.json').name")
38+
VERSION=$(node -p "require('./package.json').version")
39+
if npm view "$NAME@$VERSION" version > /dev/null 2>&1; then
40+
echo "exists=true" >> "$GITHUB_OUTPUT"
41+
echo "$NAME@$VERSION is already on npm"
42+
else
43+
echo "exists=false" >> "$GITHUB_OUTPUT"
44+
fi
45+
46+
- if: steps.version.outputs.exists == 'false'
47+
run: npm publish --provenance --access public

0 commit comments

Comments
 (0)