.NET Core CLI task

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017

Azure Pipelines

Use this task in a build or release pipeline to build, test, package, or publish a dotnet application, or to run a custom dotnet command. For package commands, this task supports NuGet.org and authenticated feeds like Package Management and MyGet.

If your .NET Core or .NET Standard build depends on NuGet packages, make sure to add two copies of this step: one with the restore command and one with the build command.

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.

YAML snippet

# .NET Core
# Build, test, package, or publish a dotnet application, or run a custom dotnet command
- task: DotNetCoreCLI@2
  inputs:
    #command: 'build' # Options: build, push, pack, publish, restore, run, test, custom
    #publishWebProjects: true # Required when command == Publish
    #projects: # Optional
    #custom: # Required when command == Custom
    #arguments: # Optional
    #publishTestResults: true # Optional
    #testRunTitle: # Optional
    #zipAfterPublish: true # Optional
    #modifyOutputPath: true # Optional
    #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 
    restoreDirectory: 
    #verbosityRestore: 'Detailed' # Options: -, quiet, minimal, normal, detailed, diagnostic
    #packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg' # Required when command == Push
    #nuGetFeedType: 'internal' # Required when command == Push# Options: internal, external
    #publishVstsFeed: # Required when command == Push && NuGetFeedType == Internal
    #publishPackageMetadata: true # Optional
    #publishFeedCredentials: # Required when command == Push && NuGetFeedType == External
    #packagesToPack: '**/*.csproj' # Required when command == Pack
    #configuration: '$(BuildConfiguration)' # Optional
    #packDirectory: '$(Build.ArtifactStagingDirectory)' # Optional
    #nobuild: false # Optional
    #includesymbols: false # Optional
    #includesource: false # Optional
    #versioningScheme: 'off' # Options: off, byPrereleaseNumber, byEnvVar, byBuildNumber
    #versionEnvVar: # Required when versioningScheme == ByEnvVar
    #majorVersion: '1' # Required when versioningScheme == ByPrereleaseNumber
    #minorVersion: '0' # Required when versioningScheme == ByPrereleaseNumber
    #patchVersion: '0' # Required when versioningScheme == ByPrereleaseNumber
    #buildProperties: # Optional
    #verbosityPack: 'Detailed' # Options: -, quiet, minimal, normal, detailed, diagnostic
    workingDirectory: 

Arguments

ArgumentDescription
command
Command
The dotnet command to run. Select custom to add arguments or use a command not listed here.
Options: build, push, pack, restore, run, test, custom
selectOrConfig
Feeds to use
You can either choose to 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 using the nugetConfigPath argument.
Options: select, config
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,

