.NET Core CLI task
Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017
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
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.
# .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 #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:
|The dotnet command to run. Select |
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 |
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 |
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)'
|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
Path to project(s)
|The path to the csproj file(s) to use. You can use wildcards (e.g. |
Disable local cache
|Prevents NuGet from using packages from local machine caches.|
|Specifies the folder in which packages are installed. If no folder is specified, packages are restored into the default NuGet package cache|
Additional build properties
|Specifies a list of |
|Specifies the amount of detail displayed in the output for the |
|Specifies the amount of detail displayed in the output for the |
|Current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory)|
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 |
Target feed location
|Specifies whether the target feed is internal or external.|
|Select a feed hosted in your organization. You must have Package Management installed and licensed to select a feed here|
Publish pipeline metadata
|Associate this build/release pipeline’s metadata (run ID, source code information) with the package|
|The NuGet service connection that contains the external NuGet server’s credentials.|
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 |
Configuration to Package
|When using a csproj file this specifies the configuration to package.|
|Folder where packages will be created. If empty, packages will be created alongside the csproj file.|
Do not build
|Don't build the project before packing. Corresponds to the |
|Additionally creates symbol NuGet packages. Corresponds to the |
|Includes source code in the package. Corresponds to the |
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.|
Zip Published Projects
|If true, folder created by the publish command will be zipped.|
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.|
Publish test results
|Enabling this option will generate a test results TRX file in |
This option appends
Test run title
|Provides a name for the test run|
|The command to pass to dotnet.exe for execution.|
For a full list of available commands, see the dotnet CLI documentation
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. Note that this is not supported for the test command.|
Use packages from NuGet.org
|Include NuGet.org in the generated NuGet.config000 0.|
Path to NuGet.config
|The NuGet.config in your repository that specifies the feeds from which to restore packages.|
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|
|Enter the variable name without $, $env, or %|
|The 'X' in version X.Y.Z.|
|The 'Y' in version X.Y.Z.|
|The 'Z' in version X.Y.Z.|
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 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 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 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)
Run tests in your repository
# Run tests and auto publish test results. - task: DotNetCoreCLI@2 inputs: command: 'test'
Q & A
Why is my build, publish, or test step failing to restore packages?
dotnet commands, including
test 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.
In addition, the
test command does not recognize the
vstsFeed arguments and feeds specified in this manner will not be included in the generated NuGet.config file when the implicit
restore step runs. It is recommended that an explicit
dotnet restore step be used to restore packages. The
restore command respects the
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.
File structure for output files is different from previous builds
Azure DevOps hosted agents are configured with .Net Core 3.0, 2.1 and 2.2. CLI for .Net Core 3.0 has a different behavior while publishing projects using output folder argument. When publishing projects with the output folder argument (-o), the output folder is created in the root directory and not in the project file’s directory. Hence while publishing more than one projects, all the files are published to the same directory, which causes an issue.
To resolve this issue, use the Add project name to publish path parameter (modifyOutputPath in YAML) in the .Net Core CLI task. This creates a sub folder with project file’s name, inside the output folder. Hence all your projects will be published under different sub-folder’s inside the main output folder.
steps: - task: DotNetCoreCLI@2 displayName: 'dotnet publish' inputs: command: publish publishWebProjects: false projects: '**/*.csproj' arguments: '-o testpath' zipAfterPublish: false modifyOutputPath: true
Project using Entity Framework has stopped working on Hosted Agents
The latest .NET Core: 3.0 does not have Entity Framework(EF) built-in. You will have to either install EF before beginning execution or add global.json to the project with required .NET Core SDK version. This will ensure that correct SDK is used to build EF project. If the required version is not present on the machine, add UseDotNetV2 task to your pipeline to install the required version. Learn more about EF with .NET Core 3.0
This task is open source on GitHub. Feedback and contributions are welcome.