1- name : Build and Publish NuGet Package
1+ name : Build and Publish NuGet Packages
22
33on :
44 push :
1313
1414env :
1515 DOTNET_VERSION : ' 8.0.x'
16- PROJECT_PATH : ' src/Baboon/Baboon.csproj '
16+ SOLUTION_PATH : ' src/Baboon.slnx '
1717
1818jobs :
1919 build-and-test :
@@ -31,25 +31,36 @@ jobs:
3131 dotnet-version : ${{ env.DOTNET_VERSION }}
3232
3333 - name : Restore dependencies
34- run : dotnet restore src/Baboon.sln
34+ run : dotnet restore ${{ env.SOLUTION_PATH }}
3535
36- - name : Build project
37- run : dotnet build ${{ env.PROJECT_PATH }} --configuration Release --no-restore
36+ - name : Build all projects
37+ run : dotnet build ${{ env.SOLUTION_PATH }} --configuration Release --no-restore
3838
3939 - name : Run tests (if any)
40- run : dotnet test src/ --configuration Release --no-build --verbosity normal --collect:"XPlat Code Coverage"
40+ run : dotnet test ${{ env.SOLUTION_PATH }} --configuration Release --no-build --verbosity normal --collect:"XPlat Code Coverage"
4141 continue-on-error : true
4242
43- - name : Pack NuGet package
44- run : dotnet pack ${{ env.PROJECT_PATH }} --configuration Release --no-build --output ./packages
43+ - name : Pack all NuGet packages
44+ run : |
45+ Write-Host "Packing all projects in src directory..."
46+ $projects = Get-ChildItem -Path "src" -Filter "*.csproj" -Recurse | Where-Object {
47+ $_.Directory.Name -notmatch "obj|bin"
48+ }
49+
50+ foreach ($project in $projects) {
51+ Write-Host "Packing $($project.Name)..."
52+ dotnet pack $project.FullName --configuration Release --no-build --output ./packages
53+ }
54+
55+ Write-Host "All packages packed successfully!"
4556
4657 - name : List packages for debugging
4758 run : Get-ChildItem -Path "./packages" -Recurse
4859
49- - name : Upload NuGet package as artifact
60+ - name : Upload NuGet packages as artifact
5061 uses : actions/upload-artifact@v4
5162 with :
52- name : nuget-package
63+ name : nuget-packages
5364 path : ./packages/*.nupkg
5465
5566 publish-nuget :
@@ -68,58 +79,13 @@ jobs:
6879 with :
6980 dotnet-version : ${{ env.DOTNET_VERSION }}
7081
71- - name : Get current version from csproj
72- id : get-version
73- run : |
74- [xml]$props = Get-Content $env:PROJECT_PATH
75- $version = $props.Project.PropertyGroup.Version
76- $packageId = $props.Project.PropertyGroup.PackageId
77- if (-not $packageId -or $packageId -eq '') { $packageId = $props.Project.PropertyGroup.Title }
78- if (-not $packageId -or $packageId -eq '') { $packageId = [System.IO.Path]::GetFileNameWithoutExtension($env:PROJECT_PATH) }
79- Write-Host "Current version: $version"
80- Write-Host "Package name: $packageId"
81- echo "version=$version" >> $env:GITHUB_OUTPUT
82- echo "package-name=$packageId" >> $env:GITHUB_OUTPUT
83-
84- - name : Check if version exists on NuGet.org
85- id : check-version
86- run : |
87- $packageName = "${{ steps.get-version.outputs.package-name }}"
88- $version = "${{ steps.get-version.outputs.version }}"
89-
90- Write-Host "Checking if $packageName version $version exists on NuGet.org..."
91-
92- try {
93- $response = Invoke-RestMethod -Uri "https://api.nuget.org/v3-flatcontainer/$($packageName.ToLower())/index.json" -Method Get
94- $existingVersions = $response.versions
95-
96- if ($existingVersions -contains $version) {
97- Write-Host "Version $version already exists on NuGet.org. Skipping publish."
98- echo "should-publish=false" >> $env:GITHUB_OUTPUT
99- } else {
100- Write-Host "Version $version does not exist on NuGet.org. Proceeding with publish."
101- echo "should-publish=true" >> $env:GITHUB_OUTPUT
102- }
103- } catch {
104- if ($_.Exception.Response.StatusCode -eq 404) {
105- Write-Host "Package $packageName not found on NuGet.org. This might be the first publish."
106- echo "should-publish=true" >> $env:GITHUB_OUTPUT
107- } else {
108- Write-Host "Error checking NuGet.org: $($_.Exception.Message)"
109- Write-Host "Proceeding with publish (will use --skip-duplicate flag)."
110- echo "should-publish=true" >> $env:GITHUB_OUTPUT
111- }
112- }
113-
114- - name : Download NuGet package artifact
115- if : steps.check-version.outputs.should-publish == 'true'
82+ - name : Download NuGet packages artifact
11683 uses : actions/download-artifact@v4
11784 with :
118- name : nuget-package
85+ name : nuget-packages
11986 path : ./packages
12087
12188 - name : Verify packages exist
122- if : steps.check-version.outputs.should-publish == 'true'
12389 run : |
12490 if (-not (Test-Path "./packages/*.nupkg")) {
12591 Write-Error "No .nupkg files found in ./packages directory"
@@ -129,23 +95,60 @@ jobs:
12995 Write-Host "Found packages:"
13096 Get-ChildItem -Path "./packages" -Filter "*.nupkg"
13197
132- - name : Publish NuGet package
133- if : steps.check-version.outputs.should-publish == 'true'
98+ - name : Check and publish NuGet packages
13499 run : |
135- Write-Host "Publishing version ${{ steps.get-version.outputs.version }} to NuGet.org..."
136100 $nupkgFiles = Get-ChildItem -Path "./packages" -Filter "*.nupkg"
101+
137102 foreach ($file in $nupkgFiles) {
138- Write-Host "Publishing $($file.Name)..."
139- dotnet nuget push $file.FullName `
140- --api-key ${{ secrets.NUGET_API_KEY }} `
141- --source https://api.nuget.org/v3/index.json `
142- --skip-duplicate
103+ Write-Host "`n========================================"
104+ Write-Host "Processing: $($file.Name)"
105+ Write-Host "========================================"
106+
107+ # 从文件名提取包名和版本号
108+ $fileName = $file.BaseName
109+ if ($fileName -match '(.+)\.(\d+\.\d+\.\d+.*)') {
110+ $packageName = $matches[1]
111+ $version = $matches[2]
112+
113+ Write-Host "Package: $packageName"
114+ Write-Host "Version: $version"
115+
116+ # 检查版本是否已存在
117+ $shouldPublish = $true
118+ try {
119+ $response = Invoke-RestMethod -Uri "https://api.nuget.org/v3-flatcontainer/$($packageName.ToLower())/index.json" -Method Get
120+ $existingVersions = $response.versions
121+
122+ if ($existingVersions -contains $version) {
123+ Write-Host "Version $version already exists on NuGet.org. Skipping."
124+ $shouldPublish = $false
125+ } else {
126+ Write-Host "Version $version does not exist. Will publish."
127+ }
128+ } catch {
129+ if ($_.Exception.Response.StatusCode -eq 404) {
130+ Write-Host "Package not found on NuGet.org. This might be the first publish."
131+ } else {
132+ Write-Host "Could not check existing versions. Will attempt to publish with --skip-duplicate."
133+ }
134+ }
135+
136+ # 发布包
137+ if ($shouldPublish) {
138+ Write-Host "Publishing $($file.Name)..."
139+ dotnet nuget push $file.FullName `
140+ --api-key ${{ secrets.NUGET_API_KEY }} `
141+ --source https://api.nuget.org/v3/index.json `
142+ --skip-duplicate
143+ Write-Host "Published successfully!"
144+ }
145+ } else {
146+ Write-Host "Warning: Could not parse package name and version from $($file.Name)"
147+ }
143148 }
144- Write-Host "NuGet package published successfully!"
149+
150+ Write-Host "`n========================================"
151+ Write-Host "All packages processed!"
152+ Write-Host "========================================"
145153 env :
146154 NUGET_API_KEY : ${{ secrets.NUGET_API_KEY }}
147-
148- - name : Skip publish notification
149- if : steps.check-version.outputs.should-publish == 'false'
150- run : |
151- Write-Host "::notice::NuGet package version ${{ steps.get-version.outputs.version }} already exists. Publishing skipped."
0 commit comments