Restore NuGet packages in Azure Pipelines
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018
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.
- Set up your solution to consume packages from Azure Artifacts feed.
- Create your first pipeline.
- Set up permissions for your pipelines.
Restore packages with NuGet restore
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 are done.
Restore packages with NuGet CLI
nuget.config in the same folder as your
.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:
Restore packages with the .NET Core CLI task
- task: DotNetCoreCLI@2 displayName: dotnet restore inputs: command: restore ## The dotnet command to run. Options: build, push, pack, restore, run, test, and custom. projects: '**/*.csproj' ## Path to your csproj file feedsToUse: 'select' ## Options: select, config vstsFeed: '<projectName>/<feedName>' ## Required when feedsToUse == Select includeNuGetOrg: true ## 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 use a personal access token to authenticate.
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.
Navigate to your pipeline definition and select the NuGet restore task. Make sure you're using version 2 or greater.
Select the Feeds and authentication section, and then select Feeds in my NuGet.config option.
Enter the path to 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), a service connection name, and the personal access token you created earlier. Select Save when you are done.
Select Save & queue when you are done.
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?
For new pipelines, the NuGet Tool Installer will be added automatically to any pipeline that uses a NuGet task. We periodically update the NuGet default version around the same time we install Visual Studio updates on the Hosted build agents.
For existing pipelines, add the NuGet Tool Installer to your pipeline and select the NuGet version for all the subsequent tasks. Check out the dist.nuget to see all the available versions.
Submit and view feedback for