arguments
Arguments
Arguments to the selected command. For example, build configuration, output folder, runtime. The arguments depend on the command selected
Note: This input only currently accepts arguments for build, publish, run, test, custom. If you would like to add arguments for a command not listed, use custom.
projects
Path to project(s)
The path to the csproj file(s) to use. You can use wildcards (e.g. **/*.csproj for all .csproj files in all subfolders).
noCache
Disable local cache
Prevents NuGet from using packages from local machine caches.
packagesDirectory
Destination directory
Specifies the folder in which packages are installed. If no folder is specified, packages are restored into the default NuGet package cache
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
verbosityPack
Verbosity
Specifies the amount of detail displayed in the output for the pack command.
verbosityRestore
Verbosity
Specifies the amount of detail displayed in the output for the restore command.
workingDirectory
Working Directory
Current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory)
searchPatternPush
Path to NuGet package(s) to publish
The pattern to match or path to nupkg files to be uploaded. Multiple patterns can be separated by a semicolon, and you can make a pattern negative by prefixing it with -:. Example: **/*.nupkg;-:**/*.Tests.nupkg.
nuGetFeedType
Target feed location
Specifies whether the target feed is internal or external.
Options: internal, external
feedPublish
Target feed
Select a feed hosted in your organization. You must have Package Management installed and licensed to select a feed here
publishPackageMetadata
Publish pipeline metadata
Associate this build/release pipeline’s metadata (run ID, source code information) with the package
externalEndpoint
NuGet server
The NuGet service connection that contains the external NuGet server’s credentials.
searchPatternPack
Path to csproj or nuspec file(s) to pack
Pattern to search for csproj or nuspec files 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
configurationToPack
Configuration to Package
When using a csproj file this specifies the configuration to package.
outputDir
Package Folder
Folder where packages will be created. If empty, packages will be created alongside the csproj file.
nobuild
Do not build
Don't build the project before packing. Corresponds to the --no-build command line parameter.
includesymbols
Include Symbols
Additionally creates symbol NuGet packages. Corresponds to the --include-symbols command line parameter.
includesource
Include Source
Includes source code in the package. Corresponds to the --include-source command line parameter.
publishWebProjects
Publish Web Projects
If true, the task will try to find the web projects in the repository and run the publish command on them. Web projects are identified by presence of either a web.config file or wwwroot folder in the directory.
zipAfterPublish
Zip Published Projects
If true, folder created by the publish command will be zipped.
modifyOutputPath
Add project name to publish path
If true, folders created by the publish command will have project file name prefixed to their folder names when output path is specified explicitly in arguments. This is useful if you want to publish multiple projects to the same folder.
publishTestResults
Publish test results
Enabling this option will generate a test results TRX file in $(Agent.TempDirectory) and results will be published to the server.
This option appends --logger trx --results-directory $(Agent.TempDirectory) to the command line arguments.
testRunTitle
Test run title
Provides a name for the test run
custom
Custom command
The command to pass to dotnet.exe for execution.
For a full list of available commands, see the dotnet CLI documentation
feedRestore
Use packages from this Azure Artifacts/TFS feed
Include the selected feed in the generated NuGet.config. You must have Package Management installed and licensed to select a feed here.
includeNuGetOrg
Use packages from NuGet.org
Include NuGet.org in the generated NuGet.config000 0.
nugetConfigPath
Path to NuGet.config
The NuGet.config in your repository that specifies the feeds from which to restore packages.
externalEndpoints
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
versionEnvVar
Environment variable
Enter the variable name without $, $env, or %
requestedMajorVersion
Major
The 'X' in version X.Y.Z.
requestedMinorVersion
Minor
The 'Y' in version X.Y.Z.
requestedPatchVersion
Patch
The 'Z' in version X.Y.Z.
Control options

Examples

Build

Build a project

# Build project
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'

Build Multiple Projects

# Build multiple projects
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: |
     src/proj1/proj1.csproj 
     src/proj2/proj2.csproj 
     src/other/other.sln    # Pass a solution instead of a csproj.

Push

Push NuGet packages to internal feed

# Push non test NuGet packages from a build to internal organization Feed
- task: DotNetCoreCLI@2
  inputs:
    command: 'push'
    searchPatternPush: '$(Build.ArtifactStagingDirectory)/*.nupkg;-:$(Build.ArtifactStagingDirectory)/*.Tests.nupkg'
    feedPublish: 'FabrikamFeed'

Push NuGet packages to external feed

# Push all NuGet packages from a build to internal organization Feed
- task: DotNetCoreCLI@2
  inputs:
    command: 'push'
    nugetFeedType: 'external'
    externalEndPoint: 'MyNuGetServiceConnection'

Pack

Pack a NuGetPackage to a specific output directory

# Pack a NuGet package to a test directory
- task: DotNetCoreCLI@2
  inputs: 
    command: 'pack'
    outputDir: '$(Build.ArtifactStagingDirectory)/TestDir'

Pack a Symbol Package

# Pack a symbol package along with NuGet package
- task: DotNetCoreCLI@2
  inputs: 
    command: 'pack'
    includesymbols: true

Publish

Publish a package to external endpoint

# Publish package to an external endpoint with a stored NuGet config file and stored credentials.
- task: DotNetCoreCLI@2
  inputs:
    command: 'pack'
    selectOrConfig: 'config'
    nugetConfigPath: '$(System.DefaultWorkingDirectory)/NuGet.Config'
    externalEndpoints: $(externalFeedCredential)

Test

Run tests in your repository

# Run tests and auto publish test results.
- task: DotNetCLI@2
  inputs:
    command: 'test'

Q & A

Why is my build or publish step failing to restore packages?

Most dotnet commands, including build and publish, include an implicit restore step. This will fail against authenticated feeds, even if you ran a successful dotnet restore in an earlier step, because the earlier step will have cleaned up the credentials it used.

To fix this issue, add the --no-restore flag to the Arguments textbox.

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.

Open Source

This task is open source on GitHub. Feedback and contributions are welcome.