diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8fc5d81..67cc803 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -124,6 +124,13 @@ jobs: if: github.event_name == 'release' runs-on: ubuntu-latest needs: [ validate_nuget, run_test ] + + # Job-level, so it must restate contents: read — a job-level block REPLACES the + # workflow-level one rather than adding to it. + permissions: + contents: read + id-token: write # request the GitHub OIDC token for NuGet Trusted Publishing + steps: # Download the NuGet package created in the previous job - uses: actions/download-artifact@v8 @@ -140,8 +147,21 @@ jobs: # Publish all NuGet packages to NuGet.org # Use --skip-duplicate to prevent errors if a package with the same version already exists. # If you retry a failed workflow, already published packages will be skipped without error. + # Trusted Publishing: exchanges the OIDC token (needs `id-token: write` above) for a NuGet + # key valid ~1 hour, so no long-lived secret is stored. Keep it adjacent to the push so the + # key cannot expire in between. Requires a Trusted Publishing policy on nuget.org naming + # this repository and `main.yml`, plus the NUGET_USER secret (the nuget.org profile name). + # Deliberately UNGUARDED: this job only runs on a published Release, so publishing is + # expected. A missing policy must fail loudly rather than skip and leave the release + # unpublished behind a green check. + - name: NuGet login (OIDC -> short-lived key) + id: nuget-login + uses: NuGet/login@8d196754b4036150537f80ac539e15c2f1028841 # v1.2.0 + with: + user: ${{ secrets.NUGET_USER }} + - name: Publish NuGet package run: | foreach($file in (Get-ChildItem "${{ env.NuGetDirectory }}" -Recurse -Include *.nupkg)) { - dotnet nuget push $file --api-key "${{ secrets.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate + dotnet nuget push $file --api-key "${{ steps.nuget-login.outputs.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate } \ No newline at end of file