title | description | ms.assetid | ms.author | ms.reviewer | author | ms.custom | ms.topic | ms.date | monikerRange |
---|---|---|---|---|---|---|---|---|---|
Restore NuGet packages in Azure Pipelines |
Work with feeds in Azure Pipelines |
C3D7008E-7C23-49A4-9642-E5906DAE3BAD |
rabououn |
rabououn |
ramiMSFT |
seodec18, contperfq1 |
conceptual |
09/08/2020 |
>= tfs-2017 |
[!INCLUDE version-tfs-2017-rtm]
::: moniker range="<= tfs-2018" [!INCLUDE temp] ::: moniker-end
NuGet package restore allows you to have all your project's dependencies available without having to store them in source control. This allows for a cleaner development environment and smaller repository size. You can restore your NuGet packages using the NuGet restore build task, the NuGet CLI, or the .NET Core CLI. This article will show you how to restore your NuGet packages using both YAML and the classic Azure pipelines.
- Set up your solution to consume packages from Azure artifacts feed.
- Created your first pipeline for your repository.
- Set up the build identity permissions for your feed.
To build a solution that relies on NuGet packages from Azure artifacts feeds, we will want to add the NuGet build task to our pipeline.
- Navigate to your build pipeline and select Edit.
- Under Tasks, Agent job, select the plus sign "+" to add a new task. Search for NuGet task and add it to your agent job.
- Fill out the following information:
- Display name: NuGet restore.
- Command: restore.
- Path to solution, packages.config, or project.json: The path to the solution, packages.config, or project.json file that references the packages to be restored.
- If you've checked in a NuGet.config, select Feeds in my NuGet.config and specify the file from your repo. If you're using a single Azure Artifacts feed, select the Feed(s) I select here option and select your feed from the dropdown.
- Check the Use packages from NuGet.org option if you want to include NuGet.org in the generated NuGet.config.
- Select Save & queue .
In order for your project to be set up properly, your nuget.config
must be in the same folder as your .csproj
or .sln
file.
The nuGet.config
file you check-in also should list all the package sources you want to consume. The example below demonstrates how that might look.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- remove any machine-wide sources with <clear/> -->
<clear />
<!-- add an Azure Artifacts feed -->
<add key="FabrikamFiber" value="https://pkgs.dev.azure.com/microsoftLearnModule/_packaging/FabrikamFiber/nuget/v3/index.json" />
<!-- also get packages from the NuGet Gallery -->
<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
To restore your package using YAML and the .NET Core CLI task, use the following example:
- 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
, andcustom
.projects
: The path to the csproj file(s) to use. You can use wildcards (e.g. **/*.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 usingnugetConfigPath
. Options:select
,config
.vstsFeed
: This argument is required whenfeedsToUse
==Select
. Value format:<projectName>/<feedName>
.includeNuGetOrg
: Use packages from NuGet.org.
If your NuGet.config contains feeds in a different Azure DevOps organization than the one running the build, you'll need to set up credentials for those feeds manually.
-
Select an account (either a service account (recommended) or a user account) that has access to the remote feed.
-
In your browser, open a Private mode, Incognito mode, or a similar mode window and navigate to the Azure DevOps organization that hosts the feed. Sign in with the credentials mentioned in step 1, select User settings then Personal Access Tokens.
-
Create your PAT with the Packaging (read) scope and keep it handy.
-
In the Azure DevOps organization that contains the build, edit the build's NuGet step and ensure you're using version 2 or greater of the task, using the version selector.
-
In the Feeds and authentication section, Ensure you've selected the Feeds in my NuGet.config radio button.
-
Set the path to your NuGet.config in the Path to NuGet.config.
-
In Credentials for feeds outside this organization/collection, select the + New.
-
In the service connection dialog that appears, select the External Azure DevOps Server option and 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.
-
Save & queue a new build.
NuGet restore can fail due to a variety of issues. One of the most common issues is the introduction of a new project in your solution that requires a target framework that isn't understood by the version of NuGet your build is using. This issue generally doesn't present itself on a developer machine because Visual Studio updates the NuGet restore mechanism at the same time it adds new project types. We're looking into similar features for Azure Artifacts. In the meantime though, the first thing to try when you can't restore packages is to update to the latest version of NuGet.
::: moniker range=">= tfs-2018"
If you're using Azure Pipelines or TFS 2018, new template-based builds will work automatically thanks to a new "NuGet Tool Installer" task that's been added to the beginning of all build templates that use the NuGet task. We periodically update the default version that's selected for new builds around the same time we install Visual Studio updates on the Hosted build agents.
For existing builds, just add or update a NuGet Tool Installer task to select the version of NuGet for all the subsequent tasks. You can see all available versions of NuGet on nuget.org.
::: moniker-end
::: moniker range="<=tfs-2017"
Because the NuGet Tool Installer is not available in TFS versions prior to TFS 2018, there is a recommended workaround to use versions of NuGet > 4.0.0 in Azure Pipelines.
- Add the task, if you haven't already. If you have a "NuGet Restore" task in the catalog (it may be in the Deprecated tasks section), insert it into your build. Otherwise, insert a "NuGet" task.
- For your NuGet/NuGet Installer task, use the version selector under the task name to select version "0.*".
- In the Advanced section, set the NuGet Version to "Custom" and the Path to NuGet.exe as $(Build.BinariesDirectory)\nuget.exe
- Before your NuGet task, add a "PowerShell" task, select "Inline Script" as the Type, enter this PowerShell script as the Inline Script, and enter "4.3.0" (or any version of NuGet from this list) as the Arguments.
Our thanks to GitHub user leftler for creating the original version of the PowerShell script linked above.
::: moniker-end