Restore your nuget packages on build server

Some time ago I wrote about how you can manage your dependencies with nuget. What I left out is how you configure Visual Studio to not check in the packages but to restore them during build on the CI server.

Disable Source Control Integration

To disable source control integration in Visual Studio you have to add a nuget.config to your solution folder. In the file set disableSourceControlIntegration for the solution element to true.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
</configuration>

If you use TFSVC you also have to add a file with the name .tfignore add solution level to source control if it does not yet exist. Add a line with \packages to the file.

If you use git then check that your gitignore file contains the following line:

**/packages/*

Now you can delete the packages folder from source control. New packages will not added by Visual Studio and VS will automatically restore them for you.

Restore nuget packages on the build server

If your build server is behind a proxy you can configure the proxy in the nuget.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="http_proxy" value=http://server:Port />
  </config>
</configuration>

To enable the build server to find all your packages you have to specify the package sources.

Be extremely careful to specify the URL to nuget.org corectly! There is a lot of documentation out there that does not work any more. If your URI points to the V3 API you also have to specify protocolVersion=”3”. Check the URL in your Visual Studio Settings in case this changes with a future update.

nuget.org in Visual Studio settings dialog

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Share" value=\\server\share />
    <add key="NuGet Server" value=http://server/nuget />
  </packageSources>
</configuration>

In your build you don’t need the “NuGet Installer” task. It is sufficient to check the “Restore NuGet Packages” check box for your build task – even if you have custom packages sources.

Restore NuGet packages in Visual Studio Build task

Troubleshooting nuget on the build server

If you have problems with restoring packages on the build server – it is really hard to get a verbose output. The best thing you can do is to download the latest nuget.exe and add it to your source control. Then use the NuGet Installer task and specify the path to the exe. Add “-Verbosity detailed” to the NuGet Arguments.

Trouble shooting nuget restore on the build server

This gave me a detailed output of the API calls that are performed by nuget.exe that helped me troubleshooting. I have no idea why this does not work with the default nuget.exe.

5 thoughts on “Restore your nuget packages on build server

  1. I’m wondering if the nuget.config with disableSourceControlIntegration=true in it is still needed. Especially with git and the .gitignore file. I never had any luck with the .tfignore file, that seemed like a hoax 😉

  2. I’m wondering if the nuget.config with disableSourceControlIntegration=true in it is still needed. Especially with git and the .gitignore file. I never had any luck with the .tfignore file, that seemed like a hoax 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s