Skip to content

ci: publish .NET SDK packages with NuGet trusted publishing, only when the version is new - #894

Merged
vadim-korolik merged 3 commits into
mainfrom
termag-vkorolik/cmwl8thxv0hs37bf97g5455aw-dotnet-nuget
Nov 18, 2026
Merged

vadim-korolik merged 3 commits into
mainfrom
termag-vkorolik/cmwl8thxv0hs37bf97g5455aw-dotnet-nuget

Conversation

@vadim-korolik

@vadim-korolik vadim-korolik commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Demo

Before/fix/after transcript: https://app.termag.ldinfra.net/termag/files/vkorolik-observability-sdk/artifacts/ci-devskim-dotnet/dotnet-demo.md

Why main fails

Both jobs of .NET SDK have failed at "Push generated package to NuGet registry" on every push to main since 2026-04-16. The last green run was 2026-04-15, and no .NET code changed between the two.

  • .github/workflows/dotnet.yml:35-39 (ASP.NET Core) and :67-70 (ASP.NET4) push on every main push. The versions they push are Highlight.ASPCore 0.2.16 (published 2025-03-25) and Highlight.ASP4 0.2.10, both already on nuget.org. So each push was a duplicate, and --skip-duplicate / -SkipDuplicate turned nuget.org's 409 into a pass.
  • The NUGET_API_KEY repo secret was last updated 2025-04-16T19:49Z. The first failure was 2026-04-16T21:02Z, which matches nuget.org's 365-day maximum key lifetime. nuget.org rejects an expired key before it checks for duplicates, so the push exits 1.

Change

  • Only push new versions. A new step, "Check whether the package version is already on NuGet" (.github/scripts/nuget-needs-publish.sh), looks up each built .nupkg's id and version (symbols skipped) in nuget.org's flat-container index. It writes publish=true only when a version isn't published yet. Pushes to main that don't bump a version no longer touch NuGet.
  • Trusted publishing instead of NUGET_API_KEY, as in NuGet's docs:
    • Both jobs get permissions: contents: read, id-token: write.
    • A new step, NuGet/login@8d196754b4036150537f80ac539e15c2f1028841 # v1.2.0 with user: ${{ secrets.NUGET_USER }}, runs only when a push will happen. This is the docs' NuGet/login@v1, pinned by SHA like the repo's other third-party actions (SEC-7924). It trades the job's GitHub OIDC token for a nuget.org API key that lasts one hour.
    • The push steps take that key from steps.login.outputs.NUGET_API_KEY through an env var. ASP.NET Core uses dotnet nuget push --api-key "$NUGET_API_KEY". ASP.NET4 keeps its nuget push bin\**\*.nupkg command (now -ApiKey $env:NUGET_API_KEY). I dropped -SymbolApiKey because the project builds no symbol packages.
    • dotnet.yml no longer references secrets.NUGET_API_KEY. I didn't delete the secret.
  • No other workflow in the repo publishes to NuGet, so there was nothing to stay consistent with. observability-dotnet has no NuGet release workflow here.
  • No package versions or release-please files changed.

Verification

  • PR run 44205999788 passed on both jobs (Linux and Windows). Notices show highlight.aspcore 0.2.16 and highlight.asp4 0.2.10 are already on nuget.org. Both the "NuGet login" and "Push" steps were skipped, and nothing was published.
  • actionlint 1.7.12 reports only the custom runner label, which main also has.
  • After switching to secrets.NUGET_USER (3c4e6f9): PR run 44207123511 passed on both jobs, with login and push skipped. actionlint is unchanged.
  • Not exercised yet: the OIDC login and push only run on main when a version is new, so they can't be tested until the next version bump of Highlight.ASPCore or Highlight.ASP4. Before that bump, do the setup below. Otherwise the first push fails at "NuGet login".

Setup needed on nuget.org

These must match .github/workflows/dotnet.yml exactly, or NuGet/login fails (fields are case-insensitive).

  • Sign in to nuget.org as a member of the Highlight organization, which owns both Highlight.ASPCore and Highlight.ASP4. Then choose your username → Trusted Publishing → add a GitHub Actions policy:
    • Policy owner: Highlight. The policy covers every package that owner has, which includes Highlight.ASPCore and Highlight.ASP4.
    • Repository Owner: launchdarkly
    • Repository: observability-sdk
    • Workflow File: dotnet.yml (file name only, no .github/workflows/ path)
    • Environment: leave empty. Neither job sets environment:.
  • Repository secret NUGET_USER (Settings → Secrets and variables → Actions → Secrets), set 2026-11-18. It holds the nuget.org username (profile name, not email) of the account that created the policy. The workflow passes it as user: ${{ secrets.NUGET_USER }} to NuGet/login, as NuGet's docs recommend.
  • Don't rename dotnet.yml. Renaming it breaks the policy.
  • After the first successful trusted-publishing push, delete the expired NUGET_API_KEY repo secret. Nothing in the repo uses it any more, and this PR leaves it in place.

Related

The DevSkim half of this ask: #893.

Asked by vkorolik through termag ask; follow up with termag ask … --thread cmwl8thxv0hs37bf97g5455aw.

🤖 Generated with Claude Code

Opened from termag

Both jobs re-push the same Highlight.ASPCore 0.2.16 / Highlight.ASP4
0.2.10 packages on every main push and relied on --skip-duplicate. Since
2026-04-16 nuget.org rejects the push before the duplicate check because
NUGET_API_KEY (last set 2025-04-16) hit nuget.org's 365-day key expiry,
so the job fails on every push. Check nuget.org's flat container first
and only push when a package version is not published yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generated-by: termag
@vadim-korolik

Copy link
Copy Markdown
Contributor Author

bugbot run

@vadim-korolik
vadim-korolik marked this pull request as ready for review November 17, 2026 22:36
@vadim-korolik
vadim-korolik requested a review from a team as a code owner November 17, 2026 22:36
Replace the expired long-lived NUGET_API_KEY secret with nuget.org trusted
publishing: when a new version needs pushing, NuGet/login (v1.2.0, pinned
by SHA) exchanges the job's GitHub OIDC token for a one-hour API key, which
the push step uses. Both jobs get permissions contents: read and
id-token: write. The nuget.org profile name comes from the NUGET_USER repo
variable. The "only push when the version is not on nuget.org" check is
kept, and login runs only when a push will happen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generated-by: termag
@vadim-korolik vadim-korolik changed the title ci: only push .NET SDK packages to NuGet when the version is not published ci: publish .NET SDK packages with NuGet trusted publishing, only when the version is new Nov 18, 2026
The nuget.org profile name is stored as the NUGET_USER repository secret
(as NuGet's trusted publishing docs suggest), not a variable, so
vars.NUGET_USER would pass an empty user to NuGet/login.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generated-by: termag
@vadim-korolik vadim-korolik added the merge-ready Approved, CI green, Bugbot clean, no open threads label Nov 18, 2026
@vadim-korolik
vadim-korolik merged commit 004bb80 into main Nov 18, 2026
29 checks passed
@vadim-korolik
vadim-korolik deleted the termag-vkorolik/cmwl8thxv0hs37bf97g5455aw-dotnet-nuget branch November 18, 2026 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Approved, CI green, Bugbot clean, no open threads

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants