Redaguoti

Bendrinti naudojant


Restore NuGet packages with Azure Pipelines

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

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 Classic and YAML Pipelines.

Prerequisites

Restore NuGet packages from a feed

  1. Navigate to your classic 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. Name your task and select Restore from the Command.

  4. Select Feed(s) I select here, and select your feed from the dropdown menu. If you want to use your own config file, select Feeds in my NuGet.config and enter the path to your NuGet.config file and the service connection if you want to authenticate with feeds outside your organization.

  5. If you want to include packages from NuGet.org, check the Use packages from NuGet.org checkbox.

  6. Select Save & queue when you're done.

    Screenshot that shows how to configure the NuGet restore task.

Note

Classic NuGet restore uses the NuGetCommand@2 task. By default, this version uses NuGet 4.1.0. Use the NuGet Tool Installer task if you want to use a different NuGet version.

Restore NuGet packages locally

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 NuGet packages from a feed in a different organization

To restore NuGet packages from a feed in a different Azure DevOps organization, you must use a personal access token to authenticate.

Create a personal access token

  1. Navigate to your Azure DevOps organization, and then select User settings > Personal Access Tokens.

    Screenshot showing how to create a personal access token.

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

    A screenshot showing how to create a personal access token with packaging read permissions.

Restore NuGet packages

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

    Screenshot showing the NuGet restore task version.

  2. Select Feeds and authentication, and then select Feeds in my NuGet.config.

  3. Select the path of your NuGet.config file.

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

    Screenshot showing how to configure the NuGet restore task.

  5. Select External Azure DevOps Server, and then enter your feed URL (make sure it matches what's in your NuGet.config), your service connection name, and the personal access token you created earlier. Select Save when you're done.

    Screenshot showing how to add a new service connection.

  6. Select Save & queue when you're done.

FAQ

Q: My pipeline is failing to restore my NuGet packages?

A: 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.