1- name : Publish Package
1+ name : Publish and Release
22
33on :
44 push :
55 tags :
66 - ' *.*.*'
77
8- # OIDC Trusted Publishing 需要以下权限
9- # - id-token: write - 允许 GitHub Actions 生成 OIDC token
10- # - contents: read - 允许读取仓库内容
8+ concurrency :
9+ group : publish-${{ github.ref }}
10+ cancel-in-progress : false
11+
12+ # npm Trusted Publishing 需要 id-token: write;创建 GitHub Release 需要
13+ # contents: write。
1114permissions :
15+ contents : write
1216 id-token : write
13- contents : read
1417
1518jobs :
1619 publish :
20+ name : Publish npm package and create GitHub Release
1721 runs-on : ubuntu-latest
22+ timeout-minutes : 30
1823
1924 steps :
20- - name : Checkout
25+ - name : Checkout repository
2126 uses : actions/checkout@v4
27+ with :
28+ fetch-depth : 0
2229
2330 - name : Setup pnpm
2431 uses : pnpm/action-setup@v4
@@ -30,17 +37,33 @@ jobs:
3037 with :
3138 node-version : ' 24'
3239 registry-url : ' https://registry.npmjs.org'
40+ cache : pnpm
3341
3442 - name : Install dependencies
35- run : pnpm install
43+ run : pnpm install --frozen-lockfile
3644
37- - name : Run build
45+ - name : Build
3846 run : pnpm build
3947
40- - name : Run tests
48+ - name : Test
4149 run : pnpm test
4250
43- # 使用 OIDC Trusted Publishing 发布,无需 token
44- # 在 npmjs.com 上配置 Trusted Publisher 后,npm 会自动使用 OIDC 认证
51+ - name : Create package tarball
52+ id : pack
53+ shell : bash
54+ run : |
55+ package_file="$(npm pack --json | node -e "let input=''; process.stdin.on('data', chunk => input += chunk); process.stdin.on('end', () => console.log(JSON.parse(input)[0].filename));")"
56+ echo "file=$package_file" >> "$GITHUB_OUTPUT"
57+
58+ # 在 npmjs.com 为该仓库和 workflow 配置 Trusted Publisher 后,
59+ # npm 会通过 GitHub OIDC 完成认证,无需 NPM_TOKEN。
4560 - name : Publish to npm
46- run : npm publish --access public
61+ run : npm publish "${{ steps.pack.outputs.file }}" --access public --provenance
62+
63+ - name : Create GitHub Release
64+ uses : softprops/action-gh-release@v2
65+ with :
66+ name : ${{ github.ref_name }}
67+ files : ${{ steps.pack.outputs.file }}
68+ generate_release_notes : true
69+ fail_on_unmatched_files : true
0 commit comments