Skip to content

ci: publish to NuGet via Trusted Publishing, not a long-lived key - #197

Merged
phmatray merged 1 commit into
devfrom
ci/nuget-trusted-publishing
Jul 27, 2026
Merged

ci: publish to NuGet via Trusted Publishing, not a long-lived key#197
phmatray merged 1 commit into
devfrom
ci/nuget-trusted-publishing

Conversation

@phmatray

Copy link
Copy Markdown
Owner

The publish job handed a long-lived NUGET_API_KEY to cake. It now uses Trusted Publishing: the GitHub OIDC token is exchanged for a NuGet key valid ~1 hour and that is passed through --nuget-key instead. The only remaining secret is NUGET_USER — the nuget.org profile name, not a credential.

build.cake is unchanged. Cake neither knows nor cares that the key is short-lived; it just receives one. That is what makes this migration cheap for tool-driven builds.

Two findings while reading the publish path

--github-key was dead. build.cake reads only nuget-key--github-key=${{ secrets.GITHUB_TOKEN }} was handing a token to a script that never asked for it. Dropped in this change.

The job had no permissions block at all, so it ran on repository defaults. It now declares the minimum: contents: read + id-token: write. I checked Publish-NuGet before narrowing it — it only pushes to nuget.org, so nothing needs write access. (Its inline comment says "Publish to GitHub Packages" but the source is api.nuget.org; stale comment, left alone as out of scope.)

Required before the next release

  1. Register a Trusted Publishing policy on nuget.org for MasterCommander, naming phmatray/MasterCommander and publish.yml.
  2. Set the NUGET_USER secret.
  3. Cut one release, confirm the package appears, then delete NUGET_API_KEY.

The publish job passed a NUGET_API_KEY repository secret to cake. It now
exchanges the GitHub OIDC token for a key valid ~1 hour (NuGet/login v1.2.0)
and passes that instead — cake neither knows nor cares that the key is
short-lived, it just receives one through --nuget-key, so build.cake is
unchanged. The only remaining secret is NUGET_USER, the nuget.org profile name.

The job gains an explicit minimal permissions block: build.cake's
Publish-NuGet target only pushes to nuget.org, so contents: read plus
id-token: write is all it needs.

--github-key is dropped in the same change: build.cake reads only 'nuget-key',
so it was handing GITHUB_TOKEN to a script that never asked for it.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@phmatray

Copy link
Copy Markdown
Owner Author

Important

Bloquée : ne pas merger tant que la politique nuget.org n'est pas déclarée.

Cette PR est verte, et son code est bon. Elle casserait quand même la prochaine release, silencieusement.

Pourquoi la CI ne peut pas le voir. L'étape de login est gardée par if: steps.should_release.outputs.new_release == 'true', et non par if: env.NUGET_USER != ''. Aucun job de CI n'exerce le chemin de publication : le vert porte sur le build, pas sur la release. Vérifié le 2026-07-26 — le secret NUGET_USER n'existe sur aucun des trois dépôts concernés, et aucune politique de publication de confiance n'est déclarée côté nuget.org. Au prochain tag, NuGet/login échouerait faute d'identité, et le paquet ne serait pas publié.

Ordre impératif pour débloquer (l'inverse laisse sans retour arrière) :

  1. déclarer la politique de publication de confiance sur nuget.org (dépôt + fichier de workflow + ids de paquets) ;
  2. poser le secret NUGET_USER (le nom de profil nuget.org, pas un identifiant secret) ;
  3. merger cette PR ;
  4. publier une release et vérifier que le paquet apparaît ;
  5. puis seulement supprimer NUGET_API_KEY.

Table des 7 politiques à déclarer : ~/Repositories/nuget-trusted-publishing-policies.md.

Passée en brouillon exprès, pour qu'elle ne ressorte pas comme « verte et mergeable » dans le tableau de bord.

@phmatray
phmatray marked this pull request as draft July 26, 2026 08:57
@phmatray

Copy link
Copy Markdown
Owner Author

Les deux prérequis de cette PR sont faits (2026-07-27).

  1. Policy créée — nom MasterCommander, Package Owner phmatray, dépôt phmatray/MasterCommander, workflow publish.yml. Active.
  2. NUGET_USER posé = phmatray.

Vérifié : mastercommander est bien owned by phmatray sur nuget.org, donc la policy est sous le bon compte.

Il ne reste que l'étape 3 : une release réelle, vérifier que le paquet apparaît, puis supprimer NUGET_API_KEY (encore présent comme secret de dépôt).

Procédure complète : repo-audit/TRUSTED_PUBLISHING.md.

@phmatray
phmatray marked this pull request as ready for review July 27, 2026 18:33
@phmatray
phmatray merged commit 970a006 into dev Jul 27, 2026
5 checks passed
@phmatray
phmatray deleted the ci/nuget-trusted-publishing branch July 27, 2026 18:33
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.

1 participant