Skip to content

Commit ef3de5d

Browse files
Cryolitiazccrs
authored andcommitted
feat(ci): add auto release ci
Log:
1 parent 50d2b7b commit ef3de5d

1 file changed

Lines changed: 236 additions & 0 deletions

File tree

.github/workflows/auto-release.yml

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

0 commit comments

Comments
 (0)