-
Notifications
You must be signed in to change notification settings - Fork 1
310 lines (277 loc) · 11.1 KB
/
CI-Build-Release.yml
File metadata and controls
310 lines (277 loc) · 11.1 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
name: CI-Build & Release
#!!!不要把FancyHelper改成fancyhelper
on:
push:
tags: [ 'v*' ]
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:
inputs:
build_type:
description: '构建类型(snapshot-快照包/release-正式包)'
required: true
default: 'snapshot'
type: choice
options:
- snapshot
- release
custom_version:
description: '自定义版本号(仅release类型生效,无需带v前缀)'
required: false
type: string
skip_checks:
description: '跳过检查(仅用于紧急发布)'
required: false
default: false
type: boolean
permissions:
contents: write
pull-requests: read
jobs:
# ==================== 单元测试 ====================
test:
name: 单元测试 (Java ${{ matrix.java }})
runs-on: ubuntu-latest
if: ${{ !(github.event_name == 'workflow_dispatch' && inputs.skip_checks) }}
strategy:
matrix:
java: [ 17, 21 ]
fail-fast: false
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: temurin
cache: maven
- name: 运行单元测试
run: mvn -B test
- name: 上传测试报告
if: always()
uses: actions/upload-artifact@v4
with:
name: test-report-java${{ matrix.java }}
path: target/surefire-reports/
# ==================== 构建和发布 ====================
build:
name: 构建
runs-on: ubuntu-latest
needs: [ test ]
if: |
always() &&
(needs.test.result == 'success' || needs.test.result == 'skipped')
outputs:
IS_RELEASE: ${{ steps.check_release.outputs.IS_RELEASE }}
RELEASE_VERSION: ${{ steps.check_release.outputs.RELEASE_VERSION }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine Release Type
id: check_release
run: |
git fetch origin master --tags
IS_RELEASE=false
RELEASE_VERSION=""
SHORT_SHA="${GITHUB_SHA::7}"
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ inputs.build_type }}" == "release" ]]; then
IS_RELEASE=true
if [[ -n "${{ inputs.custom_version }}" ]]; then
RELEASE_VERSION="${{ inputs.custom_version }}"
else
RELEASE_VERSION="$SHORT_SHA"
fi
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
if git branch -r --contains ${{ github.sha }} | grep -q 'origin/master'; then
IS_RELEASE=true
RELEASE_VERSION=${GITHUB_REF_NAME#v}
fi
elif [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
TAG=$(git tag --points-at HEAD | grep '^v' | head -n 1)
if [[ -n "$TAG" ]]; then
IS_RELEASE=true
RELEASE_VERSION=${TAG#v}
fi
fi
echo "IS_RELEASE=$IS_RELEASE" >> $GITHUB_OUTPUT
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_OUTPUT
- uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
cache: maven
- name: 生成短 SHA
id: sha
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: 版本设置与构建打包
run: |
# 统一处理版本设置逻辑,解决原先多个 if 步骤导致的混乱
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
if [[ "${{ inputs.build_type }}" == "release" ]]; then
VERSION="${{ inputs.custom_version }}"
[[ -z "$VERSION" ]] && VERSION="${{ steps.sha.outputs.SHORT_SHA }}"
SKIP_TESTS="-DskipTests"
else
VERSION="#${{ steps.sha.outputs.SHORT_SHA }}"
SKIP_TESTS=""
fi
elif [[ "${{ steps.check_release.outputs.IS_RELEASE }}" == "true" ]]; then
VERSION="${{ steps.check_release.outputs.RELEASE_VERSION }}"
SKIP_TESTS="-DskipTests"
else
VERSION="#${{ steps.sha.outputs.SHORT_SHA }}"
SKIP_TESTS=""
fi
mvn -B versions:set -DnewVersion="$VERSION" -DgenerateBackupPoms=false
mvn -B package $SKIP_TESTS
- name: 上传构建产物
uses: actions/upload-artifact@v4
with:
name: FancyHelper-build-${{ github.run_id }}
path: target/FancyHelper-*.jar
# ==================== AI 生成发行注记 ====================
generate-overview:
name: AI 生成发行注记
runs-on: ubuntu-latest
needs: [ build ]
if: needs.build.outputs.IS_RELEASE == 'true'
outputs:
release_body_path: ${{ steps.compose_body.outputs.BODY_PATH }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 调用 Cloudflare AI 生成内容
id: ai_gen
env:
CLOUDFLARE_API_KEY: ${{ secrets.CLOUDFLARE_API_KEY }}
CLOUDFLARE_ACCOUNT_ID: "867520dfa43947e67f0416d72989a2af"
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ needs.build.outputs.RELEASE_VERSION }}
run: |
LAST_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
# 获取变更信息
if [[ -n "$LAST_TAG" ]]; then
COMMITS=$(git log $LAST_TAG..HEAD --pretty=format:"- %s" --no-merges)
PRS=$(gh api "repos/${{ github.repository }}/pulls?state=closed" --jq '.[] | select(.merged_at != null and .merged_at > "'$(git log -1 --format=%cI $LAST_TAG)'") | "- \(.title)"')
else
COMMITS=$(git log -20 --pretty=format:"- %s" --no-merges)
PRS=""
fi
# 构建 prompt 并写入文件
echo "你是一个专业的版本发布助手。请根据以下 Pull Request 和 Commit 信息,为 FancyHelper Minecraft 插件生成中文的发行注记 Overview。" > /tmp/prompt.txt
echo "" >> /tmp/prompt.txt
echo "版本号:v$VERSION" >> /tmp/prompt.txt
echo "" >> /tmp/prompt.txt
echo "PR 列表:" >> /tmp/prompt.txt
echo "$PRS" >> /tmp/prompt.txt
echo "" >> /tmp/prompt.txt
echo "Commit 列表:" >> /tmp/prompt.txt
echo "$COMMITS" >> /tmp/prompt.txt
echo "" >> /tmp/prompt.txt
echo "要求:" >> /tmp/prompt.txt
echo "1. 用中文撰写" >> /tmp/prompt.txt
echo "2. 必须使用以下格式(严格遵守):" >> /tmp/prompt.txt
echo " ## Overview" >> /tmp/prompt.txt
echo " * 第一条变更说明" >> /tmp/prompt.txt
echo " * 第二条变更说明" >> /tmp/prompt.txt
echo " * 第三条变更说明" >> /tmp/prompt.txt
echo "3. 每条变更必须以 \"* \" 开头(星号 + 空格),不要使用其他符号" >> /tmp/prompt.txt
echo "4. 简洁明了,每条控制在 10-30 字" >> /tmp/prompt.txt
echo "5. 突出主要功能更新、bug 修复和重要改动" >> /tmp/prompt.txt
echo "6. 不要添加 emoji" >> /tmp/prompt.txt
echo "7. 直接输出内容,不要有开场白" >> /tmp/prompt.txt
echo "8. 总条目控制在 3-6 条" >> /tmp/prompt.txt
echo "" >> /tmp/prompt.txt
echo "请生成发行注记 Overview:" >> /tmp/prompt.txt
# 获取 Cloudflare Account ID
ACCOUNT_ID="${CLOUDFLARE_ACCOUNT_ID}"
if [[ -z "$ACCOUNT_ID" || "$ACCOUNT_ID" == "null" ]]; then
echo "未配置 CloudFlare Account ID,跳过生成 Overview"
echo "## Overview" > /tmp/overview.md
echo "* 自动生成的版本 v$VERSION 发行说明" >> /tmp/overview.md
exit 0
fi
echo "Account ID: $ACCOUNT_ID"
# 构建请求体
PROMPT_CONTENT=$(cat /tmp/prompt.txt)
REQUEST_BODY=$(jq -n \
--arg model "@cf/meta/llama-3-8b-instruct" \
--arg prompt "$PROMPT_CONTENT" \
'{
"model": $model,
"messages": [
{
"role": "system",
"content": "你是一个专业的技术文档撰写助手。请生成简洁、专业的发行说明 Overview 部分。"
},
{
"role": "user",
"content": $prompt
}
]
}')
# 调用 Cloudflare AI API
RESPONSE=$(curl -s -X POST \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai/run/@cf/meta/llama-3-8b-instruct" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
echo "API 响应:"
echo "$RESPONSE" | head -c 2000
echo ""
# 解析响应
AI_CONTENT=$(echo "$RESPONSE" | jq -r '.result.response // empty')
if [[ -n "$AI_CONTENT" ]]; then
echo "$AI_CONTENT" > /tmp/overview.md
echo "AI 生成内容已写入 /tmp/overview.md"
else
echo "AI 响应为空,使用默认内容"
echo "## Overview" > /tmp/overview.md
echo "* 自动生成的版本 v$VERSION 发行说明" >> /tmp/overview.md
fi
# 获取 GitHub 自动生成的 Notes 作为补充
gh api "repos/${{ github.repository }}/releases/generate-notes" \
-f tag_name="v$VERSION" \
-f target_commitish="${{ github.sha }}" \
--jq '.body' > /tmp/github_notes.md || echo "" > /tmp/github_notes.md
- name: 组合 Release Body
id: compose_body
run: |
cat /tmp/overview.md > /tmp/release_body.md
echo "" >> /tmp/release_body.md
cat /tmp/github_notes.md >> /tmp/release_body.md
echo "BODY_PATH=/tmp/release_body.md" >> $GITHUB_OUTPUT
- name: 上传 Release Body
uses: actions/upload-artifact@v4
with:
name: release-body
path: /tmp/release_body.md
# ==================== 发布正式 Release ====================
release:
name: 发布 Release
runs-on: ubuntu-latest
needs: [ build, generate-overview ]
if: needs.build.outputs.IS_RELEASE == 'true'
steps:
- name: 下载构建产物
uses: actions/download-artifact@v4
with:
pattern: FancyHelper-build-*
path: target/
merge-multiple: true
- name: 下载 Release Body
uses: actions/download-artifact@v4
with:
name: release-body
path: ./
- name: 发布正式 Release
uses: softprops/action-gh-release@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: target/FancyHelper-*.jar
tag_name: v${{ needs.build.outputs.RELEASE_VERSION }}
name: Release v${{ needs.build.outputs.RELEASE_VERSION }}
body_path: ./release_body.md
make_latest: true