C# (NuGet)
The C# target publishes to NuGet. The package id is the target's packageName reduced to a PascalCase .NET identifier: acme-api and Acme.Api both publish as AcmeApi. See the C# configuration for options.
Authenticate the release with OIDC trusted publishing (recommended) or a NuGet API key.
Enable publishing
{
"targets": {
"csharp": {
"packageName": "AcmeApi",
"publish": { "nuget": true }
}
}
}
Trusted publishing (OIDC)
Recommended. The release workflow uses the NuGet/login action to exchange the workflow's identity token for a short-lived API key, so no long-lived key is stored. NuGet's OIDC login still needs your profile name as one secret.
Add a trusted publishing policy on NuGet
On nuget.org, open your username → Trusted Publishing and add a policy for:
- Package owner: you, or an organization you are an active member of
- Repository owner and name: your linked repository
- Workflow file:
release-please.yml - Environment:
publish.nuget.releaseEnvironment, or blank when none is set
The policy's scopes and package glob decide whether it may publish a package that does not exist yet: select the scope that allows publishing new packages, and a glob the package id matches, or the first release is rejected.
The automated publish runs as the publish job inside release-please.yml, so that is the workflow NuGet sees. If you also dispatch sdk-release.yml to re-publish a tag by hand, add it as a second policy.
A new policy is sometimes only provisionally active for 7 days (nuget.org says this usually happens for a private repository); the first successful publish inside that window makes it permanent. You can restart the window at any time, including after it lapses.
Add your NuGet username as a secret
Add a repository secret named NUGET_USER set to your nuget.org profile name (not your email). See Adding repository secrets. This is the only secret OIDC needs; the API key itself is issued at publish time.
Keep the default config
{ "targets": { "csharp": { "publish": { "nuget": true } } } }
Publishing with a NuGet API key
Create a NuGet API key
On nuget.org, go to API Keys → Create, give it the scope to push new packages and package versions with a glob that matches your package id, and copy the key.
Add it to the repository
Add the key as a repository secret named NUGET_API_KEY. See Adding repository secrets. When publish.nuget.releaseEnvironment is set, an environment secret of the same name overrides the repository one.
Switch the target to token auth
{
"targets": {
"csharp": {
"publish": { "nuget": { "authMethod": "access-token" } }
}
}
}
Notes
- The workflow pushes with
--skip-duplicate, so re-publishing an existing version is a no-op.
Strong naming
With options.strongName set, the release has to sign the package with your .snk key, which the generator never creates or writes. Provide it in one of two ways before the first release:
- Commit it at its
keyFilepath on the integration branch. - Store it as a secret named
STRONG_NAME_KEY, holding the key file base64-encoded (base64 -w0 Sdk.snkon Linux,base64 -i Sdk.snkon macOS). The release workflows write it tokeyFilebefore building, so the key never has to be committed. Whenpublish.nuget.releaseEnvironmentis set, an environment secret of the same name overrides the repository one.
The release workflows check for the key before anything builds, and stop with an error if neither the file nor the secret is there: an unsigned package is never published.
The generated CI also has a Strong-name key check on the release PR, which fails if the key is missing. Wait for it before merging: merging the release PR is what tags the release, and the publish job runs after that. A pull request run cannot read environment secrets, so with releaseEnvironment set and no repository-level key, the check only warns.
If a release was tagged without a key anyway, its publish job fails. To publish it, add the STRONG_NAME_KEY secret, then re-run the failed publish job or dispatch sdk-release.yml at that tag. Committing the key does not help here, because both workflows build the tagged commit, which does not contain it. This needs the tagged commit to contain workflows that restore the key. A tag cut before the SDK was regenerated with them still runs the old workflows, so cut a new release instead.
What did you think of this content?