Skip to content

Commit f75ebcf

Browse files
Cryolitiazccrs
authored andcommitted
feat: enhance auto release
Log:
1 parent faab85c commit f75ebcf

1 file changed

Lines changed: 85 additions & 52 deletions

File tree

.github/workflows/auto-release.yml

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ on:
33
workflow_call:
44
inputs:
55
version:
6-
description: 'Release version (e.g., 1.0.0)'
7-
required: true
6+
description: 'Release version (e.g., 1.0.0), keep empty to auto-increment from VERSION file'
7+
required: false
88
type: string
99
name:
1010
description: 'The name of the person to release the version'
@@ -16,7 +16,7 @@ on:
1616
type: string
1717
timezone:
1818
description: 'The timezone in the debian changelog file'
19-
required: false
19+
required: true
2020
type: string
2121
default: 'Asia/Shanghai'
2222

@@ -25,17 +25,6 @@ jobs:
2525
create_changelog:
2626
runs-on: ubuntu-latest
2727
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-
3928
- name: 使用 GitHub App 进行身份验证
4029
id: auth
4130
uses: actions/create-github-app-token@v1
@@ -44,7 +33,56 @@ jobs:
4433
private-key: ${{ secrets.APP_PRIVATE_KEY }}
4534
owner: ${{ github.repository_owner }}
4635

47-
- name: 获取触发者的昵称
36+
- name: 检出代码
37+
uses: actions/checkout@v4
38+
with:
39+
fetch-depth: 0
40+
token: ${{ steps.auth.outputs.token }}
41+
42+
- name: 验证语义化版本号
43+
id: validate_version
44+
run: |
45+
version="${{ github.event.inputs.version }}"
46+
47+
# 如果没有提供版本号,尝试从 VERSION 文件读取并自动递增
48+
if [ -z "$version" ]; then
49+
if [ -f "VERSION" ]; then
50+
current_version=$(cat VERSION | tr -d '\n\r')
51+
echo "从 VERSION 文件读取到版本号: $current_version"
52+
53+
# 验证现有版本号格式
54+
if [[ ! "$current_version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then
55+
echo "VERSION 文件中的版本号无法自动递增,请手动输入: $current_version" >&2
56+
exit 1
57+
fi
58+
59+
# 移除 v 前缀并分割版本号
60+
IFS='.' read -ra version_parts <<< "${current_version%%-*}" # 移除预发布标识
61+
major=${version_parts[0]}
62+
minor=${version_parts[1]}
63+
patch=${version_parts[2]}
64+
65+
# 递增补丁版本号
66+
patch=$((patch + 1))
67+
version="${major}.${minor}.${patch}"
68+
echo "自动递增版本号为: $version"
69+
else
70+
echo "未提供版本号且未找到 VERSION 文件" >&2
71+
exit 1
72+
fi
73+
else
74+
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
75+
echo "无效的版本号格式,请使用语义化版本号: $version" >&2
76+
exit 1
77+
fi
78+
fi
79+
80+
echo "新版本号: $version"
81+
version=${version#v}
82+
83+
echo "version=${version}" >> $GITHUB_OUTPUT
84+
85+
- name: 获取 actor 的昵称
4886
id: actor_name
4987
uses: actions/github-script@v7
5088
with:
@@ -57,6 +95,7 @@ jobs:
5795
- name: 获取触发者的 GitHub 邮箱
5896
id: get_email
5997
uses: evvanErb/get-github-email-by-username-action@v1.25
98+
continue-on-error: true
6099
with:
61100
github-username: ${{ github.actor }}
62101
token: ${{ steps.auth.outputs.token }}
@@ -71,6 +110,9 @@ jobs:
71110
# 设置用户名
72111
if [ -z "$input_name" ]; then
73112
author_name="${{ steps.actor_name.outputs.name }}"
113+
if [ -z "$author_name" ]; then
114+
author_name="${{ github.actor }}"
115+
fi
74116
echo "未提供用户名,使用 GitHub 用户名: $author_name"
75117
else
76118
author_name="$input_name"
@@ -99,12 +141,6 @@ jobs:
99141
100142
echo "最终用户信息: $author_name <$author_email>"
101143
102-
- name: 检出代码
103-
uses: actions/checkout@v4
104-
with:
105-
fetch-depth: 0
106-
token: ${{ steps.auth.outputs.token }}
107-
108144
- name: Install git-cliff from crates.io
109145
uses: baptiste0928/cargo-install@v3
110146
with:
@@ -132,14 +168,7 @@ jobs:
132168
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
133169
134170
echo "正在更新源代码信息……"
135-
find . -maxdepth 1 -name "*.in" -type f | while read -r file; do
136-
if [ -f "$file" ]; then
137-
echo "处理文件: $file"
138-
# 替换 @version@ 为实际版本号
139-
sed "s/@version@/${version}/g" "$file" > "${file%.in}"
140-
echo "生成文件: ${file%.in}"
141-
fi
142-
done
171+
echo ${version} > VERSION
143172
144173
echo "正在生成 CHANGELOG.md ……"
145174
if [ -f "CHANGELOG.md" ]; then
@@ -165,21 +194,12 @@ jobs:
165194
if [ -d "debian" ]; then
166195
echo "为Debian生成版本更新……"
167196
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-
"
197+
sudo apt-get update && sudo apt-get install -y devscripts
198+
export TZ="${{ github.event.inputs.timezone }}"
199+
export DEBFULLNAME="${author_name}"
200+
export DEBEMAIL="${author_email}"
178201
179-
# 将新条目添加到changelog开头
180-
echo "${new_entry}" > debian/changelog.tmp
181-
cat debian/changelog >> debian/changelog.tmp
182-
mv debian/changelog.tmp debian/changelog
202+
dch --newversion "${version}" --distribution unstable "Release ${version}"
183203
fi
184204
185205
if [ -f "archlinux/PKGBUILD" ]; then
@@ -192,18 +212,31 @@ jobs:
192212
sed -i "s/^pkgrel=.*/pkgrel=1/" archlinux/PKGBUILD
193213
fi
194214
195-
if [ -f "rpm/${repo_name}.spec" ]; then
196-
echo "正在更新 RPM spec 文件版本号……"
197-
198-
# 更新版本号
199-
sed -i "s/^Version:.*/Version: ${version}/" rpm/"${repo_name}".spec
200-
201-
# 重置 Release 为 1
202-
sed -i "s/^Release:.*/Release: 1/" rpm/"${repo_name}".spec
215+
if [ -d "rpm" ]; then
216+
echo "正在更新 RPM 版本号……"
217+
pushd rpm
218+
find . -maxdepth 1 -name "*.spec" -type f | while read -r file; do
219+
if [ -f "$file" ]; then
220+
echo "处理文件: $file"
221+
sed -i -E "s/^(Version:[[:space:]]*)[0-9].*/\1${version}/" "$file"
222+
echo "生成文件: ${file}"
223+
fi
224+
done
225+
popd
203226
fi
227+
228+
if [ -f "version.sh" ]; then
229+
echo "发现自定义 version.sh 文件,正在执行……"
230+
chmod +x version.sh
231+
./version.sh ${version}
232+
fi
233+
234+
echo "
235+
---
236+
This PR is triggered by @${{ github.actor }}" >> ../CHANGELOG.md
204237
205238
git add .
206-
git commit -m "chore(rpm): New release ${version}
239+
git commit -m "chore: New release ${version}
207240
208241
Log:"
209242
git show -s

0 commit comments

Comments
 (0)