Skip to content

Commit 109717b

Browse files
committed
chore: 修正了release workflow的逻辑
修正gitee的bug 修正gitcode的bug 修正通知的bug
1 parent 414cb78 commit 109717b

File tree

1 file changed

+54
-66
lines changed

1 file changed

+54
-66
lines changed

.github/workflows/release.yml

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ jobs:
5252
Copy-Item -Path "$srcLocalesDir\*" -Destination $destDir -Force
5353
Write-Host "✓ i18n locales files copied to src-tauri/locales/"
5454
55-
- name: Rust cache
56-
uses: Swatinem/rust-cache@v2
57-
with:
58-
workspaces: src-tauri -> target
59-
6055
- name: Get version from tag
6156
id: get_version
6257
shell: pwsh
@@ -289,6 +284,7 @@ jobs:
289284
tag_name = $tag
290285
name = $releaseTitle
291286
body = $releaseNotes
287+
target_commitish = "main"
292288
prerelease = $false
293289
} | ConvertTo-Json
294290
@@ -362,79 +358,71 @@ jobs:
362358
$version = $env:VERSION
363359
$releaseTitle = $env:RELEASE_TITLE
364360
$tag = "v$version"
365-
$projectId = "ghost-him%2FZeroLaunch-rs" # 注意这里需要 URL 编码
366-
361+
$owner = "ghost-him"
362+
$repo = "ZeroLaunch-rs"
363+
$token = $env:GITCODE_TOKEN
364+
367365
# 读取生成的 Release Notes
368366
$releaseNotes = Get-Content -Path "release_notes.txt" -Raw -Encoding UTF8
369-
370-
# 1. 创建 Release (不带附件)
371-
# GitCode (GitLab) API 需要先创建 Release,然后再附加文件
367+
368+
# 根据 GitCode API 规范,token 需要统一放在 Header 中
372369
$headers = @{
373370
"Content-Type" = "application/json"
374-
"PRIVATE-TOKEN" = $env:GITCODE_TOKEN
371+
"Private-Token" = $token
375372
}
376-
377-
$releaseBody = @{
378-
tag_name = $tag
379-
name = $releaseTitle
380-
description = $releaseNotes
381-
} | ConvertTo-Json
382-
373+
383374
try {
384-
# 先尝试删除旧的 Release,确保幂等性
375+
# 1. 尝试删除旧的 Release (确保幂等性)
376+
# 需要先通过 tag 获取 release id,再进行删除
385377
try {
386-
Invoke-RestMethod -Uri "https://gitcode.com/api/v4/projects/$projectId/releases/$tag" -Method Delete -Headers $headers -ErrorAction SilentlyContinue
387-
echo "已删除旧的 GitCode Release"
378+
$getReleaseUri = "https://gitcode.com/api/v5/repos/$owner/$repo/releases/tags/$tag"
379+
$existingRelease = Invoke-RestMethod -Uri $getReleaseUri -Method Get -Headers $headers -ErrorAction SilentlyContinue
380+
if ($existingRelease) {
381+
$releaseIdToDelete = $existingRelease.id
382+
$deleteUri = "https://gitcode.com/api/v5/repos/$owner/$repo/releases/$releaseIdToDelete"
383+
Invoke-RestMethod -Uri $deleteUri -Method Delete -Headers $headers
384+
echo "已删除旧的 GitCode Release"
385+
}
388386
} catch {
389387
echo "没有旧的 GitCode Release 需要删除"
390388
}
391-
392-
# 创建新的 Release
393-
Invoke-RestMethod -Uri "https://gitcode.com/api/v4/projects/$projectId/releases" -Method Post -Headers $headers -Body $releaseBody
394-
echo "✅ GitCode Release 创建成功"
395-
} catch {
396-
echo "❌ 创建 GitCode Release 失败: $_"
397-
exit 1
398-
}
399389
400-
# 2. 上传构建产物作为 Release 的附件
401-
$files = Get-ChildItem -Path . -Include *.exe,*.msi,*.zip -File
402-
foreach ($file in $files) {
403-
echo "📦 正在上传 $($file.Name) 到 GitCode Release..."
404-
405-
# GitCode/GitLab 上传附件的 API 比较特殊,需要先将文件上传到项目,获取链接,再将链接附加到 Release
406-
try {
407-
$uploadUrl = "https://gitcode.com/api/v4/projects/$projectId/uploads"
408-
$uploadHeaders = @{
409-
"PRIVATE-TOKEN" = $env:GITCODE_TOKEN
390+
# 2. 创建新的 Release
391+
$createReleaseUri = "https://gitcode.com/api/v5/repos/$owner/$repo/releases"
392+
$releaseBody = @{
393+
tag_name = $tag
394+
name = $releaseTitle
395+
body = $releaseNotes
396+
target_commitish = "main"
397+
} | ConvertTo-Json
398+
399+
$response = Invoke-RestMethod -Uri $createReleaseUri -Method Post -Headers $headers -Body $releaseBody
400+
$releaseId = $response.id
401+
echo "✅ GitCode Release 创建成功,ID: $releaseId"
402+
403+
# 3. 上传构建产物作为附件
404+
$files = Get-ChildItem -Path . -Include *.exe,*.msi,*.zip -File
405+
foreach ($file in $files) {
406+
echo "📦 正在上传 $($file.Name) 到 GitCode Release..."
407+
$uploadUrl = "https://gitcode.com/api/v5/repos/$owner/$repo/releases/$releaseId/attach_files"
408+
409+
# 使用 curl 上传文件,对于 multipart/form-data 更可靠,并将 token 放入 header
410+
curl.exe -X POST $uploadUrl -H "Private-Token: $token" -F "file=@$($file.FullName)" --silent --show-error
411+
412+
if ($LASTEXITCODE -eq 0) {
413+
echo "✅ 已上传: $($file.Name)"
414+
} else {
415+
echo "❌ 上传失败: $($file.Name)"
416+
throw "Failed to upload $($file.Name) to GitCode"
410417
}
411-
412-
# 使用 curl 上传文件,Invoke-RestMethod 对 multipart/form-data 支持不佳
413-
$responseJson = curl.exe -s -X POST -H "PRIVATE-TOKEN: $env:GITCODE_TOKEN" -F "file=@$($file.FullName)" $uploadUrl
414-
$responseObj = $responseJson | ConvertFrom-Json
415-
416-
# 获取上传后的文件链接
417-
$fileUrl = $responseObj.url
418-
$fileName = $responseObj.alt
419-
420-
# 将上传后的文件链接附加到 Release
421-
$linkBody = @{
422-
name = $fileName
423-
url = "https://gitcode.com/ghost-him/ZeroLaunch-rs$($fileUrl)" # 需要拼接成完整 URL
424-
} | ConvertTo-Json
425-
426-
$attachUrl = "https://gitcode.com/api/v4/projects/$projectId/releases/$tag/assets/links"
427-
Invoke-RestMethod -Uri $attachUrl -Method Post -Headers $headers -Body $linkBody
428-
429-
echo "✅ 已上传并附加: $($file.Name)"
430-
} catch {
431-
echo "❌ 上传或附加 $($file.Name) 失败: $_"
432-
throw "Failed to upload $($file.Name) to GitCode"
433418
}
434-
}
435-
436-
echo "✅ GitCode Release 所有文件上传完成"
437419
420+
echo "✅ GitCode Release 所有文件上传完成"
421+
} catch {
422+
echo "❌ GitCode Release 创建或上传失败: $_"
423+
throw
424+
}
425+
438426
notify:
439427
name: Notify on completion
440428
needs:
@@ -446,11 +434,11 @@ jobs:
446434
steps:
447435
- name: Notify build status
448436
run: |
449-
if [ "${{ needs.build.result }}" == "success" && "${{ needs.release.result }}" == "success" ]; then
437+
if [[ "${{ needs.build.result }}" == "success" && "${{ needs.release.result }}" == "success" ]]; then
450438
echo "✅ Build and release completed successfully!"
451439
else
452440
echo "❌ Workflow failed. Status:"
453441
echo " - Build Job: ${{ needs.build.result }}"
454442
echo " - Release Job: ${{ needs.release.result }}"
455443
exit 1
456-
fi
444+
fi

0 commit comments

Comments
 (0)