From 958ae8acee221fa9cbf9bbf0bbe494dce74c4b7b Mon Sep 17 00:00:00 2001 From: "Jose A. Fernandez" Date: Sun, 22 Nov 2026 20:48:41 -0300 Subject: [PATCH] Add NuGet trusted publishing workflow --- .github/workflows/publish-nuget.yml | 107 ++++++++++++++++++++++++++++ README.md | 15 ++++ 2 files changed, 122 insertions(+) create mode 100644 .github/workflows/publish-nuget.yml diff --git a/.github/workflows/publish-nuget.yml b/.github/workflows/publish-nuget.yml new file mode 100644 index 0000000..a34a9a3 --- /dev/null +++ b/.github/workflows/publish-nuget.yml @@ -0,0 +1,107 @@ +name: Publish to NuGet + +on: + push: + tags: + - 'v*.*.*' + +permissions: + contents: read + +jobs: + package: + if: ${{ !github.event.deleted }} + runs-on: windows-latest + outputs: + version: ${{ steps.version.outputs.version }} + + steps: + - name: Validate release tag + id: version + shell: pwsh + run: | + $number = '(?:0|[1-9][0-9]*)' + $identifier = "(?:$number|[0-9]*[A-Za-z-][0-9A-Za-z-]*)" + if ($env:GITHUB_REF_NAME -cnotmatch "\Av(?$number\.$number\.$number(?:-$identifier(?:\.$identifier)*)?)\z") { + throw 'Use a SemVer tag without build metadata, such as v2.0.0 or v2.0.0-rc.1.' + } + "version=$($Matches.version)" >> $env:GITHUB_OUTPUT + + - name: Checkout + uses: actions/checkout@v5 + with: + persist-credentials: false + + - name: Setup .NET SDKs + uses: actions/setup-dotnet@v5 + with: + dotnet-version: | + 10.0.x + 11.0.x + + - name: Restore, build and test + shell: pwsh + env: + RELEASE_VERSION: ${{ steps.version.outputs.version }} + run: | + $PSNativeCommandUseErrorActionPreference = $true + $tests = 'src/ColorHashSharp.Tests/ColorHashSharp.Tests.csproj' + $library = 'src/ColorHashSharp/ColorHashSharp.csproj' + dotnet restore $tests + dotnet build $library --configuration Release --no-restore -p:Version=$env:RELEASE_VERSION -p:GeneratePackageOnBuild=false + dotnet test $tests --configuration Release --no-restore -p:Version=$env:RELEASE_VERSION -p:GeneratePackageOnBuild=false + + - name: Pack + shell: pwsh + env: + RELEASE_VERSION: ${{ steps.version.outputs.version }} + run: dotnet pack src/ColorHashSharp/ColorHashSharp.csproj --configuration Release --no-build --output artifacts -p:Version=$env:RELEASE_VERSION -p:PackageVersion=$env:RELEASE_VERSION + + - name: Upload package + uses: actions/upload-artifact@v4 + with: + name: nuget-package + path: artifacts/ColorHashSharp.${{ steps.version.outputs.version }}.nupkg + if-no-files-found: error + + publish: + needs: package + runs-on: ubuntu-latest + environment: nuget + permissions: + id-token: write + + steps: + - name: Check NuGet configuration + shell: bash + env: + NUGET_USER: ${{ vars.NUGET_USER }} + run: | + if [[ -z "$NUGET_USER" ]]; then + echo "::error::Set the NUGET_USER variable in the nuget environment." + exit 1 + fi + + - name: Setup .NET SDK + uses: actions/setup-dotnet@v5 + with: + dotnet-version: 11.0.x + + - name: Download package + uses: actions/download-artifact@v4 + with: + name: nuget-package + path: artifacts + + - name: Authenticate to NuGet.org + uses: NuGet/login@v1 + id: login + with: + user: ${{ vars.NUGET_USER }} + + - name: Publish package + shell: bash + env: + NUGET_API_KEY: ${{ steps.login.outputs.NUGET_API_KEY }} + RELEASE_VERSION: ${{ needs.package.outputs.version }} + run: dotnet nuget push "artifacts/ColorHashSharp.${RELEASE_VERSION}.nupkg" --source https://api.nuget.org/v3/index.json --api-key "$NUGET_API_KEY" diff --git a/README.md b/README.md index 09f0ca5..7bd634e 100644 --- a/README.md +++ b/README.md @@ -82,3 +82,18 @@ An empty `HueRanges` list uses the default hue calculation. Each range requires `0 <= Min <= Max <= 360`. Invalid settings raise `ArgumentException` at construction. A null input string is treated as empty, including when passed to a custom hash. + +### Publishing to NuGet.org + +[Publish to NuGet](.github/workflows/publish-nuget.yml) builds and tests each release before packing it. The package version comes from the tag without its leading `v`. Use a stable tag such as `v2.0.0` or a prerelease tag such as `v2.0.0-rc.1`. Omit build metadata (`+...`) because NuGet.org normalizes it out of version identifiers. + +1. In GitHub, open **Settings → Environments → nuget**. Under **Environment variables**, set `NUGET_USER` to the NuGet.org profile name `fernandezja`. Under **Deployment branches and tags**, select **Selected branches and tags** and add a **Tag** rule for `v*.*.*`. +2. On [NuGet.org → Trusted Publishing](https://www.nuget.org/account/trustedpublishing), add a GitHub Actions policy named `ColorHashSharp GitHub Actions` for the package owner. Set **Repository Owner** to `fernandezja`, **Repository** to `ColorHashSharp`, **Workflow File** to `publish-nuget.yml`, and **Environment** to `nuget`. Select **Push only new package versions** and enter `ColorHashSharp` under **Glob Patterns and Packages**. The workflow exchanges its OIDC token for a short-lived API key when the package is ready to publish. +3. After the workflow is merged, tag the release commit from `main` with an unused package version and push the tag. For example: + + ```sh + git tag -a v2.0.0 -m "ColorHashSharp 2.0.0" + git push origin v2.0.0 + ``` + + Check **Actions → Publish to NuGet** for the result, then verify the version on the [ColorHashSharp package page](https://www.nuget.org/packages/ColorHashSharp/).