Restore Private NuGet Feed Packages During Visual Studio Team Services Build

Update! As of February 2016, you should be able to use the standard “NuGet Installer” vNext build task.

The New Way

Add the Nuget Installer build task, which can be found under “Add build step” -> “Package”. You will need to supply a NuGet.config file with the address of your custom feed:

<?xml version="1.0" encoding="utf-8"?>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
    <add key="account-name" value="" />
    <add key="" value="" />
  <disabledPackageSources />
    <add key="" value="" />

Simply supply the path to the solution and the path to this NuGet.config file to the vNext build task and it will do everything without neededing the PowerShell script I wrote below.

The Old Way

If you need to restore packages from a private NuGet feed during your Visual Studio Team Services build, you can use this script as part of a PowerShell Build Step.

	[Parameter(Mandatory)][string] $SourceName,	# The name to be used to store credentials
	[Parameter(Mandatory)][string] $FeedUrl,	# The full path to the NuGet feed
	[Parameter(Mandatory)][string] $ProjectFolders, # Comma-separated list of project sub-folders that contain a packages.config file.
	[Parameter(Mandatory)][string] $Username,       # The user name to access the private feed
	[Parameter(Mandatory)][string] $Password        # The password to access the private feed


try {
	Write-Output "Adding package Source"
	$addSourceCommand = $LocalPath + "\nuget sources add -name """ + $SourceName + """ -source " + $FeedUrl + " -username """ + $Username + """ -password """ + $Password + """ "
	$er = (Invoke-Expression -Command $addSourceCommand) 2>&1
} catch {
	Write-Output "Skipping package source add"

$projectPaths = $ProjectFolders.Split(",")

Foreach ($path in $projectPaths) {
	Write-Output "Installing NuGet packages in $LocalPath $path"
	$installCommand =  $LocalPath + "\nuget install " + $LocalPath + $path + "packages.config -Source " + $FeedUrl + " -SolutionDirectory " + $LocalPath + " "
	Invoke-Expression -Command $installCommand

The script is entirely controlled via the list of paramters, which you can supply in the “arguments” field in the PowerShell Build Step:

-SourceName "example" -FeedUrl "" -ProjectFolders "\Networking\,\Networking.Tests\" -Username "steve.fenton" -Password "secret-magic-word"