Moving to TFS Build, how can I use Enable-PackageRestore and allow the build server to get dependant nuget packages?

Nov 18, 2011 at 7:43 PM

I'm hosting my own Nuget local server which hosts some packages I've build that my projects rely on. Now that I'm moving towards CI using TFS Build, I'm getting errors because it can't find where to get the Nuget references from - with errors like "Unable to find version '' of package 'BaseLibraries'.

Any suggestions? There doesn't seem to be a good "How to get TFS Build and Nuget to work together" article.

I'm heading towards trying to set up an OctopusDeploy scenario, that's my end goal currently.

Nov 20, 2011 at 10:57 AM

I'm doing exactly this and there is not anything really specific to TFS when it comes to leveraging NuGet packages in the build process.

When you run the power command Enable-PackageRestore, it does three things:

  1. Creates a .nuget folder containing nuget.exe.
  2. Adds build targets udner .nuget\ for calling nuget.exe in (1) to install and/or update packages.
  3. Includes the targets in (2) in your project file(s) (.csproj).

(1) makes sure that even if a computer does not have NuGet installed the packages can be installed and updated. This makes it ideal for build agents since you minimize the configuration load for new agents.

The targets in (2) hooks into the build process of the project and installs and / or updates packages before any compilation takes place. It is defined to take the packages.config located directly under each project's folder.

Basically what you need to make sure this works when building with TFS is that the .nuget folder needs to be checked in. Remember that when adding files .exe files are ignored by default. Just erase that ignore filter in the Add Files dialog and you're all set.

But, in your case it gets a little bit more complicated, since you are hosting your own NuGet feed. You need to specify what sources you want NuGet to use. There are two ways to do this:

  1. You can add your feed to %APPDATA%\NuGet\NuGet.Config on your build agent.
    The problem with this is that it adds another configuration management for build agents. Personally I like to keep my build agents as clean as possible to make it easy to add new agents.
  2. So I would suggest opening the NuGet.settings.targets file in the .nuget folder and changing the package sources via the tag <PackageSources>.

Hope this helps!

Nov 21, 2011 at 1:57 AM

Ah, okay - I think I understand. I'll try this out tomorrow and I'll let you know how it works. I knew it had something to do with the local nuget feed as a source, but I wasn't sure where to add it. I found a post that mentioned add in a pre-build step and modified it to specify the local (network) feed, but I could see that that nuget command was running twice (I didn't know that Enable-PackageRestore added all that in).

Thanks :)

Nov 21, 2011 at 2:24 PM

Hmm, what should that line look like? My nuget feed is available at \\main\nuget or http://main/nuget ..

Nov 21, 2011 at 2:43 PM

Then it should look like this.

<PackageSources>\\main\nuget -source</PackageSources>
I've included the URL to the official NuGet feed as well, but you can remove that if you like. The reason i don't start with "-source" is that that is already included later in the targets file on <RestoreCommand>.

Nov 21, 2011 at 3:38 PM

Ah! That worked perfectly. Thanks!