-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
270 lines (245 loc) · 12.5 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
name: 'NuGet push'
description: 'Pushes a nuget package and symbols.'
branding:
icon: package
color: blue
inputs:
api-key:
description: 'The API key for the NuGet server. Used when pushing the NuGet packages and symbols.'
required: true
fail-if-exists:
description: 'Indicates whether this actions should fail if the NuGet package being pushed already exists. Defaults to false.'
required: false
default: 'false'
working-directory:
description: 'The directory that will be used to push NuGet packages. It will push all NuGet packages (*.nupkg) and corresponding symbol packages (*.snupkg) present in the directory.'
required: false
nuget-package:
description: 'The filepath for the NuGet package to be pushed.'
required: false
symbols-package:
description: 'The filepath for the symbols package to be pushed.'
required: false
outputs:
status:
description: "The overall status of pushing the NuGet packages and corresponding symbols. Possible values are 'ok' or 'error'."
value: ${{ steps.set-outputs.outputs.status }}
push-result:
description: "The result of pushing the NuGet packages and corresponding symbols as a JSON string."
value: ${{ steps.set-outputs.outputs.push-result }}
runs:
using: "composite"
steps:
- name: Setup variables
shell: pwsh
id: setup
run: |
Write-Output "nuget-server-url=https://api.nuget.org/v3/index.json" >> $env:GITHUB_OUTPUT
Write-Output "nuget-push-ok-status=ok" >> $env:GITHUB_OUTPUT
Write-Output "nuget-push-already-exists-status=nuget-already-exists" >> $env:GITHUB_OUTPUT
Write-Output "nuget-push-failed-status=nuget-push-failed" >> $env:GITHUB_OUTPUT
Write-Output "symbols-push-failed-status=symbols-push-failed" >> $env:GITHUB_OUTPUT
$workingDirectory = '${{ inputs.working-directory }}'
$nugetPackage = '${{ inputs.nuget-package }}'
if($workingDirectory -ne '' -and $nugetPackage -ne '')
{
Write-Output "::error::'working-directory' and 'nuget-package' input parameters cannot be specified at the same time."
Exit 1
}
- name: Push all NuGet and Symbols from working directory
shell: pwsh
id: push-workding-dir
if: inputs.working-directory != ''
working-directory: ${{ inputs.working-directory }}
run: |
$nugetServerUrl = '${{ steps.setup.outputs.nuget-server-url }}'
$output = @{
status = ''
packages = [System.Collections.ArrayList]::new()
}
# group '.nupgk' and '.snupkg' by filename. This assumes each package (.nupgk) and corresponding symbols (.snupkg)
# have the same filename, only the extension is different.
$groups = Get-ChildItem -Path .\ -Filter *.*nupkg | Group-Object -Property BaseName
# output the packages and symbols that will be uploaded
Write-Output "::group::Pushing the following NuGet packages and symbols to $nugetServerUrl"
foreach($group in $groups) {
Write-Output "The package with filename $($group.Name) will have the following files uploaded:"
foreach($file in $group.Group) {
Write-Output "$file"
}
}
Write-Output "::endgroup::"
$nugetPushOkStatus = '${{ steps.setup.outputs.nuget-push-ok-status }}'
$nugetPushAlreadyExistsStatus = '${{ steps.setup.outputs.nuget-push-already-exists-status }}'
$nugetPushFailedStatus = '${{ steps.setup.outputs.nuget-push-failed-status }}'
$symbolsPushFailedStatus = '${{ steps.setup.outputs.symbols-push-failed-status }}'
# for each group we will try to push the NuGet package and if that is successfull we will then push the symbols package.
foreach($group in $groups) {
# each group will be a group of '.nupgk' and corresponding '.snupkg'
# try to upload the nuget package and if is successful then upload its symbols if available.
$files = $group.Group
$package = $files | Where-Object {$_.Extension -eq '.nupkg' } | Select-Object -First 1
$symbols = $files | Where-Object {$_.Extension -eq '.snupkg' } | Select-Object -First 1
$packageOutput = @{
package = "$package"
symbols = "$symbols"
status = ''
}
# upload NuGet package if exists
if($package -ne $null) {
Write-Output "::group::Pushing $package NuGet package."
$nugetResponse = dotnet nuget push $package --api-key ${{ inputs.api-key }} --source $nugetServerUrl --no-symbols --force-english-output
$nugetPushExitCode = $LastExitCode
$nugetAlreadyExists = ($nugetResponse | ?{$_.Contains("Response status code does not indicate success: 409")}).Length -gt 0
if($nugetAlreadyExists) {
$packageOutput.status = $nugetPushAlreadyExistsStatus
Write-Output "The NuGet package $package already exists. The output from nuget push was:"
Write-Output $nugetResponse
}
elseif($nugetPushExitCode -ne 0) {
$packageOutput.status = $nugetPushFailedStatus
Write-Output "::error::Failed to push NuGet package $package. See the action logs for more information."
Write-Output "NuGet push failed. The output from nuget push was:"
Write-Output $nugetResponse
}
else {
$packageOutput.status = $nugetPushOkStatus
Write-Output "NuGet push was successful. The output from nuget push was:"
Write-Output $nugetResponse
# upload symbols if exists
if($symbols -ne $null) {
Write-Output "Pushing NuGet symbols package $symbols"
$symbolsResponse = dotnet nuget push $symbols --api-key ${{ inputs.api-key }} --source $nugetServerUrl
$symbolsPushExitCode = $LastExitCode
Write-Output $symbolsResponse
if($symbolsPushExitCode -ne 0) {
$packageOutput.status = $symbolsPushFailedStatus
Write-Output "::error::Failed to push NuGet symbols package $symbols. See the action logs for more information."
}
}
}
$output.packages.Add($packageOutput)
Write-Output "::endgroup::"
}
}
$outputAsJson = $output | ConvertTo-Json -compress
Write-Output "push-result=$outputAsJson" >> $env:GITHUB_OUTPUT
Exit 0 # always exit sucessfully regardless of the exit codes from 'dotnet nuget push' command, the exit code for the action will be set in the next step
- name: Push specific NuGet and Symbols
shell: pwsh
id: push-specific-files
if: inputs.nuget-package != ''
run: |
$nugePackageSpecified = '${{ inputs.nuget-package }}' -ne ''
$symbolsSpecified = '${{ inputs.symbols-package }}' -ne ''
$nugetServerUrl = '${{ steps.setup.outputs.nuget-server-url }}'
$nugetPushOkStatus = '${{ steps.setup.outputs.nuget-push-ok-status }}'
$nugetPushAlreadyExistsStatus = '${{ steps.setup.outputs.nuget-push-already-exists-status }}'
$nugetPushFailedStatus = '${{ steps.setup.outputs.nuget-push-failed-status }}'
$symbolsPushFailedStatus = '${{ steps.setup.outputs.symbols-push-failed-status }}'
$output = @{
status = ''
packages = [System.Collections.ArrayList]::new()
}
$packageOutput = @{
package = ''
symbols = ''
status = ''
}
if($nugePackageSpecified) {
$package = Get-Item '${{ inputs.nuget-package }}'
$packageOutput.package = "$package"
}
if($symbolsSpecified) {
$symbols = Get-Item '${{ inputs.symbols-package }}'
$packageOutput.symbols = "$symbols"
}
# output the packages and symbols that will be uploaded
Write-Output "::group::Pushing the following NuGet packages and symbols to $nugetServerUrl"
Write-Output "$package"
Write-Output "$symbols"
Write-Output "::endgroup::"
# try to upload the nuget package and if is successful then upload its symbols if available.
# upload NuGet package if exists
if($nugePackageSpecified) {
Write-Output "::group::Pushing $package NuGet package."
$nugetResponse = dotnet nuget push $package --api-key ${{ inputs.api-key }} --source $nugetServerUrl --no-symbols --force-english-output
$nugetPushExitCode = $LastExitCode
$nugetAlreadyExists = ($nugetResponse | ?{$_.Contains("Response status code does not indicate success: 409")}).Length -gt 0
if($nugetAlreadyExists) {
$packageOutput.status = $nugetPushAlreadyExistsStatus
Write-Output "The NuGet package $package already exists. The output from nuget push was:"
Write-Output $nugetResponse
}
elseif($nugetPushExitCode -ne 0) {
$packageOutput.status = $nugetPushFailedStatus
Write-Output "::error::Failed to push NuGet package $package. See the action logs for more information."
Write-Output "NuGet push failed. The output from nuget push was:"
Write-Output $nugetResponse
}
else {
$packageOutput.status = $nugetPushOkStatus
Write-Output "NuGet push was successful. The output from nuget push was:"
Write-Output $nugetResponse
# upload symbols if exists
if($symbolsSpecified) {
Write-Output "Pushing NuGet symbols package $symbols"
$symbolsResponse = dotnet nuget push $symbols --api-key ${{ inputs.api-key }} --source $nugetServerUrl
$symbolsPushExitCode = $LastExitCode
Write-Output $symbolsResponse
if($symbolsPushExitCode -ne 0) {
$packageOutput.status = $symbolsPushFailedStatus
Write-Output "::error::Failed to push NuGet symbols package $symbols. See the action logs for more information."
}
}
}
$output.packages.Add($packageOutput)
Write-Output "::endgroup::"
}
$outputAsJson = $output | ConvertTo-Json -compress
Write-Output "push-result=$outputAsJson" >> $env:GITHUB_OUTPUT
Exit 0 # always exit sucessfully regardless of the exit codes from 'dotnet nuget push' command, the exit code for the action will be set in the next step
- name: Set action outputs
shell: pwsh
id: set-outputs
run: |
$pushWorkingDirOutput = '${{ steps.push-workding-dir.outputs.push-result }}'
$pushSpecificFilesOutput = '${{ steps.push-specific-files.outputs.push-result }}'
if($pushWorkingDirOutput -ne '') {
$pushResult = $pushWorkingDirOutput | ConvertFrom-Json
}
elseif($pushSpecificFilesOutput -ne '') {
$pushResult = $pushSpecificFilesOutput | ConvertFrom-Json
}
else {
Write-Output "::error::Unexpected empty output from nuget push step. See action logs for more details."
Exit 1
}
$failIfAlreadyExists = [System.Convert]::ToBoolean("${{ inputs.fail-if-exists }}")
$nugetPushAlreadyExistsStatus = '${{ steps.setup.outputs.nuget-push-already-exists-status }}'
$nugetPushFailedStatus = '${{ steps.setup.outputs.nuget-push-failed-status }}'
$symbolsPushFailedStatus = '${{ steps.setup.outputs.symbols-push-failed-status }}'
$packagesFailed = $pushResult.packages | Where-Object {$_.status -eq $nugetPushFailedStatus -or $_.status -eq $symbolsPushFailedStatus }
$packagesAlreadyExist = $pushResult.packages | Where-Object {$_.status -eq $nugetPushAlreadyExistsStatus }
$failed = ($packagesFailed.count -ne 0) -or ($failIfAlreadyExists -and $packagesAlreadyExist.count -ne 0)
if($failed) {
$pushResult.status = 'error'
}
else {
$pushResult.status = 'ok'
}
Write-Output "::group::Action output"
$pushResult | ConvertTo-Json
Write-Output "::endgroup::"
# set action output
$pushResultAsJson = $pushResult | ConvertTo-Json -compress
Write-Output "push-result=$pushResultAsJson" >> $env:GITHUB_OUTPUT
Write-Output "status=$($pushResult.status)" >> $env:GITHUB_OUTPUT
if($failed) {
Write-Output "Some NuGet packages weren't uploaded successfully."
Exit 1
}
else {
Write-Output "All NuGet packages uploaded successfully."
Exit 0
}