How to add the custom nuget feed to TeamCity build?

Solution 1:

The NuGet package sources are configured through Visual Studio, but they're stored in a per-user configuration file, found at c:\Users\$USER\AppData\Roaming\NuGet\NuGet.config. The entry for the TeamCity package source needs to be added to the config file of the build agent user that's running your builds.

  1. On your local machine, open the Nuget.config file for your user
  2. Copy the entry for the TeamCity package source to the clipboard
  3. On the build agent, open the NuGet.config file for the user that's executing your TeamCity builds
  4. Paste in the TeamCity package source entry. Save & quit.
  5. Run the build again. It should now be able to find the package.

EDIT: ladenedge documents a good solution that didn't exist when I originally answered this question. The solution is especially useful if you don't have admin access to the build agent or want to configure package sources on a per-project basis.

Solution 2:

NuGet can now read sources from the NuGet.targets file stored with the source itself as explained in the answer to a duplicate question.

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
    <!-- The official NuGet package source (https://nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
    <PackageSource Include="https://nuget.org/api/v2/" />
    <PackageSource Include="https://my-nuget-source/nuget/" />
</ItemGroup>

Solution 3:

I didn't want to add feed in NuGet.config in source code as this is part of build infrastructure and not part of application code.

Neither I wanted to add feed in NuGet.config on TeamCity build agents as it's difficult to maintain (update, add, remove), plus we have many build agents.

What worked the best for me - adding %system.PackageSources% in TeamCity (on Root project) containing both global nuget feed and my custom (semicolon-separated):

https://www.nuget.org/api/v2/; http://<teamcity_server_name>:81/guestAuth/app/nuget/v1/FeedService.svc

If you look at NuGet.Targets, you'll see that it uses $(PackageSources) if it's defined. So you define it as "system" parameter in TeamCity and it gets passed to the build process.