-
Notifications
You must be signed in to change notification settings - Fork 17
195 lines (171 loc) · 6.53 KB
/
Copy pathpublish-harness-nuget.yml
File metadata and controls
195 lines (171 loc) · 6.53 KB
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
name: Publish DotCraft Harness NuGet
on:
workflow_dispatch:
inputs:
target:
description: "Dry run only, or publish to nuget.org"
required: true
default: dry-run
type: choice
options:
- dry-run
- nuget-org
confirm:
description: "Required for nuget-org: enter the exact DotCraft.Harness version from the project file"
required: false
type: string
permissions:
contents: read
jobs:
pack:
name: Validate, test, and pack
runs-on: ubuntu-latest
outputs:
version: ${{ steps.validate.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Validate release inputs
id: validate
shell: pwsh
env:
PACKAGE_PROJECT: ./src/DotCraft.Harness/DotCraft.Harness.csproj
TARGET: ${{ inputs.target }}
CONFIRM: ${{ inputs.confirm }}
GITHUB_REF_FULL: ${{ github.ref }}
run: |
$ErrorActionPreference = "Stop"
if (!(Test-Path $env:PACKAGE_PROJECT)) {
throw "Package project not found: $($env:PACKAGE_PROJECT)"
}
[xml]$project = Get-Content -Raw $env:PACKAGE_PROJECT
$version = @($project.Project.PropertyGroup | ForEach-Object { $_.Version } | Where-Object { ![string]::IsNullOrWhiteSpace($_) } | Select-Object -First 1)
$version = "$version".Trim()
if ([string]::IsNullOrWhiteSpace($version)) {
throw "Set <Version> in $($env:PACKAGE_PROJECT) before publishing."
}
$previewPattern = '^\d+\.\d+\.\d+-preview\.\d+$'
$stablePattern = '^\d+\.\d+\.\d+$'
if ($version -match $previewPattern) {
$channel = "preview"
}
elseif ($version -match $stablePattern) {
$channel = "stable"
}
else {
throw "DotCraft.Harness <Version> must be MAJOR.MINOR.PATCH-preview.N or MAJOR.MINOR.PATCH. Current value: $version"
}
if ($env:TARGET -eq "nuget-org") {
if ($env:GITHUB_REF_FULL -ne "refs/heads/main") {
throw "nuget.org publishing is allowed only from the main branch."
}
if ($env:CONFIRM -cne $version) {
throw "Confirmation must exactly match the DotCraft.Harness version: $version"
}
}
$indexUrl = "https://api.nuget.org/v3-flatcontainer/dotcraft.harness/index.json"
try {
$index = Invoke-RestMethod -Uri $indexUrl
$versions = @($index.versions)
}
catch {
$statusCode = 0
if ($_.Exception.Response -and $_.Exception.Response.StatusCode) {
$statusCode = [int]$_.Exception.Response.StatusCode
}
if ($statusCode -eq 404) {
$versions = @()
}
else {
throw
}
}
if ($versions -contains $version.ToLowerInvariant()) {
throw "DotCraft.Harness $version already exists on nuget.org."
}
"version=$version" >> $env:GITHUB_OUTPUT
Write-Host "Validated DotCraft.Harness $version ($channel) for $($env:TARGET)."
- name: Restore
run: dotnet restore ./tests/DotCraft.Harness.Tests/DotCraft.Harness.Tests.csproj
- name: Build
shell: pwsh
env:
PACKAGE_VERSION: ${{ steps.validate.outputs.version }}
run: |
$ErrorActionPreference = "Stop"
dotnet build ./tests/DotCraft.Harness.Tests/DotCraft.Harness.Tests.csproj `
--configuration Release `
--no-restore `
/p:Version=$env:PACKAGE_VERSION `
/p:PackageVersion=$env:PACKAGE_VERSION `
/p:ContinuousIntegrationBuild=true
- name: Test
run: dotnet test ./tests/DotCraft.Harness.Tests/DotCraft.Harness.Tests.csproj --configuration Release --no-build
- name: Pack release artifacts
shell: pwsh
env:
PACKAGE_VERSION: ${{ steps.validate.outputs.version }}
run: |
$ErrorActionPreference = "Stop"
New-Item -ItemType Directory -Force -Path artifacts/harness-nuget | Out-Null
dotnet pack ./src/DotCraft.Harness/DotCraft.Harness.csproj `
--configuration Release `
--no-build `
--output artifacts/harness-nuget `
/p:Version=$env:PACKAGE_VERSION `
/p:PackageVersion=$env:PACKAGE_VERSION `
/p:ContinuousIntegrationBuild=true
- name: Test Harness consumer
run: dotnet run --project ./tests/DotCraft.Harness.Consumer/DotCraft.Harness.Consumer.csproj --configuration Release
- name: Upload package artifacts
uses: actions/upload-artifact@v4
with:
name: dotcraft-harness-${{ steps.validate.outputs.version }}
path: artifacts/harness-nuget/*.nupkg
if-no-files-found: error
publish:
name: Publish to nuget.org
needs: pack
if: ${{ inputs.target == 'nuget-org' }}
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Download package artifacts
uses: actions/download-artifact@v4
with:
name: dotcraft-harness-${{ needs.pack.outputs.version }}
path: artifacts/harness-nuget
- name: Validate NuGet trusted publishing user
shell: pwsh
env:
NUGET_USER: ${{ vars.NUGET_USER }}
run: |
if ([string]::IsNullOrWhiteSpace($env:NUGET_USER)) {
throw "Set NUGET_USER to the nuget.org username that created the Trusted Publishing policy. Do not use the GitHubActions publisher label or an email address."
}
- name: NuGet login through Trusted Publishing
id: login
uses: NuGet/login@v1
with:
user: ${{ vars.NUGET_USER }}
- name: Push exact package artifact
shell: pwsh
env:
PACKAGE_VERSION: ${{ needs.pack.outputs.version }}
NUGET_API_KEY: ${{ steps.login.outputs.NUGET_API_KEY }}
run: |
$ErrorActionPreference = "Stop"
$package = "artifacts/harness-nuget/DotCraft.Harness.$env:PACKAGE_VERSION.nupkg"
dotnet nuget push $package `
--source https://api.nuget.org/v3/index.json `
--api-key $env:NUGET_API_KEY