Share via


NuGetCommand@2 - NuGet v2 task

Use this task to restore, pack, or push NuGet packages, or run a NuGet command. This task supports NuGet.org and authenticated feeds like Azure Artifacts and MyGet. This task also uses NuGet.exe and works with .NET Framework apps. For .NET Core and .NET Standard apps, use the .NET Core task.

Inputs

command - Command
string. Required. Allowed values: restore, pack, push, custom. Default value: restore.

Specifies the NuGet command to run. Use the custom value to add arguments or to use a different command.


restoreSolution - Path to solution, packages.config, or project.json
Input alias: solution. string. Required when command = restore. Default value: **/*.sln.

Specifies the path to the solution, packages.config, or project.json file that references the packages to be restored.


feedsToUse - Feeds to use
Input alias: selectOrConfig. string. Required when command = restore. Allowed values: select (Feed(s) I select here), config (Feeds in my NuGet.config). Default value: select.

Specifies a feed from Azure Artifacts and/or NuGet.org for the task to use with the select value. Alternatively, you can commit a NuGet.config file to your source code repository and set its path as the value using the config value.


vstsFeed - Use packages from this VSTS/TFS feed
Input alias: feedRestore. string. Optional. Use when selectOrConfig = select && command = restore.

Specifies the selected feed in the generated NuGet.config. You must have Package Management installed and licensed to specify a feed here.


includeNuGetOrg - Use packages from NuGet.org
boolean. Optional. Use when selectOrConfig = select && command = restore. Default value: true.

Includes NuGet.org in the generated NuGet.config.


nugetConfigPath - Path to NuGet.config
string. Optional. Use when selectOrConfig = config && command = restore.

Specifies the path to the NuGet.config in your repository that determines the feeds from which to restore packages.


externalFeedCredentials - Credentials for feeds outside this account/collection
Input alias: externalEndpoints. string. Optional. Use when selectOrConfig = config && command = restore.

Specifies the credentials to use for external registries located in the selected NuGet.config. This is the name of your NuGet service connection. For feeds in this account or collection, leave this blank; the build's credentials are used automatically.


noCache - Disable local cache
boolean. Optional. Use when command = restore. Default value: false.

Prevents NuGet from using packages from local machine caches when set to true.


restoreDirectory - Destination directory
Input alias: packagesDirectory. string. Optional. Use when command = restore.

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
string. Optional. Use when command = restore. Allowed values: Quiet, Normal, Detailed. Default value: Detailed.

Specifies the amount of detail displayed in the output.


packagesToPush - Path to NuGet package(s) to publish
Input alias: searchPatternPush. string. Required when command = push. Default value: $(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg.

Specifies the pattern to match or path to nupkg files to be uploaded. Multiple patterns can be separated by a semicolon.


nuGetFeedType - Target feed location
string. Required when command = push. Allowed values: internal (This account/collection), external (External NuGet server (including other accounts/collections)). Default value: internal.

Specifies whether the target feed is an internal feed/collection or an external NuGet server.


publishVstsFeed - Target feed
Input alias: feedPublish. string. Required when command = push && nuGetFeedType = internal.

Specifies a feed hosted in this account. You must have Azure Artifacts installed and licensed to select a feed here.


allowPackageConflicts - Allow duplicates to be skipped
boolean. Optional. Use when command = push && nuGetFeedType = internal. Default value: false.

Reports task success even if some of your packages are rejected with 409 Conflict errors.

This option is currently only available on Azure Pipelines and Windows agents. If NuGet.exe encounters a conflict, the task will fail. This option will not work and publishing will fail if you are within a proxy environment.


publishFeedCredentials - NuGet server
Input alias: externalEndpoint. string. Required when command = push && nuGetFeedType = external.

Specifies the NuGet service connection that contains the external NuGet server’s credentials.


verbosityPush - Verbosity
string. Optional. Use when command = push. Allowed values: Quiet, Normal, Detailed. Default value: Detailed.

Specifies the amount of detail displayed in the output.


packagesToPack - Path to csproj or nuspec file(s) to pack
Input alias: searchPatternPack. string. Required when command = pack. Default value: **/*.csproj.

