Skip to content

Commit 38835c4

Browse files
committed
Simplify npm publish workflow
1 parent cbf1ba7 commit 38835c4

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

.github/workflows/publish.yml

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ permissions:
1111
artifact-metadata: write
1212

1313
jobs:
14-
publish:
15-
name: publish to npmjs
14+
check:
15+
name: check npm package version
1616
if: github.event.release.prerelease == false
1717
runs-on: ubuntu-latest
18+
outputs:
19+
should_publish: ${{ steps.npm_version.outputs.should_publish }}
1820

1921
steps:
2022
- name: checkout
@@ -25,13 +27,37 @@ jobs:
2527
with:
2628
node-version: 24
2729
registry-url: https://registry.npmjs.org
28-
cache: npm
2930

30-
- name: verify publish toolchain
31+
- name: check npm package version
32+
id: npm_version
3133
run: |
32-
node --version
33-
npm --version
34-
node -e "const version = require('node:child_process').execFileSync('npm', ['--version'], { encoding: 'utf8' }).trim(); const [major, minor, patch] = version.split('.').map(Number); if (major < 11 || (major === 11 && (minor < 5 || (minor === 5 && patch < 1)))) throw new Error('npm 11.5.1 or newer is required for trusted publishing')"
34+
package_name="$(node -p "require('./package.json').name")"
35+
package_version="$(node -p "require('./package.json').version")"
36+
37+
if npm view "${package_name}@${package_version}" version --json >/dev/null 2>&1; then
38+
echo "${package_name}@${package_version} is already published; exiting without publishing."
39+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
40+
else
41+
echo "${package_name}@${package_version} is not published yet; continuing."
42+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
43+
fi
44+
45+
publish:
46+
name: publish to npmjs
47+
needs: check
48+
if: needs.check.outputs.should_publish == 'true'
49+
runs-on: ubuntu-latest
50+
51+
steps:
52+
- name: checkout
53+
uses: actions/checkout@v6
54+
55+
- name: setup node 24
56+
uses: actions/setup-node@v6
57+
with:
58+
node-version: 24
59+
registry-url: https://registry.npmjs.org
60+
cache: npm
3561

3662
- name: install dependencies
3763
run: npm ci
@@ -45,22 +71,7 @@ jobs:
4571
- name: verify runtime dependency tree
4672
run: npm ls --omit=dev --all
4773

48-
- name: check npm package version
49-
id: npm_version
50-
run: |
51-
package_name="$(node -p "require('./package.json').name")"
52-
package_version="$(node -p "require('./package.json').version")"
53-
54-
if npm view "${package_name}@${package_version}" version --json >/dev/null 2>&1; then
55-
echo "exists=true" >> "$GITHUB_OUTPUT"
56-
else
57-
echo "exists=false" >> "$GITHUB_OUTPUT"
58-
fi
59-
60-
echo "package=${package_name}@${package_version}" >> "$GITHUB_OUTPUT"
61-
6274
- name: pack package
63-
if: steps.npm_version.outputs.exists == 'false'
6475
id: pack
6576
run: |
6677
mkdir -p dist
@@ -70,21 +81,18 @@ jobs:
7081
echo "tarball=$tarball" >> "$GITHUB_OUTPUT"
7182
7283
- name: attest npm package artifact
73-
if: steps.npm_version.outputs.exists == 'false'
7484
uses: actions/attest@v4
7585
with:
7686
subject-path: ${{ steps.pack.outputs.tarball }}
7787

7888
- name: upload npm package artifact
79-
if: steps.npm_version.outputs.exists == 'false'
8089
uses: actions/upload-artifact@v4
8190
with:
8291
name: npm-package
8392
path: ${{ steps.pack.outputs.tarball }}
8493
if-no-files-found: error
8594

8695
- name: publish package
87-
if: steps.npm_version.outputs.exists == 'false'
8896
run: npm publish "$TARBALL" --provenance --access public
8997
env:
9098
TARBALL: ${{ steps.pack.outputs.tarball }}

0 commit comments

Comments
 (0)