NuGet Package Restore with TeamCity

When working with NuGet we can make use of package restore. The concept is simple: instead of committing packages that we depend on to source control, we only commit the list of dependencies we need and download the assemblies during build.

In TeamCity, package restore is supported in various ways. We can use the NuGet.targets file (NuGet v2.6 and earlier) or run nuget restore from the command line (v2.7). TeamCity has a build runner which makes setting up package restore a breeze: the NuGet Installer.

NuGet Installer

The NuGet Installer build runner will make sure all package dependencies are satisfied during build. It provides several options:

We can specify the package sources that will be used to satisfy dependencies. By default, restoring will be done using NuGet.org but we can also make use of a private NuGet feed hosted on MyGet, ProGet or our own NuGet Gallery installation. When the current build depends on NuGet packages created by another build, we can also make use of the TeamCity NuGet feed (if enabled), for which the feed URLs are available from the %teamcity.nuget.feed.server% and %teamcity.nuget.feed.auth.server% parameters.

Other available options are disabling the local NuGet cache, forcing a fresh download of packages during every build. We can also enable auto-updating of NuGet packages if needed.

Which packages are we using?

The NuGet Installer runner also adds an additional tab on the build results page, showing the list of all packages used.

Package restore and feed authentication

When consuming NuGet packages from an authenticated feed during a build, the last thing you want to do is add credentials for connecting to that authenticated feed to source control. TeamCity has a much more elegant solution for this: the NuGet Feed Credentials Build Feature. We can add it to any build configuration, one for every feed that requires authentication.

TeamCity package restore from authenticated feed

Happy building!

This entry was posted in Features, How-To's, Tips&Tricks and tagged , , , . Bookmark the permalink.

13 Responses to NuGet Package Restore with TeamCity

  1. Michael says:

    Hello Maarten!

    Since TeamCity already logs the nuget package used and provides a nuget server as well: Does consuming a nuget package produced as a TeamCity artifact also create a keep-alive dependency between the builds so the nuget package does not get cleaned up by TeamCity as long as there are exisiting downstream builds depending on it? I’m specifically looking at the idea that you do not need to create a dependency on a specific version up-front, but simply by having your source-controlled project reference a specific TeamCity-published nuget package in a specific version.

    Best regards, Michael

  2. Pingback: JetBrains .NET Tools Blog » ReSharper SDK Now Available on NuGet

  3. James says:

    The word “downloaded” is misspelled as “downlaoded” on the Used Packages tab.

  4. jgauffin says:

    How do I get TeamCity to use the versions specified in the project packages.config? My build fails every time TeamCity downloads a newer version.

  5. Pingback: MyGet Blog | Migrate away from MSBuild-based NuGet package restore

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>