Migrate NuGet publishing to Trusted Publishing for enhanced security #1358
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR migrates the NuGet package publishing workflow from API key-based authentication to Trusted Publishing, following Microsoft's recommended security best practices for NuGet.org.
Problem
The current release workflow uses long-lived API keys stored as GitHub secrets (
NUGET_KEY
) to publish NuGet packages. This approach has several security and operational concerns:Solution
Replaced the manual
dotnet nuget push
command with theazure/publish-nuget@v1
action that leverages OpenID Connect (OIDC) tokens for authentication. This implementation:Changes Made
Added OIDC Permissions to the CLI job:
Replaced Publishing Step:
dotnet nuget push **/*.nupkg --api-key ${{ secrets.NUGET_KEY }} --source ${{ env.NUGET_REPO_URL }}
azure/publish-nuget@v1
action with package path configurationRemoved Unused Variables: Cleaned up
NUGET_REPO_URL
environment variable (no longer needed)Added Documentation: Comprehensive comments in the workflow explaining Trusted Publishing setup requirements
How It Works
azure/publish-nuget
action sends the token to NuGet.orgSecurity Benefits
✅ No Long-Lived Secrets: Eliminates the need to store API keys as repository secrets
✅ Short-Lived Credentials: OIDC tokens expire automatically after use
✅ Scoped Access: Publishing credentials are tied to specific workflow and repository
✅ Automatic Rotation: No manual credential management required
✅ Better Compliance: Aligns with Microsoft and NuGet.org security recommendations
✅ Improved Auditability: Enhanced tracking of publishing operations
Required Setup (One-Time)
After merging this PR, the repository owner needs to configure Trusted Publishing on nuget.org:
christianhelle
apiclientcodegen
release.yml
Once configured, the workflow will automatically obtain secure, short-lived credentials without requiring the
NUGET_KEY
secret.Validation
master
andrelease
branchescontinue-on-error: true
preserved for graceful failure handlingReferences
Fixes #<issue_number>
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.