Skip to content

Commit 1fcd91b

Browse files
committed
测试发布
1 parent 0004f9a commit 1fcd91b

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

.github/workflows/nuget-publish.yml

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,64 @@ jobs:
6262
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
6363

6464
steps:
65-
- name: Download NuGet package artifact
66-
uses: actions/download-artifact@v4
65+
- name: Checkout code
66+
uses: actions/checkout@v4
6767
with:
68-
name: nuget-package
69-
path: ./packages
68+
fetch-depth: 0
7069

7170
- name: Setup .NET
7271
uses: actions/setup-dotnet@v4
7372
with:
7473
dotnet-version: ${{ env.DOTNET_VERSION }}
7574

75+
- name: Get current version from Directory.Build.props
76+
id: get-version
77+
run: |
78+
[xml]$props = Get-Content "Directory.Build.props"
79+
$version = $props.Project.PropertyGroup.Version
80+
Write-Host "Current version: $version"
81+
echo "version=$version" >> $env:GITHUB_OUTPUT
82+
echo "package-name=CodeInject" >> $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'
116+
uses: actions/download-artifact@v4
117+
with:
118+
name: nuget-package
119+
path: ./packages
120+
76121
- name: Verify packages exist
122+
if: steps.check-version.outputs.should-publish == 'true'
77123
run: |
78124
if (-not (Test-Path "./packages/*.nupkg")) {
79125
Write-Error "No .nupkg files found in ./packages directory"
@@ -84,13 +130,22 @@ jobs:
84130
Get-ChildItem -Path "./packages" -Filter "*.nupkg"
85131
86132
- name: Publish NuGet package
133+
if: steps.check-version.outputs.should-publish == 'true'
87134
run: |
135+
Write-Host "Publishing version ${{ steps.get-version.outputs.version }} to NuGet.org..."
88136
$nupkgFiles = Get-ChildItem -Path "./packages" -Filter "*.nupkg"
89137
foreach ($file in $nupkgFiles) {
138+
Write-Host "Publishing $($file.Name)..."
90139
dotnet nuget push $file.FullName `
91140
--api-key ${{ secrets.NUGET_API_KEY }} `
92141
--source https://api.nuget.org/v3/index.json `
93142
--skip-duplicate
94143
}
144+
Write-Host "NuGet package published successfully!"
95145
env:
96146
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."

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
English | [简体中文](README.zh-CN.md)
44

55
[![Build and Publish](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml)
6-
[![Release](https://github.com/RRQM/CodeInject/actions/workflows/release.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/release.yml)
76
[![NuGet Version](https://img.shields.io/nuget/v/CodeInject)](https://www.nuget.org/packages/CodeInject/)
87
[![NuGet Downloads](https://img.shields.io/nuget/dt/CodeInject)](https://www.nuget.org/packages/CodeInject/)
98

README.zh-CN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[English](README.md) | 简体中文
44

55
[![Build and Publish](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml)
6-
[![Release](https://github.com/RRQM/CodeInject/actions/workflows/release.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/release.yml)
76
[![NuGet Version](https://img.shields.io/nuget/v/CodeInject)](https://www.nuget.org/packages/CodeInject/)
87
[![NuGet Downloads](https://img.shields.io/nuget/dt/CodeInject)](https://www.nuget.org/packages/CodeInject/)
98

0 commit comments

Comments
 (0)