Skip to content

Commit 92ac521

Browse files
committed
chore: 优化release workflow
将构建与上传拆开,防止出现因为上传失败导致编译缓存没用上的问题 解决了上一次运行时出现的bug
1 parent ebffd3d commit 92ac521

File tree

1 file changed

+94
-22
lines changed

1 file changed

+94
-22
lines changed

.github/workflows/release.yml

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ on:
66
- 'v*.*.*' # 当推送 v1.0.0 格式的标签时触发
77
workflow_dispatch: # 允许手动触发
88

9+
permissions:
10+
contents: write
11+
actions: read
12+
913
env:
1014
CARGO_TERM_COLOR: always
1115

1216
jobs:
1317
build:
1418
name: Build Release
1519
runs-on: windows-latest
20+
outputs:
21+
version: ${{ steps.get_version.outputs.VERSION }}
22+
release_title: ${{ steps.get_version.outputs.RELEASE_TITLE }}
1623

1724
steps:
1825
- name: Checkout repository
@@ -74,6 +81,7 @@ jobs:
7481
cd xtask
7582
# 构建所有架构和 AI 变体
7683
cargo run --bin xtask build-all --arch all --ai both
84+
cd ..
7785
7886
- name: List build artifacts
7987
shell: pwsh
@@ -183,18 +191,74 @@ jobs:
183191
echo "RELEASE_NOTES_FILE=release_notes.txt" >> $env:GITHUB_OUTPUT
184192
echo "Generated release notes"
185193
194+
- name: Upload build artifacts
195+
uses: actions/upload-artifact@v4
196+
with:
197+
name: release-packages
198+
path: |
199+
*.exe
200+
*.msi
201+
*.zip
202+
if-no-files-found: error
203+
retention-days: 7
204+
205+
- name: Upload release notes
206+
uses: actions/upload-artifact@v4
207+
with:
208+
name: release-notes
209+
path: release_notes.txt
210+
if-no-files-found: error
211+
retention-days: 7
212+
213+
release:
214+
name: Publish Release
215+
needs: build
216+
runs-on: windows-latest
217+
218+
steps:
219+
- name: Checkout repository
220+
uses: actions/checkout@v4
221+
with:
222+
fetch-depth: 0
223+
224+
- name: Download build artifacts
225+
uses: actions/download-artifact@v4
226+
with:
227+
name: release-packages
228+
path: .
229+
230+
- name: Download release notes
231+
uses: actions/download-artifact@v4
232+
with:
233+
name: release-notes
234+
path: .
235+
236+
- name: List downloaded artifacts
237+
shell: pwsh
238+
run: |
239+
echo "=== Downloaded artifacts ==="
240+
$files = Get-ChildItem -Path . -File | Where-Object { $_.Extension -in '.exe', '.msi', '.zip', '.txt' }
241+
if ($files.Count -eq 0) {
242+
echo "❌ 错误:没有找到任何下载的构建产物或说明文件!"
243+
exit 1
244+
}
245+
$files | ForEach-Object {
246+
$sizeMB = [math]::Round($_.Length / 1MB, 2)
247+
echo " ✅ $($_.Name) ($sizeMB MB)"
248+
}
249+
186250
- name: Create GitHub Release
187251
id: create_release
188252
uses: softprops/action-gh-release@v2
189253
with:
190-
name: ${{ steps.get_version.outputs.RELEASE_TITLE }}
254+
name: ${{ needs.build.outputs.release_title }}
255+
body_path: release_notes.txt
191256
files: |
192257
*.exe
193258
*.msi
194259
*.zip
195260
draft: false
196261
prerelease: false
197-
generate_release_notes: true
198262
env:
199263
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
200264

