Skip to content

Commit 0558f5f

Browse files
committed
feat(ci): add auto release ci
Log:
1 parent 50d2b7b commit 0558f5f

1 file changed

Lines changed: 230 additions & 0 deletions

File tree

.github/workflows/auto-release.yml

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
name: Auto Release
2+
on:
3+
workflow_call:
4+
inputs:
5+
version:
6+
description: 'Release version (e.g., 1.0.0)'
7+
required: true
8+
type: string
9+
name:
10+
description: 'The name of the person to release the version'
11+
required: false
12+
type: string
13+
email:
14+
description: 'The email of the person to release the version'
15+
required: false
16+
type: string
17+
18+
jobs:
19+
# 生成变更日志
20+
create_changelog:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: 验证语义化版本号
24+
id: validate_version
25+
run: |
26+
version="${{ github.event.inputs.version }}"
27+
if [[ ! "$version" =~ ^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]]; then
28+
echo "无效的版本号格式,请使用语义化版本号: $version" >&2
29+
exit 1
30+
fi
31+
version=$(echo "$version" | sed 's/^v//')
32+
echo "version=${version}" >> $GITHUB_OUTPUT
33+
34+
- name: 使用 GitHub App 进行身份验证
35+
id: auth
36+
uses: actions/create-github-app-token@v1
37+
with:
38+
app-id: ${{ secrets.APP_ID}}
39+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
40+
owner: ${{ github.repository_owner }}
41+
42+
- name: 获取触发者的 GitHub 邮箱
43+
id: get_email
44+
uses: evvanErb/get-github-email-by-username-action@v1.25
45+
with:
46+
github-username: ${{ github.actor }}
47+
token: ${{ steps.auth.outputs.token }}
48+
49+
- name: 检查并设置用户信息
50+
id: setup_user_info
51+
run: |
52+
# 检查是否提供了用户名和邮箱
53+
input_name="${{ github.event.inputs.name }}"
54+
input_email="${{ github.event.inputs.email }}"
55+
56+
# 设置用户名
57+
if [ -z "$input_name" ]; then
58+
author_name="${{ github.actor }}"
59+
echo "未提供用户名,使用 GitHub 用户名: $author_name"
60+
else
61+
author_name="$input_name"
62+
echo "使用提供的用户名: $author_name"
63+
fi
64+
65+
# 设置邮箱
66+
if [ -z "$input_email" ]; then
67+
# 尝试从 GitHub API 获取邮箱
68+
github_email="${{ steps.get_email.outputs.email }}"
69+
if [ -n "$github_email" ] && [ "$github_email" != "null" ] && [ "$github_email" != "" ]; then
70+
author_email="$github_email"
71+
echo "从 GitHub API 获取到邮箱: $author_email"
72+
else
73+
echo "无法获取 GitHub 邮箱!"
74+
exit 1
75+
fi
76+
else
77+
author_email="$input_email"
78+
echo "使用提供的邮箱: $author_email"
79+
fi
80+
81+
# 输出到 GITHUB_OUTPUT
82+
echo "author_name=${author_name}" >> $GITHUB_OUTPUT
83+
echo "author_email=${author_email}" >> $GITHUB_OUTPUT
84+
85+
echo "最终用户信息: $author_name <$author_email>"
86+
87+
- name: 检出代码
88+
uses: actions/checkout@v4
89+
with:
90+
fetch-depth: 0
91+
token: ${{ steps.auth.outputs.token }}
92+
93+
- name: Install git-cliff from crates.io
94+
uses: baptiste0928/cargo-install@v3
95+
with:
96+
crate: git-cliff
97+
98+
- name: 生成变更日志
99+
run: |
100+
101+
# 获取第一条提交的hash和消息
102+
release_commit_hash=$(git rev-parse HEAD)
103+
version=${{ steps.validate_version.outputs.version }}
104+
echo "提交hash: $release_commit_hash"
105+
106+
# 获取发布者名称和邮箱
107+
author_name="${{ steps.setup_user_info.outputs.author_name }}"
108+
author_email="${{ steps.setup_user_info.outputs.author_email }}"
109+
110+
# 获取仓库名 (GitHub Actions 环境)
111+
if [ -n "$GITHUB_REPOSITORY" ]; then
112+
repo_name=$(basename "$GITHUB_REPOSITORY")
113+
else
114+
# 本地环境回退方案
115+
repo_name=$(basename $(git rev-parse --show-toplevel))
116+
fi
117+
118+
# 配置git
119+
git config --global user.name "github-actions[bot]"
120+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
121+
122+
echo "正在更新源代码信息……"
123+
find . -maxdepth 1 -name "*.in" -type f | while read -r file; do
124+
if [ -f "$file" ]; then
125+
echo "处理文件: $file"
126+
# 替换 @version@ 为实际版本号
127+
sed "s/@version@/${version}/g" "$file" > "${file%.in}"
128+
echo "生成文件: ${file%.in}"
129+
fi
130+
done
131+
132+
git add .
133+
git commit -m "chore: release ${version}"
134+
git show -s
135+
136+
echo "正在生成 Changelog.md ……"
137+
if [ -f "Changelog.md" ]; then
138+
git cliff --use-branch-tags --unreleased --tag ${version} \
139+
--github-repo "${GITHUB_REPOSITORY}" \
140+
--github-token "${{ secrets.GITHUB_TOKEN }}" \
141+
--skip-commit ${release_commit_hash} \
142+
--config keepachangelog \
143+
--prepend "Changelog.md"
144+
else
145+
git cliff --use-branch-tags --unreleased --tag ${version} \
146+
--github-repo "${GITHUB_REPOSITORY}" \
147+
--github-token "${{ secrets.GITHUB_TOKEN }}" \
148+
--skip-commit ${release_commit_hash} \
149+
--config keepachangelog \
150+
-o "Changelog.md"
151+
fi
152+
153+
git add .
154+
git commit -m "docs: Update Changelog.md"
155+
git show -s
156+
157+
echo -e "\n"
158+
159+
if [ -d "debian" ]; then
160+
echo "为Debian生成版本更新……"
161+
162+
# 获取当前日期时间
163+
current_date=$(date -R)
164+
165+
# 创建新的changelog条目
166+
new_entry="${repo_name} (${version}) unstable; urgency=medium
167+
168+
* Release ${version}
169+
170+
-- ${author_name} <${author_email}> ${current_date}
171+
"
172+
173+
# 将新条目添加到changelog开头
174+
echo "${new_entry}" > debian/changelog.tmp
175+
cat debian/changelog >> debian/changelog.tmp
176+
mv debian/changelog.tmp debian/changelog
177+
178+
git add .
179+
git commit -m "chore(debian): New release ${version}"
180+
git show -s
181+
182+
echo -e "\n"
183+
fi
184+
185+
if [ -f "archlinux/PKGBUILD" ]; then
186+
echo "正在更新 PKGBUILD 版本号……"
187+
188+
# 更新版本号
189+
sed -i "s/^pkgver=.*/pkgver=${version}/" archlinux/PKGBUILD
190+
191+
# 重置 pkgrel 为 1
192+
sed -i "s/^pkgrel=.*/pkgrel=1/" archlinux/PKGBUILD
193+
194+
git add .
195+
git commit -m "chore(archlinux): New release ${version}"
196+
git show -s
197+
198+
echo -e "\n"
199+
fi
200+
201+
if [ -f "rpm/${repo_name}.spec" ]; then
202+
echo "正在更新 RPM spec 文件版本号……"
203+
204+
# 更新版本号
205+
sed -i "s/^Version:.*/Version: ${version}/" rpm/${repo_name}.spec
206+
207+
# 重置 Release 为 1
208+
sed -i "s/^Release:.*/Release: 1/" rpm/${repo_name}.spec
209+
210+
git add .
211+
git commit -m "chore(rpm): New release ${version}"
212+
git show -s
213+
214+
echo -e "\n"
215+
fi
216+
217+
- name: 创建PR
218+
id: cpr
219+
uses: peter-evans/create-pull-request@v7
220+
with:
221+
token: ${{ steps.auth.outputs.token }}
222+
title: "Release ${{ steps.validate_version.outputs.version }}"
223+
body-path: "Changelog.md"
224+
branch: "release-${{ steps.validate_version.outputs.version }}"
225+
226+
- name: Check outputs
227+
if: ${{ steps.cpr.outputs.pull-request-number }}
228+
run: |
229+
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
230+
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"

0 commit comments

Comments
 (0)