Skip to content

Commit 99a43ac

Browse files
committed
chore: 增加自动发布配置
1 parent a5d6f9e commit 99a43ac

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Release & Publish
2+
3+
on:
4+
# 触发条件1:当你推送 v 开头的 tag 时 (如 v0.8.0, v0.8.1-alpha.0)
5+
push:
6+
tags:
7+
- 'v*'
8+
# 触发条件2:允许在 Actions 页面手动点击运行
9+
workflow_dispatch:
10+
11+
permissions:
12+
contents: write # 允许创建 GitHub Release
13+
id-token: write # 允许与 npm 进行 OIDC 认证 (必须)
14+
15+
jobs:
16+
release:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Install pnpm
23+
uses: pnpm/action-setup@v2
24+
with:
25+
version: 10
26+
27+
- name: Setup Node.js
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: '20'
31+
# 设置 registry,但不配置 token,走 provenance
32+
registry-url: 'https://registry.npmjs.org'
33+
34+
- name: Install Dependencies
35+
run: pnpm install --frozen-lockfile
36+
37+
- name: Build
38+
run: pnpm run build
39+
40+
# 核心逻辑:解析 Tag 名称并决定发布类型
41+
- name: Parse Version and Tag
42+
id: meta
43+
run: |
44+
# 获取当前的 ref 名称,例如 v0.8.1-alpha.0
45+
REF_NAME=${{ github.ref_name }}
46+
echo "Current Ref: $REF_NAME"
47+
48+
# 去掉 'v' 前缀,得到 0.8.1-alpha.0
49+
VERSION=${REF_NAME#v}
50+
echo "clean_version=$VERSION" >> $GITHUB_OUTPUT
51+
52+
# 判断 tag 策略
53+
if [[ "$VERSION" == *"-"* ]]; then
54+
# 如果版本号带横杠 (如 -alpha.0),提取横杠后的第一个单词作为 tag (如 alpha)
55+
# 逻辑:取第一个 - 后面的内容,再取第一个 . 前面的内容
56+
NPM_TAG=$(echo "$VERSION" | cut -d'-' -f2 | cut -d'.' -f1)
57+
echo "Detected pre-release version. Setting npm tag to: $NPM_TAG"
58+
echo "npm_tag=$NPM_TAG" >> $GITHUB_OUTPUT
59+
else
60+
# 否则就是正式版
61+
echo "Detected stable version. Setting npm tag to: latest"
62+
echo "npm_tag=latest" >> $GITHUB_OUTPUT
63+
fi
64+
65+
# 步骤 2:在 GitHub 右侧创建 Release
66+
- name: Create GitHub Release
67+
uses: softprops/action-gh-release@v1
68+
with:
69+
tag_name: ${{ github.ref_name }}
70+
name: Release ${{ github.ref_name }}
71+
generate_release_notes: true # 自动根据 commit 生成简单的更新日志
72+
draft: false
73+
prerelease: ${{ contains(github.ref_name, '-') }} # 如果带横杠,自动标记为 Pre-release
74+
75+
# 步骤 1 & 3 & 4 & 5:发布到 npm
76+
- name: Publish to npm
77+
run: |
78+
# 确保 package.json 的版本号与 git tag 一致 (防止忘记改文件)
79+
npm version ${{ steps.meta.outputs.clean_version }} --no-git-tag-version
80+
81+
# 使用 pnpm 发布
82+
# --provenance: 启用可信发布 (无需 token)
83+
# --no-git-checks: 因为是 CI 环境,跳过 git 检查
84+
# --tag: 使用上面计算出的 tag (latest 或 alpha/beta 等)
85+
pnpm publish --provenance --access public --no-git-checks --tag ${{ steps.meta.outputs.npm_tag }}
86+
env:
87+
# 注意:使用 Provenance 模式发布时,通常不需要 NODE_AUTH_TOKEN
88+
# 但为了兼容性,如果 setup-node 生成了 .npmrc 引用该变量,保持其为空或利用 OIDC 机制
89+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # 这里即使为空也没关系,因为我们要用 provenance

0 commit comments

Comments
 (0)