@@ -204,10 +268,10 @@ jobs:
204268
run: |
205269
# 配置 Gitee
206270
git config --global user.name "ghost-him"
207-
git config --global user.email "${{ secrets.GITEE_EMAIL }}"
271+
git config --global user.email "$env:GITEE_EMAIL"
208272
209273
# 添加 Gitee 远程仓库
210-
git remote add gitee https://oauth2:${{ secrets.GITEE_TOKEN }}@gitee.com/ghost-him/ZeroLaunch-rs.git || true
274+
git remote add gitee "https://oauth2:$($env:GITEE_TOKEN)@gitee.com/ghost-him/ZeroLaunch-rs.git" || true
211275
212276
# 推送代码和标签到 Gitee
213277
git push gitee main --tags -f || echo "Failed to push to Gitee"
@@ -218,9 +282,13 @@ jobs:
218282
- name: Create Gitee Release
219283
if: success()
220284
shell: pwsh
285+
env:
286+
VERSION: ${{ needs.build.outputs.version }}
287+
RELEASE_TITLE: ${{ needs.build.outputs.release_title }}
288+
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
221289
run: |
222-
$version = "${{ steps.get_version.outputs.VERSION }}"
223-
$releaseTitle = "${{ steps.get_version.outputs.RELEASE_TITLE }}"
290+
$version = $env:VERSION
291+
$releaseTitle = $env:RELEASE_TITLE
224292
$tag = "v$version"
225293
226294
# 读取生成的 Release Notes
@@ -229,7 +297,7 @@ jobs:
229297
# 使用 Gitee API 创建 Release
230298
$headers = @{
231299
"Content-Type" = "application/json"
232-
"Authorization" = "token ${{ secrets.GITEE_TOKEN }}"
300+
"Authorization" = "token $env:GITEE_TOKEN"
233301
}
234302
235303
$body = @{
@@ -260,7 +328,7 @@ jobs:
260328
$files = Get-ChildItem -Path . -Include *.exe,*.msi,*.zip -File
261329
foreach ($file in $files) {
262330
echo "📦 正在上传 $($file.Name) 到 Gitee Release..."
263-
$uploadUrl = "https://gitee.com/api/v5/repos/ghost-him/ZeroLaunch-rs/releases/$releaseId/attach_files?access_token=${{ secrets.GITEE_TOKEN }}"
331+
$uploadUrl = "https://gitee.com/api/v5/repos/ghost-him/ZeroLaunch-rs/releases/$releaseId/attach_files?access_token=$env:GITEE_TOKEN"
264332
265333
# 使用 curl 上传文件(更可靠)
266334
curl.exe -X POST $uploadUrl -F "file=@$($file.FullName)" --silent --show-error
@@ -278,19 +346,17 @@ jobs:
278346
echo "❌ Gitee Release 创建或上传失败: $_"
279347
throw
280348
}
281-
env:
282-
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
283349
284350
- name: Upload to GitCode
285351
if: success()
286352
shell: pwsh
287353
run: |
288354
# 配置 GitCode
289355
git config --global user.name "ghost-him"
290-
git config --global user.email "${{ secrets.GITCODE_EMAIL }}"
356+
git config --global user.email "$env:GITCODE_EMAIL"
291357
292358
# 添加 GitCode 远程仓库
293-
git remote add gitcode https://oauth2:${{ secrets.GITCODE_TOKEN }}@gitcode.com/ghost-him/ZeroLaunch-rs.git || true
359+
git remote add gitcode "https://oauth2:$($env:GITCODE_TOKEN)@gitcode.com/ghost-him/ZeroLaunch-rs.git" || true
294360
295361
# 推送代码和标签到 GitCode
296362
git push gitcode main --tags -f || echo "Failed to push to GitCode"
@@ -301,9 +367,13 @@ jobs:
301367
- name: Create and Upload to GitCode Release
302368
if: success()
303369
shell: pwsh
370+
env:
371+
VERSION: ${{ needs.build.outputs.version }}
372+
RELEASE_TITLE: ${{ needs.build.outputs.release_title }}
373+
GITCODE_TOKEN: ${{ secrets.GITCODE_TOKEN }}
304374
run: |
305-
$version = "${{ steps.get_version.outputs.VERSION }}"
306-
$releaseTitle = "${{ steps.get_version.outputs.RELEASE_TITLE }}"
375+
$version = $env:VERSION
376+
$releaseTitle = $env:RELEASE_TITLE
307377
$tag = "v$version"
308378
$projectId = "ghost-him%2FZeroLaunch-rs" # 注意这里需要 URL 编码
309379
@@ -314,7 +384,7 @@ jobs:
314384
# GitCode (GitLab) API 需要先创建 Release,然后再附加文件
315385
$headers = @{
316386
"Content-Type" = "application/json"
317-
"PRIVATE-TOKEN" = "${{ secrets.GITCODE_TOKEN }}"
387+
"PRIVATE-TOKEN" = $env:GITCODE_TOKEN
318388
}
319389
320390
$releaseBody = @{
@@ -349,11 +419,11 @@ jobs:
349419
try {
350420
$uploadUrl = "https://gitcode.com/api/v4/projects/$projectId/uploads"
351421
$uploadHeaders = @{
352-
"PRIVATE-TOKEN" = "${{ secrets.GITCODE_TOKEN }}"
422+
"PRIVATE-TOKEN" = $env:GITCODE_TOKEN
353423
}
354424
355425
# 使用 curl 上传文件,Invoke-RestMethod 对 multipart/form-data 支持不佳
356-
$responseJson = curl.exe -s -X POST -H "PRIVATE-TOKEN: ${{ secrets.GITCODE_TOKEN }}" -F "file=@$($file.FullName)" $uploadUrl
426+
$responseJson = curl.exe -s -X POST -H "PRIVATE-TOKEN: $env:GITCODE_TOKEN" -F "file=@$($file.FullName)" $uploadUrl
357427
$responseObj = $responseJson | ConvertFrom-Json
358428
359429
# 获取上传后的文件链接
@@ -377,21 +447,23 @@ jobs:
377447
}
378448
379449
echo "✅ GitCode Release 所有文件上传完成"
380-
env:
381-
GITCODE_TOKEN: ${{ secrets.GITCODE_TOKEN }}
382450
383451
notify:
384452
name: Notify on completion
385-
needs: build
453+
needs:
454+
- build
455+
- release
386456
runs-on: ubuntu-latest
387457
if: always()
388458

389459
steps:
390460
- name: Notify build status
391461
run: |
392-
if [ "${{ needs.build.result }}" == "success" ]; then
462+
if [ "${{ needs.build.result }}" == "success" && "${{ needs.release.result }}" == "success" ]; then
393463
echo "✅ Build and release completed successfully!"
394464
else
395-
echo "❌ Build failed!"
465+
echo "❌ Workflow failed. Status:"
466+
echo " - Build Job: ${{ needs.build.result }}"
467+
echo " - Release Job: ${{ needs.release.result }}"
396468
exit 1
397469
fi

0 commit comments

Comments
 (0)