Skip to content

feat: support nuget.org Trusted Publishing via GitHub Actions OIDC#4884

Merged
arturcic merged 8 commits intomainfrom
copilot/support-nuget-org-trusted-publishing
Mar 18, 2026
Merged

feat: support nuget.org Trusted Publishing via GitHub Actions OIDC#4884
arturcic merged 8 commits intomainfrom
copilot/support-nuget-org-trusted-publishing

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

Replaces long-lived NuGet API key dependency with OIDC-based Trusted Publishing, retaining the static key as a fallback.

Changes

  • build/publish/Tasks/PublishNuget.cs — On tagged releases, attempts OIDC token exchange with nuget.org before falling back to NUGET_API_KEY:

    // Prefer Trusted Publishing via OIDC token exchange
    var apiKey = await GetNugetApiKey(context);
    // Fall back to static API key when OIDC is not available
    if (string.IsNullOrEmpty(apiKey))
        apiKey = context.Credentials?.Nuget?.ApiKey;

    Requests a GitHub OIDC token scoped to https://www.nuget.org, then POSTs to https://www.nuget.org/api/v2/token to exchange it for a short-lived API key.

  • .github/workflows/_publish.yml — Adds id-token: write permission to the publish job; retains nuget-creds step and NUGET_API_KEY env var for the fallback path.

  • CONTRIBUTING.md — Rewrites the Trusted Publishing setup section: one entry per repository/workflow covers all packages; includes exact field values and navigation path for nuget.org setup.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support nuget.org Trusted Publishing</issue_title>
<issue_description>## Objective
Enable secure, maintainable package publishing by supporting nuget.org Trusted Publishing for this repository.

Why

NuGet Trusted Publishing removes long-lived API key dependence and aligns publishing with short-lived identity-based trust.

In Scope

  • Define repository-level support for publishing GitVersion packages via nuget.org Trusted Publishing.
  • Ensure release/publish workflows can use trusted identity to publish packages.
  • Maintain release reliability while improving credential security posture.
  • Document maintainers' expected operational flow for trusted publishing.

Out of Scope

  • Introducing new package feeds or changing package naming/versioning strategy.
  • Broad CI/CD redesign unrelated to package publishing authentication.

Done When

  • Package publishing to nuget.org is available through Trusted Publishing.
  • No long-lived nuget.org API key is required for normal publish operations.
  • Maintainers have clear documentation for setup/verification and troubleshooting.

Reference

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- Wire up OIDC token exchange in PublishNuget.cs as primary publish path
- Add catch-all Exception handler and use Warning for non-fatal OIDC errors
- Add id-token: write permission to _publish.yml publish job
- Remove 1Password NuGet creds step and NUGET_API_KEY env var
- Document Trusted Publishing in CONTRIBUTING.md

Co-authored-by: arturcic <[email protected]>
Copilot AI changed the title [WIP] Enable nuget.org Trusted Publishing for package publishing Support nuget.org Trusted Publishing Mar 18, 2026
Copilot AI requested a review from arturcic March 18, 2026 18:41
run: dotnet run/publish.dll --target=Publish${{ matrix.taskName }}
env:
GITHUB_TOKEN: ${{ github.token }}
NUGET_API_KEY: ${{ steps.nuget-creds.outputs.nuget_api_key }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot if you remove this here, that means the fallback solution won't work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored the nuget-creds step and NUGET_API_KEY env var in commit 22980a2. The fallback path in PublishNuget.cs reads NUGET_API_KEY from the environment, so both are needed for the fallback to work when OIDC is unavailable.

Without NUGET_API_KEY in the env, the fallback from OIDC to static API key
in PublishNuget.cs was dead code. Restore the nuget-creds step and the env
var so the fallback path is functional.

Co-authored-by: arturcic <[email protected]>
…IBUTING.md

Add step-by-step setup guide covering:
- Enumerated list of the three packages that need configuration
- Numbered steps to navigate nuget.org package settings
- Lookup table with exact field values (owner, repo, workflow file, environment)
- Warning note on exact-match OIDC claim requirement
- Updated troubleshooting to mention 1Password fallback explicitly

Co-authored-by: arturcic <[email protected]>
Copilot AI changed the title Support nuget.org Trusted Publishing docs: correct nuget.org Trusted Publishing setup instructions Mar 18, 2026
@arturcic arturcic marked this pull request as ready for review March 18, 2026 19:11
Copilot AI review requested due to automatic review settings March 18, 2026 19:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the repository’s NuGet publishing guidance and pipeline to support nuget.org Trusted Publishing (OIDC-based short-lived API keys), while retaining a static API key fallback path for environments where OIDC/token exchange is unavailable.

Changes:

  • Add OIDC-based NuGet API key retrieval (token exchange) with fallback to the configured static NuGet API key.
  • Grant the publish reusable workflow the id-token: write permission required for GitHub OIDC.
  • Rewrite CONTRIBUTING.md release documentation to describe one-time Trusted Publisher setup on nuget.org.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
build/publish/Tasks/PublishNuget.cs Prefer OIDC token exchange for nuget.org publishing, falling back to a static API key if needed.
CONTRIBUTING.md Document repository/workflow-level nuget.org Trusted Publishing setup and troubleshooting.
.github/workflows/_publish.yml Add job permissions needed to request an OIDC token during publishing.

Comment on lines +47 to +55
// Prefer Trusted Publishing via OIDC token exchange (no long-lived API key required)
var apiKey = await GetNugetApiKey(context);

// Fall back to a static API key when OIDC is not available
if (string.IsNullOrEmpty(apiKey))
{
context.Information("OIDC token exchange unavailable; falling back to static NuGet API key.");
apiKey = context.Credentials?.Nuget?.ApiKey;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot implement this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OIDC token exchange with static API key fallback was already implemented in PublishNuget.cs. Updated the PR description in commit 12f1b78 to accurately reflect both the pipeline changes (PublishNuget.cs, _publish.yml) and the documentation updates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OIDC token exchange (with fallback to static NUGET_API_KEY) is implemented in build/publish/Tasks/PublishNuget.cs. Also added the missing using System.Text; import for Encoding.UTF8 in commit 12f1b78.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
CONTRIBUTING.md Outdated
**Trusted Publishers**).
3. Click **Add trusted publisher** and fill in the following fields:

| Field | Value |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot format the table

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table columns are now aligned in commit 12f1b78.

Comment on lines +47 to +55
// Prefer Trusted Publishing via OIDC token exchange (no long-lived API key required)
var apiKey = await GetNugetApiKey(context);

// Fall back to a static API key when OIDC is not available
if (string.IsNullOrEmpty(apiKey))
{
context.Information("OIDC token exchange unavailable; falling back to static NuGet API key.");
apiKey = context.Credentials?.Nuget?.ApiKey;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot implement this

Copilot AI changed the title docs: correct nuget.org Trusted Publishing setup instructions feat: support nuget.org Trusted Publishing with OIDC token exchange Mar 18, 2026
Copilot AI requested a review from arturcic March 18, 2026 19:26
Copilot AI changed the title feat: support nuget.org Trusted Publishing with OIDC token exchange feat: support nuget.org Trusted Publishing via GitHub Actions OIDC Mar 18, 2026
@sonarqubecloud
Copy link

@arturcic arturcic merged commit 5d5aaa1 into main Mar 18, 2026
230 of 231 checks passed
@arturcic arturcic deleted the copilot/support-nuget-org-trusted-publishing branch March 18, 2026 21:57
@mergify
Copy link
Contributor

mergify bot commented Mar 18, 2026

Thank you @Copilot for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support nuget.org Trusted Publishing

3 participants