Share via


Restore NuGet packages in Azure Pipelines

TFS 2017

With NuGet package, restore you can install all your project's dependency without having to store them in source control. This allows for a cleaner development environment and a smaller repository size. You can restore your NuGet packages using the NuGet restore task, the NuGet CLI, or the .NET Core CLI. This article will show you how to restore your NuGet packages using both the YAML and the classic Azure Pipelines.

Prerequisites

Restore packages with NuGet restore

  1. Navigate to your pipeline definition, and then select Edit.

  2. Select + to add a new task. Search for NuGet, and then select Add to add the task to your pipeline.

  3. Fill out the required fields.

    Screenshot showing how to configure the NuGet restore task.

  4. Select Feeds in my NuGet.config if you want to use your own config file otherwise select Feed(s) I select here, and select your feed from the dropdown menu.

  5. Check the Use packages from NuGet.org checkbox if you want to include packages from NuGet.org.

  6. Select Save & queue when you are done.

Restore packages with NuGet CLI

Place your nuget.config in the same folder as your .csproj or .slnfile. Your config file should look similar to the following example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- remove inherited connection strings -->
    <clear />
    <!-- add an Azure Artifacts feed -->
    <add key="FabrikamFiber" value="https://pkgs.dev.azure.com/microsoftLearnModule/_packaging/FabrikamFiber/nuget/v3/index.json" />
    <!-- Get packages from NuGet.org -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

To restore your NuGet packages, run the following command in your project directory:

nuget.exe restore

Restore packages with the .NET Core CLI task

- task: DotNetCoreCLI@2
  displayName: dotnet restore
  inputs:
    command: restore
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: '<projectName>/<feedName>'
    includeNuGetOrg: true
  • command: The dotnet command to run. Options: build, push, pack, restore, run, test, and custom.
  • projects: The path to the csproj file(s) to use. You can use wildcards (example: **/*.csproj for all .csproj files in all subfolders).
  • feedsToUse: You can either choose to select a feed or commit a NuGet.config file to your source code repository and set its path using nugetConfigPath. Options: select, config.
  • vstsFeed: This argument is required when feedsToUse == Select. Value format: <projectName>/<feedName>.
  • includeNuGetOrg: Use packages from NuGet.org.

Restore packages from feeds in a different organization

To restore NuGet packages from feeds in a different Azure DevOps organization, you must set up authentication to those feeds manually.

  1. Select an account (either a service account (recommended) or a user account) that has access to the remote feed.

  2. In your browser, open a Private mode, Incognito mode, or a similar mode window and navigate to the Azure DevOps organization hosting the remote feed. Sign in, and then select User settings -> Personal Access Tokens.

    Screenshot showing how to create a personal access token

  3. Create a personal access token with Packaging (read) scope and copy your pat to the clipboard.

  4. Navigate to your pipeline definition and select the NuGet restore task. Make sure you're using version 2 or greater.

    Screenshot showing the NuGet restore task version

  5. Select the Feeds and authentication section, and then select Feeds in my NuGet.config option.

  6. Enter the path to your NuGet.config file.

  7. Select New to add Credentials for feeds outside this organization/collection.

    Screenshot showing how to configure the NuGet restore task

  8. Select External Azure DevOps Server, and then enter a connection name, the feed URL (make sure it matches what's in your NuGet.config) and the PAT you created in step 3.

    Screenshot showing how to add a NuGet service connection

  9. Select Save & queue when you are done.

FAQ

My pipeline is failing to restore my NuGet packages?

The NuGet restore task can fail for several reasons. The most common scenario is when you add a new project that requires a target framework that is not supported by the NuGet version your pipeline is using. This failure doesn't occur generally in the local development environment because Visual Studio takes care of updating your solution accordingly. Make sure you upgrade your NuGet task to the latest version.

How do I use the latest version of NuGet?

The NuGet Tool Installer is not available in only available in TFS 2018. We recommended the following workaround to use NuGet version 4.0.0 or higher in your pipeline.

  1. Add the NuGet task to your pipeline definition..

  2. Select version 0.* for your NuGet/NuGet Installer tasks.

  3. Set the NuGet Version to "Custom" and the Path to NuGet.exe to $(Build.BinariesDirectory)\nuget.exe in the Advanced section.

  4. Add a PowerShell task BEFORE your NuGet task, and then select Inline Script and paste the following script in the script text area. The script will check if the version is 4.0.0 or higher and update the task version if needed.

    if (-not (Test-Path $(Build.BinariesDirectory)\nuget.exe))
    {
        $doDownload = $true
    }
    else
    {
        $nuGetVersion = Get-ChildItem $(Build.BinariesDirectory)\nuget.exe | %{$_.VersionInfo} | %{$_.ProductVersion}
        if ([System.Version]$nuGetVersion -lt [System.Version]"4.0.0")
        {
            $doDownload = $true
        }
    }
    if($doDownload)
    {
        Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/v4.0.0/nuget.exe -OutFile $(Build.BinariesDirectory)\nuget.exe
    }
    

We appreciate the contribution of our community for creating the original version of the PowerShell script mentioned above.