Skip to content

Commit d4efe6c

Browse files
author
weilei
committed
ci(workflow): 添加自动发布工作流
1 parent 429031f commit d4efe6c

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

.github/workflows/release.yml

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
name: Auto Release on Version Change
2+
3+
on:
4+
push:
5+
branches: [master]
6+
tags-ignore:
7+
- '**'
8+
9+
jobs:
10+
check-version-and-release:
11+
name: 检查版本变化并发布
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Configure Git
22+
run: |
23+
git config --global user.name "GitHub Actions"
24+
git config --global user.email "github-actions@github.com"
25+
26+
- name: Setup Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: '16'
30+
31+
- name: Get current version from package.json
32+
id: current-version
33+
run: |
34+
current_version=$(jq -r '.version' package.json)
35+
echo "version=$current_version" >> $GITHUB_OUTPUT
36+
echo "🔍 当前 package.json 版本: $current_version"
37+
38+
- name: Get latest tag version
39+
id: latest-tag
40+
run: |
41+
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null | sed 's/^v//' || echo "0.0.0")
42+
echo "version=$latest_tag" >> $GITHUB_OUTPUT
43+
echo "📋 仓库最新标签版本: $latest_tag"
44+
45+
- name: Compare versions
46+
id: version-check
47+
run: |
48+
current="${{ steps.current-version.outputs.version }}"
49+
latest="${{ steps.latest-tag.outputs.version }}"
50+
51+
echo "当前版本: $current"
52+
echo "最新标签: $latest"
53+
54+
if [ "$current" != "$latest" ]; then
55+
echo "changed=true" >> $GITHUB_OUTPUT
56+
echo "✅ 版本已发生变化,将创建新的发布"
57+
echo "new_tag=v$current" >> $GITHUB_OUTPUT
58+
else
59+
echo "changed=false" >> $GITHUB_OUTPUT
60+
echo "ℹ️ 版本未发生变化,跳过发布流程"
61+
fi
62+
63+
- name: Generate changelog
64+
if: steps.version-check.outputs.changed == 'true'
65+
id: changelog
66+
run: |
67+
npm install -g conventional-changelog-cli
68+
69+
if [ "${{ steps.latest-tag.outputs.version }}" == "0.0.0" ]; then
70+
changelog=$(conventional-changelog -p angular)
71+
else
72+
changelog=$(conventional-changelog -p angular --from v${{ steps.latest-tag.outputs.version }})
73+
fi
74+
75+
if [ -z "$changelog" ]; then
76+
cat > changelog.md << 'EOF'
77+
### 🚀 版本 ${{ steps.current-version.outputs.version }}
78+
79+
- 版本更新至 ${{ steps.current-version.outputs.version }}
80+
- 详细更改请查看提交历史
81+
EOF
82+
else
83+
echo "$changelog" > changelog.md
84+
fi
85+
86+
echo "generated=true" >> $GITHUB_OUTPUT
87+
echo "📝 Changelog 已生成"
88+
89+
- name: Create Git tag
90+
if: steps.version-check.outputs.changed == 'true'
91+
run: |
92+
tag_name="${{ steps.version-check.outputs.new_tag }}"
93+
echo "🏷️ 创建标签: $tag_name"
94+
95+
git tag -a "$tag_name" -m "Release $tag_name"
96+
git push origin "$tag_name"
97+
98+
echo "✅ 标签 $tag_name 已创建并推送"
99+
100+
- name: Create GitHub Release
101+
if: steps.version-check.outputs.changed == 'true'
102+
uses: actions/create-release@v1
103+
env:
104+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
with:
106+
tag_name: ${{ steps.version-check.outputs.new_tag }}
107+
release_name: Release ${{ steps.version-check.outputs.new_tag }}
108+
body_path: changelog.md
109+
draft: false
110+
prerelease: false
111+
112+
- name: Install root dependencies
113+
if: steps.version-check.outputs.changed == 'true'
114+
run: |
115+
echo "📦 安装根目录依赖..."
116+
npm install
117+
118+
- name: Install package dependencies
119+
if: steps.version-check.outputs.changed == 'true'
120+
working-directory: packages/element-ui-x
121+
run: |
122+
echo "📦 安装组件库依赖..."
123+
npm install
124+
125+
- name: Build component library
126+
if: steps.version-check.outputs.changed == 'true'
127+
working-directory: packages/element-ui-x
128+
run: |
129+
echo "🔨 构建组件库..."
130+
npm run build
131+
132+
- name: Configure npm authentication
133+
if: steps.version-check.outputs.changed == 'true'
134+
working-directory: packages/element-ui-x
135+
run: |
136+
echo "🔑 配置 npm 认证..."
137+
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
138+
env:
139+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
140+
141+
- name: Publish to npm
142+
if: steps.version-check.outputs.changed == 'true'
143+
id: npm-publish
144+
working-directory: packages/element-ui-x
145+
run: |
146+
echo "🚀 发布到 npm..."
147+
npm publish --access public
148+
echo "success=true" >> $GITHUB_OUTPUT
149+
echo "✅ 包已成功发布到 npm"
150+
env:
151+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
152+
continue-on-error: true
153+
154+
- name: Output npm publish status
155+
if: steps.version-check.outputs.changed == 'true'
156+
run: |
157+
if [ "${{ steps.npm-publish.outputs.success }}" == "true" ]; then
158+
echo "✅ npm 发布成功!"
159+
echo "包名: vue-element-ui-x"
160+
echo "版本: ${{ steps.current-version.outputs.version }}"
161+
echo "npm 地址: https://www.npmjs.com/package/vue-element-ui-x"
162+
else
163+
echo "❌ npm 发布失败"
164+
echo "请检查 NPM_TOKEN 配置或手动发布"
165+
fi
166+
167+
- name: Output release info
168+
if: steps.version-check.outputs.changed == 'true'
169+
run: |
170+
echo "🎉 发布完成!"
171+
echo "版本: ${{ steps.current-version.outputs.version }}"
172+
echo "标签: ${{ steps.version-check.outputs.new_tag }}"
173+
echo "Release 地址: https://github.com/${{ github.repository }}/releases/tag/${{ steps.version-check.outputs.new_tag }}"
174+
175+
- name: Summary
176+
run: |
177+
if [ "${{ steps.version-check.outputs.changed }}" == "true" ]; then
178+
echo "✅ 自动发布流程已完成" >> $GITHUB_STEP_SUMMARY
179+
echo "- 新版本: ${{ steps.current-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
180+
echo "- 标签: ${{ steps.version-check.outputs.new_tag }}" >> $GITHUB_STEP_SUMMARY
181+
echo "- [查看 Release](https://github.com/${{ github.repository }}/releases/tag/${{ steps.version-check.outputs.new_tag }})" >> $GITHUB_STEP_SUMMARY
182+
183+
if [ "${{ steps.npm-publish.outputs.success }}" == "true" ]; then
184+
echo "- ✅ [npm 包已发布](https://www.npmjs.com/package/vue-element-ui-x)" >> $GITHUB_STEP_SUMMARY
185+
else
186+
echo "- ❌ npm 发布失败,请手动检查" >> $GITHUB_STEP_SUMMARY
187+
fi
188+
else
189+
echo "ℹ️ 版本未变化,未执行发布操作" >> $GITHUB_STEP_SUMMARY
190+
echo "- 当前版本: ${{ steps.current-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
191+
fi

0 commit comments

Comments
 (0)