@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 name: Release NuGet
2
3 # Publishes SiGit.Cli, a .NET tool that bundles the native `si` binaries, on the same tag that
4 # release-crates.yml publishes from.
5 #
6 # Auth is NuGet Trusted Publishing (OIDC), so no long-lived API key is stored. nuget.org needs a
7 # trusted publishing policy for this repository and workflow (release-nuget.yml), and the
8 # NUGET_USER repo secret must hold the nuget.org profile name that owns the package.
9
10 on:
11 push:
12 tags:
13 - "v*.*.*"
14 workflow_dispatch:
15 inputs:
16 tag:
17 description: "Release tag (e.g. v1.3.0)"
18 required: true
19
20 concurrency:
21 # A re-pushed or re-dispatched tag fires this a second time. Queue the
22 # duplicate behind the original instead of cancelling it: cancelling a run
23 # mid-publish can leave a channel half-uploaded, whereas a queued duplicate
24 # just hits the "already published" check and exits clean.
25 group: ${{ github.workflow }}-${{ github.event.inputs.tag || github.ref_name }}
26 cancel-in-progress: false
27
28 permissions:
29 contents: read
30
31 env:
32 CARGO_TERM_COLOR: always
33 DOTNET_VERSION: 8.0.x
34 NUGET_PACKAGE_ID: SiGit.Cli
35
36 jobs:
37 # The binary reports CARGO_PKG_VERSION, so a tag that disagrees with the workspace version
38 # would publish a package that lies about its version.
39 verify-version:
40 runs-on: ubuntu-latest
41 steps:
42 - uses: actions/checkout@v6
43 with:
44 ref: ${{ github.event.inputs.tag || github.ref }}
45 - name: Tag matches the workspace version
46 shell: bash
47 env:
48 TAG: ${{ github.event.inputs.tag || github.ref_name }}
49 run: |
50 workspace=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)
51 if [ "$TAG" != "v$workspace" ]; then
52 echo "::error::tag $TAG does not match the workspace version $workspace"
53 exit 1
54 fi
55
56 build-native-binaries:
57 name: Build native binary (${{ matrix.build.NAME }})
58 needs: verify-version
59 runs-on: ${{ matrix.build.OS }}
60 strategy:
61 fail-fast: false
62 matrix:
63 build:
64 - {
65 NAME: linux-x64,
66 OS: ubuntu-22.04,
67 TARGET: x86_64-unknown-linux-gnu,
68 }
69 - {
70 NAME: linux-arm64,
71 OS: ubuntu-22.04-arm,
72 TARGET: aarch64-unknown-linux-gnu,
73 }
74 - {
75 NAME: windows-x64,
76 OS: windows-2022,
77 TARGET: x86_64-pc-windows-msvc,
78 }
79 - {
80 NAME: windows-arm64,
81 OS: windows-2022,
82 TARGET: aarch64-pc-windows-msvc,
83 }
84 - { NAME: darwin-x64, OS: macos-26, TARGET: x86_64-apple-darwin }
85 - { NAME: darwin-arm64, OS: macos-26, TARGET: aarch64-apple-darwin }
86 steps:
87 - name: Checkout
88 uses: actions/checkout@v6
89 with:
90 ref: ${{ github.event.inputs.tag || github.ref }}
91
92 - name: Read Rust toolchain
93 shell: bash
94 run: |
95 rust_toolchain="$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml | head -n 1)"
96 if [ -z "$rust_toolchain" ]; then
97 echo "Failed to read Rust toolchain from rust-toolchain.toml" >&2
98 exit 1
99 fi
100 echo "RUST_TOOLCHAIN=${rust_toolchain}" >> "$GITHUB_ENV"
101
102 - name: Install Rust toolchain
103 uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9
104 with:
105 toolchain: ${{ env.RUST_TOOLCHAIN }}
106
107 - name: Install Rust target
108 shell: bash
109 run: |
110 rustup target add ${{ matrix.build.TARGET }} --toolchain ${{ env.RUST_TOOLCHAIN }}
111 rustup target list --installed
112
113 - name: Setup Rust cache
114 uses: Swatinem/rust-cache@v2
115 with:
116 key: nuget-${{ matrix.build.TARGET }}
117
118 # Linux builds on the oldest runner image for its glibc floor (2.35). The repo uses rustls,
119 # so there is no OpenSSL to link or vendor.
120 - name: Build binary
121 shell: bash
122 run: cargo build --locked --release -p sigit-si-cli --bin si --target ${{ matrix.build.TARGET }}
123
124 # The exe must not import the VC++ runtime DLLs (see .cargo/config.toml), or it fails to
125 # load on a machine without the redistributable.
126 - name: Check static CRT
127 if: contains(matrix.build.TARGET, 'windows')
128 shell: pwsh
129 run: ./packaging/windows/check-static-crt.ps1 -Path target/${{ matrix.build.TARGET }}/release/si.exe
130
131 - name: Prepare artifact
132 shell: bash
133 env:
134 BUILD_NAME: ${{ matrix.build.NAME }}
135 run: |
136 executable_name="si"
137 if [[ "${BUILD_NAME}" == windows-* ]]; then
138 executable_name="si.exe"
139 fi
140
141 mkdir -p "nuget-artifacts/native/${{ matrix.build.NAME }}"
142 cp "target/${{ matrix.build.TARGET }}/release/${executable_name}" \
143 "nuget-artifacts/native/${{ matrix.build.NAME }}/${executable_name}"
144
145 - name: Upload native artifact
146 uses: actions/upload-artifact@v4
147 with:
148 name: nuget-native-${{ matrix.build.NAME }}
149 path: nuget-artifacts/native
150
151 pack-nuget:
152 name: Pack NuGet .NET tool
153 runs-on: ubuntu-latest
154 needs: build-native-binaries
155 steps:
156 - name: Checkout
157 uses: actions/checkout@v6
158 with:
159 ref: ${{ github.event.inputs.tag || github.ref }}
160
161 - name: Set the release version
162 shell: bash
163 run: |
164 if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
165 release_version="${GITHUB_REF_NAME#v}"
166 else
167 release_version="${{ github.event.inputs.tag }}"
168 release_version="${release_version#v}"
169 fi
170 echo "RELEASE_VERSION=${release_version}" >> "$GITHUB_ENV"
171
172 - name: Download native artifacts
173 uses: actions/download-artifact@v4
174 with:
175 pattern: nuget-native-*
176 path: packaging/nuget/si/native
177 merge-multiple: true
178
179 - name: Setup .NET SDK
180 uses: actions/setup-dotnet@v4
181 with:
182 dotnet-version: ${{ env.DOTNET_VERSION }}
183
184 - name: Pack .NET tool
185 shell: bash
186 run: |
187 dotnet pack packaging/nuget/si/SiGit.Cli.csproj \
188 --configuration Release \
189 --output packaging/nuget/dist \
190 -p:PackageVersion="${RELEASE_VERSION}"
191
192 # All six binaries ship in one package, so this sits closer to nuget.org's
193 # 250 MB ceiling than the single-platform npm and PyPI artifacts do. Fail
194 # here with a clear message rather than at `dotnet nuget push`, where the
195 # error is just "the package file exceeds the size limit".
196 - name: Check package size against the NuGet limit
197 shell: bash
198 run: |
199 package="$(find packaging/nuget/dist -name '*.nupkg' | head -n 1)"
200 size_bytes="$(wc -c < "${package}")"
201 limit_bytes=$((250 * 1024 * 1024))
202
203 echo "Package: ${package}"
204 echo "Size: $((size_bytes / 1024 / 1024)) MiB"
205
206 if [ "${size_bytes}" -gt "${limit_bytes}" ]; then
207 echo "::error::${package} is over nuget.org's 250 MB package limit. Split the tool into RID-specific packages (.NET 10 DotnetToolRidPackage) instead of bundling every target." >&2
208 exit 1
209 fi
210
211 - name: Upload package artifact
212 uses: actions/upload-artifact@v4
213 with:
214 name: nuget-package
215 path: packaging/nuget/dist/*.nupkg
216
217 smoke-test:
218 name: Smoke test (${{ matrix.os }})
219 runs-on: ${{ matrix.os }}
220 needs: pack-nuget
221 strategy:
222 fail-fast: false
223 matrix:
224 os:
225 # The oldest image, so the smoke test runs the Linux binary against the glibc floor.
226 - ubuntu-22.04
227 - windows-2022
228 - macos-26
229 steps:
230 - name: Checkout
231 uses: actions/checkout@v6
232 with:
233 ref: ${{ github.event.inputs.tag || github.ref }}
234
235 - name: Set the release version
236 shell: bash
237 run: |
238 if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
239 release_version="${GITHUB_REF_NAME#v}"
240 else
241 release_version="${{ github.event.inputs.tag }}"
242 release_version="${release_version#v}"
243 fi
244 echo "RELEASE_VERSION=${release_version}" >> "$GITHUB_ENV"
245
246 - name: Download package artifact
247 uses: actions/download-artifact@v4
248 with:
249 name: nuget-package
250 path: packaging/nuget/dist
251
252 - name: Setup .NET SDK
253 uses: actions/setup-dotnet@v4
254 with:
255 dotnet-version: ${{ env.DOTNET_VERSION }}
256
257 - name: Install tool from local package
258 shell: pwsh
259 run: |
260 $packageSource = (Resolve-Path "packaging/nuget/dist").Path
261 @"
262 <?xml version="1.0" encoding="utf-8"?>
263 <configuration>
264 <packageSources>
265 <clear />
266 <add key="local" value="$packageSource" />
267 </packageSources>
268 </configuration>
269 "@ | Set-Content -Path "packaging/nuget/NuGet.Config"
270
271 dotnet tool install `
272 --tool-path ".tool" `
273 --configfile "packaging/nuget/NuGet.Config" `
274 --version "$env:RELEASE_VERSION" `
275 "$env:NUGET_PACKAGE_ID"
276
277 - name: Verify bundled native binary
278 shell: pwsh
279 run: |
280 $runtimeIdentifier = if ($IsWindows) {
281 if ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { "windows-arm64" } else { "windows-x64" }
282 } elseif ($IsMacOS) {
283 if ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { "darwin-arm64" } else { "darwin-x64" }
284 } else {
285 if ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { "linux-arm64" } else { "linux-x64" }
286 }
287
288 $binaryName = if ($IsWindows) { "si.exe" } else { "si" }
289 $nativeBinary = Get-ChildItem ".tool/.store" -Recurse -File | Where-Object {
290 $_.FullName -like "*native*${runtimeIdentifier}*${binaryName}"
291 } | Select-Object -First 1
292
293 if (-not $nativeBinary) {
294 throw "The si native binary for ${runtimeIdentifier} was not bundled in the installed .NET tool package."
295 }
296
297 Write-Host "Found bundled native binary at $($nativeBinary.FullName)"
298
299 # Finding the file proves it was packed, not that it loads. Running it catches a missing
300 # runtime DLL or a glibc mismatch before the package is published.
301 - name: Run the installed tool
302 shell: pwsh
303 run: |
304 $tool = if ($IsWindows) { ".tool/si.exe" } else { ".tool/si" }
305 $output = & $tool --version
306 if ($LASTEXITCODE -ne 0) {
307 throw "si --version exited with code $LASTEXITCODE"
308 }
309 Write-Host $output
310 if (($output -join " ") -notmatch [regex]::Escape($env:RELEASE_VERSION)) {
311 throw "si --version did not report $env:RELEASE_VERSION"
312 }
313
314 publish-nuget:
315 name: Publish to NuGet
316 runs-on: ubuntu-latest
317 needs: smoke-test
318 # Trusted Publishing exchanges a GitHub OIDC token for a short-lived NuGet
319 # API key, so no long-lived NUGET_API_KEY secret is stored. Requires a
320 # trusted publishing policy on nuget.org for this repository and workflow
321 # (release-nuget.yml); without one the login step fails.
322 permissions:
323 contents: read
324 id-token: write
325 steps:
326 - name: Set the release version
327 shell: bash
328 run: |
329 if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
330 release_version="${GITHUB_REF_NAME#v}"
331 else
332 release_version="${{ github.event.inputs.tag }}"
333 release_version="${release_version#v}"
334 fi
335 echo "RELEASE_VERSION=${release_version}" >> "$GITHUB_ENV"
336
337 - name: Download package artifact
338 uses: actions/download-artifact@v4
339 with:
340 name: nuget-package
341 path: packaging/nuget/dist
342
343 - name: Setup .NET SDK
344 uses: actions/setup-dotnet@v4
345 with:
346 dotnet-version: ${{ env.DOTNET_VERSION }}
347
348 - name: Check whether release already exists on NuGet
349 id: nuget-check
350 shell: bash
351 run: |
352 package_index_url="https://api.nuget.org/v3-flatcontainer/sigit.cli/index.json"
353
354 # Keep the fetch and the version match separate. Piping curl into
355 # grep hides a failed fetch behind an empty match, so an outage or a
356 # blocked request would read as "not published yet" and send the
357 # publish below into a version that is already there. A 404 here
358 # means the package itself is not on NuGet, which is the first
359 # release and a real "not published".
360 index=$(mktemp)
361 STATUS=$(curl -sS -o "${index}" -w '%{http_code}' \
362 --retry 3 --retry-delay 2 --retry-all-errors "${package_index_url}")
363
364 case "$STATUS" in
365 200)
366 if grep -F "\"${RELEASE_VERSION}\"" "${index}" >/dev/null; then
367 echo "${NUGET_PACKAGE_ID} ${RELEASE_VERSION} already exists on NuGet, skipping publish"
368 echo "exists=true" >> "$GITHUB_OUTPUT"
369 else
370 echo "exists=false" >> "$GITHUB_OUTPUT"
371 fi
372 ;;
373 404)
374 echo "${NUGET_PACKAGE_ID} is not on NuGet yet, publishing its first version"
375 echo "exists=false" >> "$GITHUB_OUTPUT"
376 ;;
377 *)
378 echo "Unexpected ${STATUS} from NuGet while checking whether ${NUGET_PACKAGE_ID} ${RELEASE_VERSION} is published" >&2
379 exit 1
380 ;;
381 esac
382
383 # Exchange the workflow's OIDC token for a short-lived NuGet API key. The
384 # key is written to the environment for the subsequent `dotnet nuget push`
385 # via the NUGET_API_KEY variable this action sets.
386 - name: NuGet trusted-publishing login
387 if: steps.nuget-check.outputs.exists != 'true'
388 uses: NuGet/login@v1
389 id: nuget-login
390 with:
391 user: ${{ secrets.NUGET_USER }}
392
393 - name: Publish package
394 if: steps.nuget-check.outputs.exists != 'true'
395 shell: bash
396 run: |
397 dotnet nuget push packaging/nuget/dist/*.nupkg \
398 --api-key "${{ steps.nuget-login.outputs.NUGET_API_KEY }}" \
399 --source https://api.nuget.org/v3/index.json \
400 --skip-duplicate