.NET packages and releases on GitHub
Publish packable .NET projects to a GitHub Packages NuGet feed owned by an organisation, let that organisation's other repositories restore them, and release each version as a tag, a GitHub release with notes taken from the changelog, and attached self-contained binaries.
Placeholders used throughout: OWNER is the organisation or user that owns the feed, REPO is the repository that publishes, CONSUMER is a repository that restores, PKG is a package id, X.Y.Z is a version and my-tool is an example application name.
Critical rules
- A version ships only with a changelog section.
CHANGELOG.mdmust hold a non-empty## [X.Y.Z]section before the tag is made. The gate inreferences/changelog-gate.mdrefuses otherwise, locally and in the workflow. Missing changelog sections were a recurring miss; never bypass the gate by tagging by hand. - Never put a token in a committed file, and keep it off command lines on shared machines. A process argument is visible to
psand lands in shell history.dotnet nuget push --api-keynecessarily takes the value as an argument, so use it only on an ephemeral CI runner. Elsewhere pass tokens through environment variables or stdin. GITHUB_TOKENreads only its own repository's packages. A consumer in the same organisation needs either a per-package access grant or a read token in a secret. Per-package grants do not scale; seereferences/consumer-access.md.- GitHub Packages rejects
.snupkgfiles. Push*.nupkgonly. dotnet nuget push --skip-duplicatesilently skips a version that already exists. It does not replace it. Never publish different bytes under an existing version; bump the version.- MinVer needs full git history. Use
actions/checkoutwithfetch-depth: 0, or every build reports0.0.0. - Pin third-party actions to a full commit SHA. The tags in the examples are for reading.
Situation to reference
| Situation | Load |
|---|---|
Writing nuget.config, package metadata, or a local pack and push | references/feed-and-nuget-config.md |
| Choosing MinVer or an explicit version, prerelease suffixes, tags | references/versioning.md |
Enforcing a changelog section, extracting release notes, bumping [Unreleased] | references/changelog-gate.md |
Writing the publish and release workflow, gh release create, asset upload | references/release-workflow.md |
| Self-contained single-file builds for linux-x64, win-x64, osx-arm64 | references/cross-platform-builds.md |
| A consumer repository cannot restore a private package | references/consumer-access.md |
| A release workflow failed, a local dry run, or Actions minutes ran out | references/troubleshooting-and-fallbacks.md |
Procedure 1: add a publishable package to a repository
- Confirm the feed URL. It is
https://nuget.pkg.github.com/OWNER/index.json, with the owner in lower case. - Add
nuget.configat the repository root as set out inreferences/feed-and-nuget-config.mdsection 1. Credentials come from environment variables only. - In each packable project, set
PackageId,Description,PackageLicenseExpression,RepositoryUrlandPackageReadmeFile(section 3 of the same reference). - Pack locally with an explicit version and inspect the result: ``
bash dotnet pack src/PKG/PKG.csproj -c Release -p:PackageVersion=X.Y.Z -o artifacts/nupkg unzip -l artifacts/nupkg/PKG.X.Y.Z.nupkg`The listing must show thelib/` assemblies and the readme. Nothing else needs to be there. - Add the publish job from
references/release-workflow.mdwithpermissions: packages: write.
Procedure 2: cut a release
- Confirm the branch being released has a clean tree:
git status --shortprints nothing. - Rename
## [Unreleased]to the new version and date, and add a fresh empty[Unreleased](references/changelog-gate.mdsection 2). Review the diff. - Run the gate. It must exit 0 before anything else happens: ``
bash bash "$GATE" X.Y.Z CHANGELOG.md; echo "gate exit $?"`$GATEis the script saved fromreferences/changelog-gate.md`, kept outside the repository. - Commit the changelog change on its own (
chore(release): X.Y.Z changelog). Push the branch and merge it through the repository's normal pull request flow. - Start the release workflow on the merged commit: ``
bash gh workflow run release.yml --ref main -f version=X.Y.Z gh run list --workflow release.yml --limit 3`` - When the run is green, verify each output. Do not report the release as done before this: ``
bash gh release view vX.Y.Z --json tagName,isPrerelease,assets --jq '{tagName, isPrerelease, assets: [.assets[].name]}' gh api "/orgs/OWNER/packages/nuget/PKG/versions" --jq '.[].name'`` The version must appear in the package list and the assets must match the platforms built.
Procedure 3: consume packages from another repository
- Choose the access model in
references/consumer-access.md. For an organisation with several consumers, the read-token secret is the usual answer. - Add the same
nuget.configas the publisher, with thegithub-OWNERsource and the credentials taken fromGITHUB_PACKAGES_USERandGITHUB_PACKAGES_TOKEN. - Store the token as a secret on the consumer, reading it from a file or stdin: ``
bash gh secret set GITHUB_PACKAGES_TOKEN --repo OWNER/CONSUMER < "$TOKEN_FILE" gh variable set GITHUB_PACKAGES_USER --repo OWNER/CONSUMER --body "LOGIN"`LOGINis the account that owns the token.TOKEN_FILE` is mode 600 and is deleted afterwards. - Map the secret into the restore step, not the whole job: ``
yaml run: dotnet restore env: GITHUB_PACKAGES_USER: ${{ vars.GITHUB_PACKAGES_USER }} GITHUB_PACKAGES_TOKEN: ${{ secrets.GITHUB_PACKAGES_TOKEN }}`` - Prove the source works before relying on it:
dotnet nuget list sourceshows the feed as enabled, anddotnet restore --forcesucceeds with the variables set.
Validation checklist
nuget.confighas no literal token, and thepackageSourceCredentialsvalues are%VAR%references.dotnet packoutput name matches thePackageIdand the chosen version.- The gate script exits 0 for a present section and non-zero for a missing or empty one. Test it against fixtures before trusting it (see
references/changelog-gate.mdsection 4). - The release job has
contents: writeforgh release createandpackages: writefor the push. - Every version pushed to the feed matches a tag and a changelog section.
Scope
This skill covers NuGet packages on GitHub Packages and GitHub releases produced by Actions or by a local script. It does not cover nuget.org publishing, container registries or application hosting. C# code quality belongs to dotnet-guru.