Specifies the pattern that the task uses 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
Input alias: configurationToPack. string. Optional. Use when command = pack. Default value: $(BuildConfiguration).

Specifies the configuration to package when using a csproj file.


packDestination - Package folder
Input alias: outputDir. string. Optional. Use when command = pack. Default value: $(Build.ArtifactStagingDirectory).

Specifies the folder where the task creates packages. If the value is empty, the task creates packages at the source root.


versioningScheme - Automatic package versioning
string. Required when command = pack. Allowed values: off, byPrereleaseNumber (Use the date and time), byEnvVar (Use an environment variable), byBuildNumber (Use the build number). Default value: off.

Applies automatic package versioning depending on the specified value. This string cannot be used with includeReferencedProjects. The allowed values are:

  • byPrereleaseNumber - Use the date and time: The task will generate a SemVer-compliant version formatted as X.Y.Z-ci-datetime, where you specify the values of X, Y, and Z.
  • byEnvVar- Use an environment variable: The task will use an environment variable that you specify and contains the version number you want to use.
  • byBuildNumber - Use the build number: The task will use the build number to version the package.

Note

Under General, set the build format to be $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r).


includeReferencedProjects - Include referenced projects
boolean. Optional. Use when versioningScheme = off && command = pack. Default value: false.

Includes referenced projects either as dependencies or as part of the package. Cannot be used with automatic package versioning. If a referenced project has a corresponding nuspec file that has the same name as the project, then that referenced project is added as a dependency. Otherwise, the referenced project is added as part of the package. Learn more about using the pack command for NuGet CLI to create NuGet packages.


versionEnvVar - Environment variable
string. Required when versioningScheme = byEnvVar && command = pack.

Specifies the variable name without $, $env, or %.


majorVersion - Major
Input alias: requestedMajorVersion. string. Required when versioningScheme = byPrereleaseNumber && command = pack. Default value: 1.

The X in version X.Y.Z.


minorVersion - Minor
Input alias: requestedMinorVersion. string. Required when versioningScheme = byPrereleaseNumber && command = pack. Default value: 0.

The Y in version X.Y.Z.


patchVersion - Patch
Input alias: requestedPatchVersion. string. Required when versioningScheme = byPrereleaseNumber && command = pack. Default value: 0.

The Z in version X.Y.Z.


packTimezone - Time zone
string. Optional. Use when versioningScheme = byPrereleaseNumber && command = pack. Allowed values: utc, local (Agent local time). Default value: utc.

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.


includeSymbols - Create symbols package
boolean. Optional. Use when command = pack. Default value: false.

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
boolean. Optional. Use when command = pack. Default value: false.

Determines if the output files of the project should be in the tool folder.


buildProperties - Additional build properties
string. Optional. Use when command = pack.

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.


verbosityPack - Verbosity
string. Optional. Use when command = pack. Allowed values: Quiet, Normal, Detailed. Default value: Detailed.

Specifies the amount of detail displayed in the output.


arguments - Command and arguments
string. Required when command = custom.

Specifies the command and arguments that 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 or collection that the Project Collection Build Service has access to will be automatically authenticated.


Task control options

All tasks have control options in addition to their task inputs. For more information, see Control options and common task properties.

Output variables

None.

Remarks

Important

This task no longer takes new features, and only critical bugs are addressed.

Use this task to install and update NuGet package dependencies, or package and publish NuGet packages. Uses NuGet.exe and works with .NET Framework apps. For .NET Core and .NET Standard apps, use the .NET Core task.

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

If you are working with .NET Core or .NET Standard, use the .NET Core task, which has full support for all package scenarios and is currently supported by dotnet.

Tip

This version of the NuGet task uses NuGet 4.1.0 by default. To select a different version of NuGet, use the Tool Installer.

Versioning schemes

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

For byEnvVar, the version will be set to the value of the environment variable that has the name specified by the versionEnvVar parameter, e.g. MyVersion (no $, just the environment variable name). Make sure the environment variable is set to a proper SemVer, such as 1.2.3 or 1.2.3-beta1.

