Skip to content

Commit 3efa546

Browse files
王璨claude
andcommitted
ci: publish npm from version tags
Trigger npm publishing from pushed v* tags, derive package.json version from the tag, and fail early if that version already exists on npm. This keeps tag-driven releases aligned with the published package version. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 9682132 commit 3efa546

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

.github/workflows/publish.yml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Publish to npm
22

33
on:
4-
release:
5-
types: [created]
6-
workflow_dispatch:
4+
push:
5+
tags:
6+
- "v*"
77

88
jobs:
99
build:
@@ -16,6 +16,26 @@ jobs:
1616
registry-url: "https://registry.npmjs.org"
1717
cache: "npm"
1818
- run: npm ci
19+
- name: Derive version from tag
20+
run: |
21+
VERSION="${GITHUB_REF_NAME#v}"
22+
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
23+
echo "Invalid tag version: $GITHUB_REF_NAME"
24+
exit 1
25+
fi
26+
VERSION="$VERSION" node --eval '
27+
const fs = require("fs");
28+
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
29+
pkg.version = process.env.VERSION;
30+
fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2) + "\n");
31+
'
32+
- name: Ensure npm version is unpublished
33+
run: |
34+
VERSION="${GITHUB_REF_NAME#v}"
35+
if npm view "@wangcan26/dscode@$VERSION" version >/dev/null 2>&1; then
36+
echo "Version $VERSION already exists on npm"
37+
exit 1
38+
fi
1939
- run: node scripts/build.mjs
2040
- uses: actions/upload-artifact@v4
2141
with:
@@ -36,4 +56,4 @@ jobs:
3656
registry-url: "https://registry.npmjs.org"
3757
- run: cd dist-standalone && npm publish --access public
3858
env:
39-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
59+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)