Publish NuGet to releases #830
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish NuGet to releases | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: '发布版本号 (例如: 1.0.0)' | |
| required: true | |
| type: string | |
| jobs: | |
| build-and-publish: | |
| runs-on: windows-latest | |
| permissions: | |
| contents: write # 创建 Release | |
| id-token: write # 用于 nuget.org Trusted Publishing (OIDC) | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v6 | |
| - name: 安装.NET SDK | |
| uses: actions/setup-dotnet@v5 | |
| with: | |
| dotnet-version: '10.0.x' # 支持GeneralUpdate.Drivelution的.NET 10目标框架 | |
| - name: 恢复依赖 | |
| run: dotnet restore ./src/c#/GeneralUpdate.slnx # 使用解决方案统一恢复 | |
| - name: 构建项目(确保生成DLL) | |
| run: dotnet build ./src/c#/GeneralUpdate.slnx -c Release --no-restore # 显式构建,避免重复恢复 | |
| - name: 打包NuGet包 | |
| continue-on-error: true # NuGet包可单独上传,不阻塞Release | |
| run: | | |
| $projects = @( | |
| "GeneralUpdate.Bowl", | |
| "GeneralUpdate.Core", | |
| "GeneralUpdate.Differential", | |
| "GeneralUpdate.Drivelution", | |
| "GeneralUpdate.Extension" | |
| ) | |
| $failed = $false | |
| foreach ($project in $projects) { | |
| Write-Host "=== 打包 $project ===" | |
| & dotnet pack ./src/c#/$project/$project.csproj ` | |
| -c Release ` | |
| -o ./nupkgs ` | |
| -p:Version='${{ github.event.inputs.version }}' ` | |
| -p:PackageVersion='${{ github.event.inputs.version }}' | |
| if ($LASTEXITCODE -ne 0) { | |
| Write-Host "!!! 警告: $project 打包时遇到可恢复错误 !!!" | |
| $failed = $true | |
| } | |
| } | |
| if ($failed) { Write-Host "部分包打包时有警告,但继续执行" } | |
| shell: pwsh | |
| - name: 推送NuGet包到 nuget.org | |
| continue-on-error: true # 即便推送失败也继续创建GitHub Release | |
| run: | | |
| $nupkgs = Get-ChildItem ./nupkgs/*.nupkg | |
| foreach ($nupkg in $nupkgs) { | |
| Write-Host "推送 $($nupkg.Name)..." | |
| dotnet nuget push $nupkg.FullName ` | |
| --api-key azure ` | |
| --source https://api.nuget.org/v3/index.json ` | |
| --skip-duplicate | |
| } | |
| shell: pwsh | |
| - name: 生成更新日志 | |
| shell: pwsh | |
| run: | | |
| $previousTag = git describe --tags --abbrev=0 --always 2>$null | |
| if ($LASTEXITCODE -ne 0 -or -not $previousTag) { | |
| $log = git log --oneline --no-decorate -100 | |
| } else { | |
| $log = git log "${previousTag}..HEAD" --oneline --no-decorate | |
| } | |
| $features = @() | |
| $fixes = @() | |
| $refactors = @() | |
| $tests = @() | |
| $docs = @() | |
| $chores = @() | |
| $others = @() | |
| foreach ($line in ($log -split "`n")) { | |
| $line = $line.Trim() | |
| if (-not $line) { continue } | |
| # Remove leading commit hash | |
| $msg = $line -replace '^[a-f0-9]+\s+', '' | |
| if ($msg -match '^(feat|feature)') { $features += "- $msg" } | |
| elseif ($msg -match '^fix') { $fixes += "- $msg" } | |
| elseif ($msg -match '^refactor') { $refactors += "- $msg" } | |
| elseif ($msg -match '^test') { $tests += "- $msg" } | |
| elseif ($msg -match '^docs') { $docs += "- $msg" } | |
| elseif ($msg -match '^(chore|cleanup|ci|build)') { $chores += "- $msg" } | |
| else { $others += "- $msg" } | |
| } | |
| $body = "" | |
| function append-section($title, $items) { | |
| if ($items.Count -gt 0) { | |
| $script:body += "## $title`n`n$($items -join "`n")`n`n" | |
| } | |
| } | |
| append-section "🚀 Features" $features | |
| append-section "🐛 Bug Fixes" $fixes | |
| append-section "♻️ Refactoring" $refactors | |
| append-section "✅ Tests" $tests | |
| append-section "📝 Documentation" $docs | |
| append-section "🔧 Chores" $chores | |
| append-section "📦 Other Changes" $others | |
| if (-not $body) { | |
| $body = "_No changes detected between tags._" | |
| } | |
| $body | Out-File -FilePath changelog.md -Encoding UTF8 | |
| Write-Host "=== 生成的更新日志 ===" | |
| Write-Host $body | |
| - name: 创建GitHub Release并上传NuGet包 | |
| uses: softprops/action-gh-release@v3 | |
| with: | |
| tag_name: v${{ github.event.inputs.version }} | |
| name: Release v${{ github.event.inputs.version }} | |
| body_path: changelog.md | |
| files: ./nupkgs/*.nupkg | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |