TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Pushing to a Feed & Consuming It

Pushing to a Feed & Consuming It

Packing produces a file; publishing makes it available to other builds. dotnet nuget push uploads a .nupkg to a source, which can be nuget.org, a hosted private feed or, as here, a folder. The consuming side is just a PackageReference and a nuget.config that knows the source.

Push, then consume from another project

bash · try it yourself
mkdir greet && cd greet
cat > Greet.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <PackageId>Ada.Greet</PackageId>
    <VersionPrefix>1.0.0</VersionPrefix>
    <Authors>Ada Lovelace</Authors>
    <Description>Friendly greetings.</Description>
  </PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Greeter { public static string Hello(string n) => 
quot;Hello, {n}!"; }' > Greeter.cs dotnet pack -o out -nologo -v:q dotnet nuget push out/Ada.Greet.1.0.0.nupkg --source local ls /opt/nuget-feed | grep '^Ada\.' cd .. mkdir app && cd app cat > app.csproj <<'XML' <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> </PropertyGroup> </Project> XML echo 'System.Console.WriteLine(Ada.Greeter.Hello("feed"));' > Program.cs dotnet package add Ada.Greet | grep -E 'Installed|added to file' | sed 's/ with content hash.*//' dotnet run
Pushing Ada.Greet.1.0.0.nupkg to '/opt/nuget-feed'...
Your package was pushed.
Ada.Greet.1.0.0.nupkg
info : Installed Ada.Greet 1.0.0 from /opt/nuget-feed to /home/learner/.nuget/packages/ada.greet/1.0.0
info : PackageReference for package 'Ada.Greet' version '1.0.0' added to file '/home/learner/app/app.csproj'.
Hello, feed!

The app has no idea where Ada.Greet came from: to it, the package is indistinguishable from one on nuget.org. That's the point of the exercise, and the mini project does the same for a whole solution.

For a hosted feed the command is the same plus authentication:

bash
# nuget.org or any v3 feed with API keys (sample, not run here)
dotnet nuget push "out/*.nupkg" --source https://api.nuget.org/v3/index.json --api-key "$NUGET_API_KEY"
# GitHub Packages / Azure Artifacts: a source with credentials in nuget.config, then
dotnet nuget push "out/*.nupkg" --source github --api-key "$GITHUB_TOKEN"

A glob pushes every package in the folder; matching .snupkg files are pushed automatically. Keep the key in a CI secret, never in nuget.config in the repository. nuget.org also supports trusted publishing from GitHub Actions (a short-lived key obtained through OIDC), which removes the long-lived key entirely.

Prerelease versions and upgrades

bash · try it yourself
mkdir greet && cd greet
cat > Greet.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <PackageId>Ada.Greet</PackageId>
    <VersionPrefix>1.0.0</VersionPrefix>
  </PropertyGroup>
</Project>
XML
publish() {  # publish <file content version> [pack args]
  echo "namespace Ada; public static class Greeter { public const string V = \"$1\"; }" > Greeter.cs
  shift
  dotnet pack -o out -nologo -v:q "$@" && dotnet nuget push "out/*.nupkg" -s local > /dev/null && rm -rf out
}
publish 1.0.0
publish 1.1.0-beta.1 -p:VersionPrefix=1.1.0 --version-suffix beta.1
publish 1.1.0 -p:VersionPrefix=1.1.0
ls /opt/nuget-feed | grep '^Ada\.'
cd .. && mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Ada.Greet" Version="1.0.0" />
  </ItemGroup>
</Project>
XML
echo 'System.Console.WriteLine(
quot;using Ada.Greet {Ada.Greeter.V}");' > Program.cs dotnet run dotnet package list --outdated | grep '>' dotnet package list --outdated --include-prerelease | grep '>' sed -i 's/Version="1.0.0"/Version="1.1.0-beta.1"/' app.csproj dotnet run
Ada.Greet.1.0.0.nupkg
Ada.Greet.1.1.0-beta.1.nupkg
Ada.Greet.1.1.0.nupkg
using Ada.Greet 1.0.0
   > Ada.Greet            1.0.0       1.0.0      1.1.0
   > Ada.Greet            1.0.0       1.0.0      1.1.0
using Ada.Greet 1.1.0-beta.1
  • VersionPrefix + --version-suffix builds a SemVer 2.0 prerelease version (1.1.0-beta.1). Setting Version directly overrides both.
  • Prereleases sort below the release with the same numbers: even with --include-prerelease, the latest version is still 1.1.0, because the release outranks 1.1.0-beta.1. NuGet never picks a prerelease unless asked (a range like 1.0.0 never resolves to one); to use it, reference it explicitly, as the last dotnet run did.
  • The app stayed on 1.0.0 although 1.1.0 was available: lowest applicable version again. Upgrading is a deliberate edit.

Unlisting and deleting

On nuget.org you can't delete a version, only unlist it (dotnet nuget delete Id Version does that there): it disappears from search and from floating resolution, but existing lock files and exact references still restore it. On a folder feed dotnet nuget delete really removes the file. Either way, a version number, once published, should never be reused for different content: lock files would reject it, and caches all over the world already hold the old one.

Test yourself

An app references Ada.Greet Version 1.0.0; the feed has 1.0.0, 1.1.0-beta.1 and 1.1.0. What does restore pick?

  1. 1.1.0
  2. 1.1.0-beta.1
  3. 1.0.0
  4. It fails

How do you build version 2.0.0-rc.1 from a project with <VersionPrefix>2.0.0</VersionPrefix>?

  1. dotnet pack --version-suffix rc.1
  2. dotnet pack -p:Prerelease=true
  3. dotnet pack -c rc.1
  4. Rename the .nupkg

What does deleting a package version on nuget.org actually do?

  1. Removes it permanently
  2. Unlists it: hidden from search and new floating resolution, but still restorable by exact version
  3. Marks it as vulnerable
  4. Moves it to a symbol server