Skip to content

Commit 1259ebe

Browse files
committed
chore: update NPM publish workflow to trigger on release events and handle prerelease tags
1 parent 87f2628 commit 1259ebe

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

.github/workflows/npmpublish.yml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Publish to NPM
22

33
on:
4-
push:
5-
tags:
6-
- 'v*.*.*'
4+
release:
5+
types:
6+
- published
77

88
permissions:
99
contents: read
@@ -13,12 +13,16 @@ jobs:
1313
publish-npm:
1414
environment: production
1515
runs-on: ubuntu-latest
16+
env:
17+
RELEASE_TAG: ${{ github.event.release.tag_name }}
18+
IS_PRERELEASE: ${{ github.event.release.prerelease }}
1619
steps:
1720
- name: Checkout (no repo token persisted)
1821
uses: actions/checkout@v4
1922
with:
2023
fetch-depth: 0
2124
persist-credentials: false
25+
ref: ${{ github.event.release.tag_name }}
2226

2327
- name: Setup Node
2428
uses: actions/setup-node@v4
@@ -39,16 +43,15 @@ jobs:
3943
# fail if publishConfig.registry set
4044
node -e "const p=require('./package.json'); if(p.publishConfig?.registry){console.error('publishConfig.registry present — refuse to publish'); process.exit(1)}"
4145
# optional: block workflow/script changes in the release commit
42-
# git diff --name-only HEAD~1..HEAD | grep -E '^\.github/(workflows|scripts)/' && { echo 'Workflow/scripts changed in release commit — refuse.'; exit 1; } || true
43-
SHA=$(git rev-list -n 1 "$GITHUB_REF_NAME")
46+
SHA=$(git rev-list -n 1 "$RELEASE_TAG")
4447
PARENT=$(git rev-list -n 1 "$SHA^")
45-
git diff --name-only "$PARENT" "$SHA" | grep -E '^\.github/(workflows|scripts)/' \
48+
git diff --name-only "$PARENT" "$SHA" | grep -E '^\\.github/(workflows|scripts)/' \
4649
&& { echo 'Workflow/scripts changed in release commit — refuse.'; exit 1; } || true
4750
4851
- name: Verify tag matches package version
4952
run: |
5053
PKG_VERSION=$(node -p "require('./package.json').version")
51-
TAG="${GITHUB_REF_NAME#v}"
54+
TAG="${RELEASE_TAG#v}"
5255
[[ "$PKG_VERSION" == "$TAG" ]] || { echo "Tag v$TAG != package.json $PKG_VERSION"; exit 1; }
5356
5457
- name: Install deps (no lifecycle scripts)
@@ -57,7 +60,17 @@ jobs:
5760
- run: npm run clean
5861
- run: npm run build
5962

63+
- name: Resolve dist-tag
64+
id: dist
65+
run: |
66+
if [ "$IS_PRERELEASE" = "true" ]; then
67+
echo "tag=beta" >> "$GITHUB_OUTPUT"
68+
else
69+
echo "tag=latest" >> "$GITHUB_OUTPUT"
70+
fi
71+
6072
- name: Publish
6173
env:
6274
NPM_CONFIG_PROVENANCE: true
63-
run: npm publish --access public --ignore-scripts --registry=https://registry.npmjs.org/ --provenance
75+
DIST_TAG: ${{ steps.dist.outputs.tag }}
76+
run: npm publish --access public --ignore-scripts --registry=https://registry.npmjs.org/ --provenance --tag "$DIST_TAG"

0 commit comments

Comments
 (0)