NuGet task

Version 2.

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018

Use this task in a build or release pipeline to install and update NuGet package dependencies, or package and publish NuGet packages.

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, run and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

If your code depends on NuGet packages, make sure to add this step before your Visual Studio Build step. Also make sure to clear the deprecated Restore NuGet Packages checkbox in that step.

Tip

Looking for help to get started? See the how-tos for restoring and publishing packages. This version of the NuGet task uses NuGet 4.1.0 by default. To select a different version of NuGet, use the Tool Installer.

Note

Using or creating .NET Core or .NET Standard packages? Use the .NET Core task, which has full support for all package scenarios currently supported by dotnet, including restore, pack, and nuget push.

YAML snippet

# NuGet
# Restore, pack, or push NuGet packages, or run a NuGet command. Supports NuGet.org and authenticated feeds like Azure Artifacts and MyGet. Uses NuGet.exe and works with .NET Framework apps. For .NET Core and .NET Standard apps, use the .NET Core task.
- task: NuGetCommand@2
  inputs:
    #command: 'restore' # Options: restore, pack, push, custom
    #restoreSolution: '**/*.sln' # Required when command == Restore
    #feedsToUse: 'select' # Options: select, config
    #vstsFeed: # Required when feedsToUse == Select
    #includeNuGetOrg: true # Required when feedsToUse == Select
    #nugetConfigPath: # Required when feedsToUse == Config
    #externalFeedCredentials: # Optional
    #noCache: false 
    #disableParallelProcessing: false 
    restoreDirectory: 
    #verbosityRestore: 'Detailed' # Options: quiet, normal, detailed
    #packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg' # Required when command == Push
    #nuGetFeedType: 'internal' # Required when command == Push# Options: internal, external
    #publishVstsFeed: # Required when command == Push && NuGetFeedType == Internal
    #publishPackageMetadata: true # Optional
    #allowPackageConflicts: # Optional
    #publishFeedCredentials: # Required when command == Push && NuGetFeedType == External
    #verbosityPush: 'Detailed' # Options: quiet, normal, detailed
    #packagesToPack: '**/*.csproj' # Required when command == Pack
    #configuration: '$(BuildConfiguration)' # Optional
    #packDestination: '$(Build.ArtifactStagingDirectory)' # Optional
    #versioningScheme: 'off' # Options: off, byPrereleaseNumber, byEnvVar, byBuildNumber
    #includeReferencedProjects: false # Optional
    #versionEnvVar: # Required when versioningScheme == ByEnvVar
    #majorVersion: '1' # Required when versioningScheme == ByPrereleaseNumber
    #minorVersion: '0' # Required when versioningScheme == ByPrereleaseNumber
    #patchVersion: '0' # Required when versioningScheme == ByPrereleaseNumber
    #packTimezone: 'utc' # Required when versioningScheme == ByPrereleaseNumber# Options: utc, local
    #includeSymbols: false # Optional
    #toolPackage: # Optional
    #buildProperties: # Optional
    #basePath: # Optional, specify path to nuspec files
    #verbosityPack: 'Detailed' # Options: quiet, normal, detailed
    #arguments: # Required when command == Custom

Arguments

Argument Description
command
Command
The NuGet command to run. Select 'Custom' to add arguments or to use a different command.
Options: restore, pack, custom, push
restoreSolution
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.
feedsToUse
Feeds to use
You can either select a feed from Azure Artifacts and/or NuGet.org here, or commit a nuget.config file to your source code repository and set its path here.
vstsFeed
Use packages from this Azure Artifacts/TFS feed
Include the selected feed in the generated NuGet.config. You must have Azure Artifacts installed and licensed to select a feed here.
includeNuGetOrg
Use packages from NuGet.org
Include NuGet.org in the generated NuGet.config.
nugetConfigPath
Path to NuGet.config
The NuGet.config in your repository that specifies the feeds from which to restore packages.
externalFeedCredentials
Credentials for feeds outside this organization/collection
Credentials to use for external registries located in the selected NuGet.config. For feeds in this organization/collection, leave this blank; the build’s credentials are used automatically.
noCache
Disable local cache
Prevents NuGet from using packages from local machine caches.
disableParallelProcessing
Disable parallel processing
Prevents NuGet from installing multiple packages in parallel.
restoreDirectory
Destination directory
Specifies the folder in which packages are installed. If no folder is specified, packages are restored into a packages/ folder alongside the selected solution, packages.config, or project.json.
verbosityRestore
Verbosity
Specifies the amount of detail displayed in the output.
Options: Quiet, Normal, Detailed
packagesToPush
Target feed location
Specifies whether the target feed is and internal feed/collection or an external NuGet server.
Options: internal, external
publishVstsFeed
Target feed
Select a feed hosted in this account. You must have Azure Artifacts installed and licensed to select a feed here.
publishPackageMetadata
Publish pipeline metadata
If you continually publish a set of packages and only change the version number of the subset of packages that changed, use this option.
allowPackageConflicts It allows the task to report success even if some of your packages are rejected with 409 Conflict errors.
This option is currently only available on Azure Pipelines and using Windows agents. If NuGet.exe encounters a conflict, the task will fail. This option will not work and publish will fail if you are within a proxy environment.
publishFeedCredentials
NuGet server
The NuGet service connection that contains the external NuGet server’s credentials.
verbosityPush
Verbosity
Specifies the amount of detail displayed in the output.
Options: Quiet, Normal, Detailed
packagesToPack
Path to csproj or nuspec file(s) to pack
Pattern to search for csproj directories to pack.
You can separate multiple patterns with a semicolon, and you can make a pattern negative by prefixing it with '!'. Example: **\\*.csproj;!**\\*.Tests.csproj
configuration
Configuration to package
When using a csproj file this specifies the configuration to package.
packDestination
Package folder
Folder where packages will be created. If empty, packages will be created at the source root.
versioningScheme
Automatic package versioning
Cannot be used with include referenced projects. If you choose 'Use the date and time', this will generate a SemVer-compliant version formatted as X.Y.Z-ci-datetime where you choose X, Y, and Z.
If you choose 'Use an environment variable', you must select an environment variable and ensure it contains the version number you want to use.
If you choose 'Use the build number', this will use the build number to version your package. Note: Under Options set the build number format to be '$(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)'.
Options: off, byPrereleaseNumber, byEnvVar, byBuildNumber
includeReferencedProjects
Environment variable
Enter the variable name without $, $env, or %.
majorVersion
Major
The 'X' in version X.Y.Z
minorVersion
Minor
The 'Y' in version X.Y.Z
patchVersion
Patch
The 'Z' in version X.Y.Z
packTimezone
Time zone
Specifies the desired time zone used to produce the version of the package. Selecting UTC is recommended if you're using hosted build agents as their date and time might differ.
Options: utc, local
includeSymbols
Create symbols package
Specifies that the package contains sources and symbols. When used with a .nuspec file, this creates a regular NuGet package file and the corresponding symbols package.
toolPackage
Tool Package
Determines if the output files of the project should be in the tool folder.
buildProperties
Additional build properties
Specifies a list of token=value pairs, separated by semicolons, where each occurrence of $token$ in the .nuspec file will be replaced with the given value. Values can be strings in quotation marks.
basePath
Base path
The base path of the files defined in the nuspec file.
verbosityPack
Verbosity
Specifies the amount of detail displayed in the output.
Options: Quiet, Normal, Detailed
arguments
Command and arguments
The command and arguments which will be passed to NuGet.exe for execution. If NuGet 3.5 or later is used, authenticated commands like list, restore, and publish against any feed in this organization/collection that the Project Collection Build Service has access to will be automatically authenticated.
Control options

