Skip to content

Commit a453baa

Browse files
committed
fix: auto update npm package version
1 parent 66aded8 commit a453baa

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

.github/workflows/publish-contract.yml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ name: Publish API Contract to GitHub Packages
44
on:
55
push:
66
branches:
7-
- main # 每当 main 分支有更新时触发
7+
- main
88

99
jobs:
1010
publish:
1111
runs-on: ubuntu-latest
1212
permissions:
13-
contents: read
13+
contents: write
1414
packages: write
1515
steps:
1616
- uses: actions/checkout@v4
17+
with:
18+
token: ${{ secrets.GITHUB_TOKEN }}
19+
1720
- uses: actions/setup-node@v4
1821
with:
1922
node-version: '20'
2023
registry-url: 'https://npm.pkg.github.com'
21-
scope: '@tobenot' # 明确指定 scope
24+
scope: '@tobenot'
2225

2326
- name: Install dependencies and build
2427
run: |
@@ -28,7 +31,17 @@ jobs:
2831
env:
2932
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3033

31-
- name: Publish to GitHub Packages
32-
run: npm publish
34+
- name: Update version and publish
35+
run: |
36+
npm version patch --no-git-tag-version
37+
npm publish
3338
env:
3439
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
41+
- name: Commit and push version update
42+
run: |
43+
git config --local user.email "action@github.com"
44+
git config --local user.name "GitHub Action"
45+
git add package.json
46+
git commit -m "Auto update version for npm publish"
47+
git push

docx/tutorial/50_type_sync_npm.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,44 @@ jobs:
9898
publish:
9999
runs-on: ubuntu-latest
100100
permissions:
101-
contents: read
101+
contents: write
102102
packages: write
103103
steps:
104104
- uses: actions/checkout@v4
105+
with:
106+
token: ${{ secrets.GITHUB_TOKEN }}
105107
- uses: actions/setup-node@v4
106108
with:
107-
node-version: '18'
109+
node-version: '20'
108110
registry-url: 'https://npm.pkg.github.com'
109111
scope: '@yourusername' # 替换为您的GitHub用户名
110-
- run: npm ci && npm run build
112+
- name: Install dependencies and build
113+
run: |
114+
rm -rf package-lock.json node_modules
115+
npm install
116+
npm run build
111117
env:
112118
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
113-
- run: npm publish
119+
- name: Update version and publish
120+
run: |
121+
npm version patch --no-git-tag-version
122+
npm publish
114123
env:
115124
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125+
- name: Commit and push version update
126+
run: |
127+
git config --local user.email "action@github.com"
128+
git config --local user.name "GitHub Action"
129+
git add package.json
130+
git commit -m "Auto update version for npm publish"
131+
git push
116132
```
117133
134+
**关键改进:**
135+
- 自动递增补丁版本号(如 1.0.0 → 1.0.1)
136+
- 自动提交版本更新到仓库
137+
- 避免版本冲突错误
138+
118139
---
119140
120141
### **2. 前端仓库配置**
@@ -172,8 +193,13 @@ jobs:
172193
---
173194
174195
## **总结**
175-
1. 后端:每次推送到`main`分支时,自动发布新版本的契约包
196+
1. 后端:每次推送到`main`分支时,自动递增版本号并发布新版本的契约包
176197
2. 前端:通过`.npmrc`和PAT令牌安装最新版本的契约包。
177198
3. 开发:直接导入类型,享受完整的类型安全。
178199

200+
**自动版本管理:**
201+
- 每次推送都会自动递增补丁版本号(1.0.0 → 1.0.1 → 1.0.2...)
202+
- 版本更新会自动提交到仓库
203+
- 避免版本冲突,确保每次都能成功发布
204+
179205
这样,无论您的GitHub用户名是什么,都可以轻松实现前后端类型同步!

0 commit comments

Comments
 (0)