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 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!


About Maarten Balliauw

Maarten Balliauw is a Developer Advocate at JetBrains, working on .NET tools. He focuses on .NET, Azure, web technologies and application performance. Maarten is a frequent speaker at various national and international events. In his free time, he brews his own beer. Follow him on Twitter or check out his personal blog.
This entry was posted in Features, How-To's, Tips&Tricks and tagged , , , . Bookmark the permalink.

15 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

    • Maarten Balliauw says:

      Let me check with the team. I will get back to you!

    • Eugene Petrenko says:

      This should be an amazing feature in TeamCity once we implement it. Unfortunately, it’s not implemented yet.
      Once you use authenticated TeamCity’s NuGet feed you may notice all downloaded packages are tracked by TeamCity as downloaded artifacts. The thing is we do not enforce NuGet to re-download all dependencies at once

      The feature could also be expanded to make TeamCity’s feed be aware of build snapshot dependencies making packages fetch/update more predictable within build chains.

      Please vote for or create your own issue to cover your exact case

  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

  6. Erik says:

    I dont expect you to read this seeing as the post is really old, but as of teamcity v 9.0.1 (build 32116) this runner is impossible to use in a template where the solution file name is a parameter as the “solution file path” setting is hardcoded to check if the entered text ends with .sln

Comments are closed.