-
Notifications
You must be signed in to change notification settings - Fork 111
257 lines (219 loc) · 9.41 KB
/
Copy pathauto-release.yml
File metadata and controls
257 lines (219 loc) · 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
name: Auto Release
on:
workflow_call:
inputs:
version:
description: 'Release version (e.g., 1.0.0), keep empty to auto-increment from VERSION file'
required: false
type: string
name:
description: 'The name of the person to release the version'
required: false
type: string
email:
description: 'The email of the person to release the version'
required: false
type: string
timezone:
description: 'The timezone in the debian changelog file'
required: true
type: string
default: 'Asia/Shanghai'
jobs:
# 生成变更日志
create_changelog:
runs-on: ubuntu-latest
steps:
- name: 使用 GitHub App 进行身份验证
id: auth
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.APP_ID}}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: 检出代码
uses: actions/checkout@v5
with:
fetch-depth: 0
token: ${{ steps.auth.outputs.token }}
- name: 验证语义化版本号
id: validate_version
run: |
version="${{ github.event.inputs.version }}"
# 如果没有提供版本号,尝试从 VERSION 文件读取并自动递增
if [ -z "$version" ]; then
if [ -f "VERSION" ]; then
current_version=$(cat VERSION | tr -d '\n\r')
echo "从 VERSION 文件读取到版本号: $current_version"
# 验证现有版本号格式
if [[ ! "$current_version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then
echo "VERSION 文件中的版本号无法自动递增,请手动输入: $current_version" >&2
exit 1
fi
# 移除 v 前缀并分割版本号
IFS='.' read -ra version_parts <<< "${current_version%%-*}" # 移除预发布标识
major=${version_parts[0]}
minor=${version_parts[1]}
patch=${version_parts[2]}
# 递增补丁版本号
patch=$((patch + 1))
version="${major}.${minor}.${patch}"
echo "自动递增版本号为: $version"
else
echo "未提供版本号且未找到 VERSION 文件" >&2
exit 1
fi
else
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
echo "无效的版本号格式,请使用语义化版本号: $version" >&2
exit 1
fi
fi
echo "新版本号: $version"
version=${version#v}
echo "version=${version}" >> $GITHUB_OUTPUT
- name: 获取 actor 的昵称
id: actor_name
uses: actions/github-script@v7
with:
script: |
const { data: user } = await github.rest.users.getByUsername({
username: context.actor
});
core.setOutput('name', user.name);
- name: 获取触发者的 GitHub 邮箱
id: get_email
uses: evvanErb/get-github-email-by-username-action@v1.25
continue-on-error: true
with:
github-username: ${{ github.actor }}
token: ${{ steps.auth.outputs.token }}
- name: 检查并设置用户信息
id: setup_user_info
run: |
# 检查是否提供了用户名和邮箱
input_name="${{ github.event.inputs.name }}"
input_email="${{ github.event.inputs.email }}"
# 设置用户名
if [ -z "$input_name" ]; then
author_name="${{ steps.actor_name.outputs.name }}"
if [ -z "$author_name" ]; then
author_name="${{ github.actor }}"
fi
echo "未提供用户名,使用 GitHub 用户名: $author_name"
else
author_name="$input_name"
echo "使用提供的用户名: $author_name"
fi
# 设置邮箱
if [ -z "$input_email" ]; then
# 尝试从 GitHub API 获取邮箱
github_email="${{ steps.get_email.outputs.email }}"
if [ -n "$github_email" ] && [ "$github_email" != "null" ] && [ "$github_email" != "" ]; then
author_email="$github_email"
echo "从 GitHub API 获取到邮箱: $author_email"
else
echo "无法获取 GitHub 邮箱!"
exit 1
fi
else
author_email="$input_email"
echo "使用提供的邮箱: $author_email"
fi
# 输出到 GITHUB_OUTPUT
echo "author_name=${author_name}" >> $GITHUB_OUTPUT
echo "author_email=${author_email}" >> $GITHUB_OUTPUT
echo "最终用户信息: $author_name <$author_email>"
- name: Install git-cliff from crates.io
uses: baptiste0928/cargo-install@v3
with:
crate: git-cliff
- name: 生成变更日志
run: |
version=${{ steps.validate_version.outputs.version }}
# 获取发布者名称和邮箱
author_name="${{ steps.setup_user_info.outputs.author_name }}"
author_email="${{ steps.setup_user_info.outputs.author_email }}"
# 获取仓库名 (GitHub Actions 环境)
if [ -n "$GITHUB_REPOSITORY" ]; then
repo_name=$(basename "$GITHUB_REPOSITORY")
else
# 本地环境回退方案
repo_name=$(basename "$(git rev-parse --show-toplevel)")
fi
# 配置git
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
echo "正在更新源代码信息……"
echo ${version} > VERSION
echo "正在生成 CHANGELOG.md ……"
if [ -f "CHANGELOG.md" ]; then
git cliff --use-branch-tags --unreleased --tag "${version}" \
--github-repo "${GITHUB_REPOSITORY}" \
--github-token "${{ secrets.GITHUB_TOKEN }}" \
--config keepachangelog \
--prepend "CHANGELOG.md"
else
git cliff --use-branch-tags --unreleased --tag "${version}" \
--github-repo "${GITHUB_REPOSITORY}" \
--github-token "${{ secrets.GITHUB_TOKEN }}" \
--config keepachangelog \
-o "CHANGELOG.md"
fi
git cliff --use-branch-tags --unreleased --tag "${version}" \
--github-repo "${GITHUB_REPOSITORY}" \
--github-token "${{ secrets.GITHUB_TOKEN }}" \
--config keepachangelog \
-o "../CHANGELOG.md"
if [ -d "debian" ]; then
echo "为Debian生成版本更新……"
sudo apt-get update && sudo apt-get install -y devscripts
export TZ="${{ github.event.inputs.timezone }}"
export DEBFULLNAME="${author_name}"
export DEBEMAIL="${author_email}"
dch --newversion "${version}" --distribution unstable "Release ${version}"
fi
if [ -f "archlinux/PKGBUILD" ]; then
echo "正在更新 PKGBUILD 版本号……"
# 更新版本号
sed -i "s/^pkgver=.*/pkgver=${version}/" archlinux/PKGBUILD
# 重置 pkgrel 为 1
sed -i "s/^pkgrel=.*/pkgrel=1/" archlinux/PKGBUILD
fi
if [ -d "rpm" ]; then
echo "正在更新 RPM 版本号……"
pushd rpm
find . -maxdepth 1 -name "*.spec" -type f | while read -r file; do
if [ -f "$file" ]; then
echo "处理文件: $file"
sed -i -E "s/^(Version:[[:space:]]*)[0-9].*/\1${version}/" "$file"
echo "生成文件: ${file}"
fi
done
popd
fi
if [ -f "version.sh" ]; then
echo "发现自定义 version.sh 文件,正在执行……"
chmod +x version.sh
./version.sh ${version}
fi
echo "
---
This PR is triggered by @${{ github.actor }}" >> ../CHANGELOG.md
git add .
git commit -m "chore: New release ${version}
Log:"
git show -s
- name: 创建PR
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ steps.auth.outputs.token }}
title: "Release ${{ steps.validate_version.outputs.version }}"
body-path: "../CHANGELOG.md"
branch: "release-${{ steps.validate_version.outputs.version }}"
- name: Check outputs
if: ${{ steps.cpr.outputs.pull-request-number }}
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"