From 789c07058696286bcf98d0c4ec850bf820c1bab8 Mon Sep 17 00:00:00 2001 From: Bruno Brito Date: Tue, 17 Nov 2026 21:40:59 -0300 Subject: [PATCH] ci: publish to NuGet with trusted publishing nuget.org API keys expire and the org key already did, which made the last releases fail with 403. Publish with NuGet/login (OIDC) instead: the job gets a short-lived key from the trusted publishing policy for this repository and workflow file. Also adds a workflow_dispatch input to publish an existing release tag again, since re-running the job skips the push once the release exists. --- .github/workflows/publish-package.yml | 40 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index c49961f..9784c80 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -3,10 +3,21 @@ name: Master - Publish packages on: push: branches: [ master ] + workflow_dispatch: + inputs: + version: + description: Existing release to publish again (e.g. 8.0.1). Builds tag v and pushes it to nuget.org. + required: true env: CURRENT_REPO_URL: https://github.com/${{ github.repository }} +permissions: + contents: write + issues: write + pull-requests: write + id-token: write # NuGet trusted publishing (OIDC) + jobs: build: runs-on: ubuntu-latest @@ -14,7 +25,9 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v3 - + with: + ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }} + - name: Setup .NET 8 uses: actions/setup-dotnet@v3 with: @@ -24,7 +37,7 @@ jobs: uses: actions/setup-dotnet@v3 with: dotnet-version: 9.0.x - + - name: Setup .NET 10 uses: actions/setup-dotnet@v3 with: @@ -32,6 +45,7 @@ jobs: - name: Semantic Release id: semantic + if: github.event_name == 'push' uses: cycjimmy/semantic-release-action@v4 with: extra_plugins: | @@ -40,8 +54,26 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Resolve version + id: version + if: github.event_name == 'workflow_dispatch' || steps.semantic.outputs.new_release_published == 'true' + env: + VERSION: ${{ inputs.version || steps.semantic.outputs.new_release_version }} + run: | + [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || { echo "::error::Invalid version '$VERSION'"; exit 1; } + echo "value=$VERSION" >> "$GITHUB_OUTPUT" + - name: Generate Package - run: dotnet pack -c Release -o out -p:PackageVersion=${{ steps.semantic.outputs.new_release_version }} -p:RepositoryUrl=${{env.CURRENT_REPO_URL}} + if: steps.version.outputs.value != '' + run: dotnet pack -c Release -o out -p:PackageVersion=${{ steps.version.outputs.value }} -p:RepositoryUrl=${{env.CURRENT_REPO_URL}} + + - name: NuGet login (trusted publishing) + id: nuget + if: steps.version.outputs.value != '' + uses: NuGet/login@v1 + with: + user: ${{ vars.NUGET_USER }} - name: Publish the package to nuget.org - run: dotnet nuget push ./out/*.nupkg --skip-duplicate -n -d -k ${{ secrets.NUGET_AUTH_TOKEN}} -s https://api.nuget.org/v3/index.json + if: steps.version.outputs.value != '' + run: dotnet nuget push ./out/*.nupkg --skip-duplicate -n -d -k ${{ steps.nuget.outputs.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json