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
- An Azure DevOps organization. Create an organization, if you don't have one already.
- An Azure DevOps project. If you don't have one yet, you can create a new project.
- An Azure Artifacts feed. Create a new feed if you don't have one already.
- Connect to Azure Artifacts feed: NuGet.exe, dotnet.
- Set up your pipeline permissions.
Restore NuGet packages from a feed
Navigate to your classic pipeline definition, and then select Edit.
Select + to add a new task. Search for NuGet, and then select Add to add the task to your pipeline.
Name your task and select Restore from the Command.
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.
If you want to include packages from NuGet.org, check the Use packages from NuGet.org checkbox.
Select Save & queue when you're done.
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 .sln
file. 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
Navigate to your Azure DevOps organization, and then select User settings > Personal Access Tokens.
Create a personal access token with Packaging (read) scope and copy your PAT to the clipboard.
Restore NuGet packages
Navigate to your pipeline definition and select the NuGet restore task. Make sure you're using version 2 of the task.
Select Feeds and authentication, and then select Feeds in my NuGet.config.
Select the path of your NuGet.config file.
Select New to add Credentials for feeds outside this organization/collection.
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.
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.