ci: publish releases to npm through trusted publishing

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
大阔andClaude Opus 5.5 committed 2026-10-15 02:33:48 +08:00
1 parent 89fd2b8107
commit a92927d173
2 files changed
+46 -6

No files matched your search

+30
View File
@@ -7,6 +7,8 @@ on:
permissions:
contents: write
# npm trusted publishing (OIDC): no npm token is stored anywhere.
id-token: write
jobs:
release:
@@ -22,6 +24,22 @@ jobs:
with:
node-version: 22.20.0
cache: pnpm
- name: Require an npm CLI that supports trusted publishing
run: |
node <<'EOF'
const { execSync } = require('node:child_process')
const version = () => execSync('npm --version', { encoding: 'utf8' }).trim()
const supports = value => {
const [major, minor, patch] = value.split('.').map(Number)
return major > 11 || (major === 11 && (minor > 5 || (minor === 5 && patch >= 1)))
}
if (!supports(version())) execSync('npm install -g npm@11', { stdio: 'inherit' })
if (!supports(version())) {
console.error(`npm ${version()} cannot publish with a trusted publisher`)
process.exit(1)
}
console.log(`npm ${version()}`)
EOF
- name: Install locked dependencies
run: pnpm install --frozen-lockfile
- name: Refuse a tag that does not match package.json
@@ -54,3 +72,15 @@ jobs:
else
gh release create "$GITHUB_REF_NAME" "$ASSET" .artifacts/dsh-geek2api.tgz --title "dsh-geek2api v${VERSION}" --notes "$NOTES"
fi
- name: Publish the same tarball to npm
# Trusted publishing: npm exchanges this job's OIDC token, so no
# NODE_AUTH_TOKEN and no setup-node registry-url (either would switch
# npm to token auth and skip OIDC). Re-running a tag that is already
# on npm is a no-op.
run: |
VERSION="${GITHUB_REF_NAME#v}"
if npm view "dsh-geek2api@${VERSION}" version --registry=https://registry.npmjs.org >/dev/null 2>&1; then
echo "dsh-geek2api@${VERSION} is already on npm"
else
npm publish ".artifacts/dsh-geek2api-${VERSION}.tgz" --access public --registry=https://registry.npmjs.org
fi