Restore NuGet packages with Azure Pipelines (YAML/Classic)

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

With NuGet Package Restore you can install all your project's dependency without needing 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 guide you through restoring your NuGet packages using both Classic and YAML Pipelines.

Prerequisites

Restore NuGet packages from a feed in the same organization

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

    steps:
    - task: NuGetAuthenticate@1
    
    - task: NuGetToolInstaller@1
      inputs:
        versionSpec: '*'
        checkLatest: true
    
    - script: nuget restore <SOLUTION_PATH>
    

Note

Make sure that The NuGet Gallery upstream is enabled in your feed. See Enable upstream sources in an existing feed for details.

Restore NuGet packages from a feed in another organization

To restore NuGet packages from a feed in a different Azure DevOps organization, you must first create a personal access token then use it to set up a NuGet service connection.

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 new personal access token with Packaging* > Read scope. Copy your PAT as you'll need it in the following section.

  3. Select Create when you're done.

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

Create a service connection

  1. Sign in to the Azure DevOps organization where your pipeline will run, and then navigate to your project.

  2. Navigate to your Project settings > Service connections.

  3. Select New service connection, select NuGet, and then select Next.

  4. Select External Azure DevOps Server as the Authentication method, and then enter your target Feed URL. Paste the Personal Access Token you created earlier, provide a name for your service connection, and check Grant access permission to all pipelines if applicable to your scenario.

  5. Select Save when you're done.

    A screenshot showing how to create a new NuGet service connection.

Restore packages

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select Pipelines, and then select your pipeline definition.

  3. Select Edit, and then add the following snippet to your YAML pipeline.

    - task: NuGetToolInstaller@1
      inputs:
        versionSpec: '*'
        checkLatest: true
    
    - task: NuGetAuthenticate@1
      inputs:
        nuGetServiceConnections: <SERVICE_CONNECTION_NAME>
    
    - script: |
          nuget.exe restore <SOLUTION_PATH>
      displayName: Restore