For byBuildNumber, the version will be set using the pipeline run's build number. This is the value specified for the pipeline's name property, which gets saved to the BUILD_BUILDNUMBER environment variable). Ensure that the build number being used contains a proper SemVer, such as 1.0.$(Rev:r). When using byBuildNumber, the task will extract the dotted version, 1.2.3.4, from the build number string, and use only that portion. The rest of the string will be dropped. If you want to use the build number as is, you can use byEnvVar as described above, and set versionEnvVar to BUILD_BUILDNUMBER.

Migrate from NuGetInstaller@0 or NuGetRestore@1

NuGetInstaller@0 and NuGetRestore@1 are deprecated, and you should replace them in your pipeline with NuGetCommand@2.

If you were using NuGetInstaller@0 with restoreMode: restore, configure the following inputs when using NuGetCommand@2.

NuGetCommand@2 task input Value
command restore
restoreSolution Path the .sln file

If you were using NuGetInstaller@0 with restoreMode: install, configure the following inputs when using NuGetCommand@2.

NuGetCommand@2 task input Value
command custom
arguments What the full install command would look like in the NuGet CLI. For example, if you want to run the equivalent of nuget install ninject -OutputDirectory c:\proj in your pipeline, then the arguments parameter would be install ninject -OutputDirectory c:\proj. If you were using the NuGetInstaller@0 nuGetRestoreArgs parameter these also now go in arguments.

If you were using NuGetRestore@1, configure the following inputs when using NuGetCommand@2.

NuGetCommand@2 task input Value
command restore
restoreSolution Path the .sln file

Similar to using NuGetRestore@1 or the NuGetInstaller@0 restore option, NuGetCommand@2 has inputs to set the feed, decide between select or config, specify the path to the NuGet.config file, and use packages from nuget.org.

For more information, see the following examples.

Examples

Restore

Restore all your solutions with packages from a selected feed.

# Restore from a project scoped feed in the same organization
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    vstsFeed: 'my-project/my-project-scoped-feed'
    includeNuGetOrg: false
    restoreSolution: '**/*.sln'
# Restore from an organization scoped feed in the same organization
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    vstsFeed: 'my-organization-scoped-feed'
    restoreSolution: '**/*.sln'
# Restore from a feed in a different organization
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: config
    nugetConfigPath: ./nuget.config
    restoreSolution: '**/*.sln'
    externalFeedCredentials: 'MyServiceConnectionName'
    noCache: true
  continueOnError: true
# Restore from feed(s) set in nuget.config
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    feedsToUse: 'config'
    nugetConfigPath: 'nuget.config'

Package

Create a NuGet package in the destination folder.

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

Push

Note

Pipeline artifacts are downloaded to the Pipeline.Workspace directory, and to the System.ArtifactsDirectory directory for classic release pipelines. packagesToPush value can be set to $(Pipeline.Workspace)/**/*.nupkg or $(System.ArtifactsDirectory)/**/*.nupkg respectively.

  • 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 an organization scoped feed

    # Push a project
    - task: NuGetCommand@2
      inputs:
        command: 'push'
        nuGetFeedType: 'internal'
        publishVstsFeed: 'my-organization-scoped-feed'
    
  • Push/Publish a package to a project scoped feed

    # Push a project
    - task: NuGetCommand@2
      inputs:
        command: 'push'
        nuGetFeedType: 'internal'
        publishVstsFeed: 'my-project/my-project-scoped-feed'
    
  • Push/Publish a package to NuGet.org

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

Custom

Run any other NuGet command besides the default ones: pack, push, and restore.

# list local NuGet resources.
- task: NuGetCommand@2
  displayName: 'list locals'
  inputs:
    command: custom
    arguments: 'locals all -list'

Requirements

Requirement Description
Pipeline types YAML, Classic build, Classic release
Runs on Agent, DeploymentGroup
Demands None
Capabilities This task does not satisfy any demands for subsequent tasks in the job.
Command restrictions Any
Settable variables Any
Agent version 2.115.0 or greater
Task category Package