Versioning schemes

For byPrereleaseNumber, the version will be set to whatever you choose for major, minor, and patch, plus the date and time in the format yyyymmdd-hhmmss.

For byEnvVar, the version will be set as whatever environment variable, e.g. MyVersion (no $, just the environment variable name), you provide. Make sure the environment variable is set to a proper SemVer e.g. 1.2.3 or 1.2.3-beta1.

For byBuildNumber, the version will be set to the build number, ensure that your build number is a proper SemVer e.g. 1.0.$(Rev:r). If you select byBuildNumber, the task will extract a dotted version, 1.2.3.4 and use only that, dropping any label. To use the build number as is, you should use byEnvVar as described above, and set the environment variable to BUILD_BUILDNUMBER.

Examples

Restore

Restore all solutions. Packages are restored into a packages folder alongside solutions using currently selected feeds.

# Restore project
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: Select
    restoreSolution: '**/*.sln'

Package

Package a your solution to your Artifact Staging directory

# Package a project
- task: NuGetCommand@2
  inputs:
    command: 'pack'
    packagesToPack: '**/*.csproj'
    packDestination: '$(Build.ArtifactStagingDirectory)'

Push

Note

Release pipelines download pipeline artifacts to System.ArtifactsDirectory so you can use the $(System.ArtifactsDirectory)/**/*.nupkg for the packagesToPush input in release pipelines.

Push/Publish a package to a feed defined in your NuGet.config.

# Push a project
- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg'
    feedsToUse: 'config'
    nugetConfigPath: '$(Build.WorkingDirectory)/NuGet.config'

Push/Publish a package to a feed you define in the task

# Push a project
- task: NuGetCommand@2
  inputs:
    command: 'push'
    feedsToUse: 'select'
    publishVstsFeed: 'myTestFeed'

Push/Publish a package to NuGet.org

# Push a project
- task: NuGetCommand@2
  inputs:
    command: 'push'
    feedsToUse: 'config'
    includeNugetOrg: 'true'

Open source

These tasks are open source on GitHub. Feedback and contributions are welcome.

Q & A

Why should I check in a NuGet.Config?

Checking a NuGet.Config into source control ensures that a key piece of information needed to build your project-the location of its packages-is available to every developer that checks out your code.

However, for situations where a team of developers works on a large range of projects, it's also possible to add an Azure Artifacts feed to the global NuGet.Config on each developer's machine. In these situations, using the "Feeds I select here" option in the NuGet task replicates this configuration.

Where can I learn about Azure Artifacts?

Package Management in Azure Artifacts and TFS

Where can I learn more about NuGet?

NuGet Docs Overview

NuGet Create Packaging and publishing

NuGet Consume Setting up a solution to get dependencies

What other kinds of apps can I build?

Build your app

What other kinds of build tasks are available?

Specify your build tasks

How do we protect our codebase from build breaks?

How do I modify other parts of my build pipeline?

I selected parallel multi-configuration, but only one build is running at a time.

If you're using Azure Pipelines, you might need more parallel jobs. See Parallel jobs in Azure Pipelines.

How do I see what has changed in my build pipeline?

View the change history of your build pipeline

Do I need an agent?

You need at least one agent to run your build or release.

I'm having problems. How can I troubleshoot them?

See Troubleshoot Build and Release.

I can't select a default agent pool and I can't queue my build or release. How do I fix this?

See Agent pools.

I use TFS on-premises and I don't see some of these features. Why not?

Some of these features are available only on Azure Pipelines and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.