49

I have created a Nuget Server using Teamcity (running on a virtual machine in internet) and created the build that publishes a package into it.

I also have another project that needs to use that package. This project is built on teamcity as well. On my local Visual Studio I added the nuget feed uri, installed the package and everything works fine. But when I try to build it on teamcity it says that "Package not found".

So my question is : "How to add the custom nuget feed to TeamCity build?"

Eugene
  • 1,515
  • 1
  • 13
  • 23

4 Answers4

52

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.

John Hoerr
  • 7,955
  • 2
  • 30
  • 40
  • Great, works perfect. And one more small question, how to save credentials for nuget feed? Not I'm using public teamcity feed, but I wish I used a login/password auth. – Eugene Jan 27 '13 at 16:05
  • 1
    Check out the answers to [this question](http://stackoverflow.com/q/14102695/1453907). I'm not crazy about the accepted answer because it leaves your password in plain text. Eugene Petrenko's answer is worth checking out -- he's a TeamCity developer and knows what he's talking about. – John Hoerr Jan 27 '13 at 16:50
  • 20
    If you are running the build agent under the Windows SYSTEM account then the configuration file path will be `C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config` (for 64 bit OS) or `C:\Windows\System32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config` (for 32 bit OS). – Andrew Cupper Apr 18 '13 at 01:45
  • 1
    btw, TeamCity writes source of config file in build log, so you can easily find what exactly config should be edited in this case `NuGet Config files used: C:\Windows\system32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config Feeds used: https://api.nuget.org/v3/index.json` – Anton Sep 04 '18 at 07:29
  • Thanks for sharing this! For me, on Windows 11 (64-bit) the folder was: C:\Windows\System32\config\systemprofile\AppData\Roaming\NuGet – Markus Knappen Johansson May 01 '22 at 08:54
28

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>
Community
  • 1
  • 1
ladenedge
  • 13,197
  • 11
  • 60
  • 117
25

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.

Ivan
  • 9,089
  • 4
  • 61
  • 74
  • 1
    This is brilliant - I wanted to avoid the two dependencies you mentioned as well, and this worked like a charm. The other answers are good, but IMO this is the method of choice. – ket Mar 31 '17 at 21:34
  • Did you mean to include the percent sign in the name of the variable? I'm assuming not. That's just how it's called. So the name of the variable would be "system.PackageSources" without the quotes? – as9876 Dec 07 '18 at 17:14
  • Enclosing in percent sign is the way to reference properties in TeamCity, e.g. %build.counter%, of course it's not a part of the property name, see: https://confluence.jetbrains.com/display/TCD18/Configuring+Build+Parameters#ConfiguringBuildParameters-DefiningBuildParametersinBuildConfiguration – Ivan Dec 07 '18 at 21:07
  • 5
    @Ivan is there any way to add authentication (username and password) via environment variables? Our feeds are protected and I cannot find any way to set these via environment variables. Specifically, I need to set values that are usually in the `packageSourceCredentials` section in NuGet.Config. – feO2x May 07 '19 at 14:46
0

In TeamCity, we set up two NuGet Installer Steps; the first takes care of the local(custom) NuGet Install, and the second restores internet-accessible NuGet packages.

Two TeamCity Build Steps

The local(custom) install looks at the .sln file and then uses our defined Package Sources path to find our local NuGet packages.

Local(custom) NuGet Build Step

This was done using TeamCity v2022.04.3

Geovani Martinez
  • 2,053
  • 2
  • 27
  • 32