แชร์ผ่าน


DotNetCoreCLI@2 - .NET Core v2 task

Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet.

Inputs

command - Command
string. Required. Allowed values: build, push (nuget push), pack, publish, restore, run, test, custom. Default value: build.

The dotnet command to run. Specify custom to add arguments or use a command not listed here.


publishWebProjects - Publish Web Projects
boolean. Optional. Use when command = publish. Default value: true.

If this input is set to true, the projects property value is skipped, and the task tries to find the web projects in the repository and run the publish command on them. Web projects are identified by the presence of either a web.config file or a wwwroot folder in the directory. In the absence of a web.config file or a wwwroot folder, projects that use a web SDK, like Microsoft.NET.Sdk.Web, are selected.


projects - Path to project(s)
string. Optional. Use when command = build || command = restore || command = run || command = test || command = custom || publishWebProjects = false.

The path to the .csproj file(s) to use. You can use wildcards (e.g. **/*.csproj for all .csproj files in all subfolders). For more information, see the file matching patterns reference.

This path is relative to the root of the repository regardless of the workingDirectory setting.


custom - Custom command
string. Required when command = custom.

The command to pass to dotnet.exe for execution. For a full list of available commands, see the dotnet CLI documentation.


arguments - Arguments
string. Optional. Use when command = build || command = publish || command = run || command = test || command = custom.

Specifies the arguments for the selected command. For example, build configuration, output folder, and runtime. The arguments depend on the command selected.

This input currently only accepts arguments for build, publish, run, test, and custom. If you would like to add arguments for a command not listed, use custom.


publishTestResults - Publish test results
boolean. Optional. Use when command = test. Default value: true.

Enabling this option will generate a test results TRX file in $(Agent.TempDirectory), and the results will be published to the server.

This option appends the --logger trx --results-directory $(Agent.TempDirectory) to the command line arguments.

Code coverage can be collected by adding the --collect "Code coverage" option to the command line arguments.


zipAfterPublish - Zip Published Projects
boolean. Optional. Use when command = publish. Default value: true.

If this input is set to true, the folder created by the publish command will be zipped and deleted.


modifyOutputPath - Add project name to publish path
boolean. Optional. Use when command = publish. Default value: true.

If this input is set to true, folders created by the publish command will have the project file name prefixed to their folder names when the output path is specified explicitly in arguments. This is useful if you want to publish multiple projects to the same folder.


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.

You can either select a feed from Azure Artifacts and/or NuGet.org here, or you can commit a nuget.config file to your source code repository and set its path using the nugetConfigPath input.


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

Includes 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
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.

The NuGet.config in your repository that specifies 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.

The credentials to use for external registries located in the selected NuGet.config. For feeds in this organization/collection, leave this input 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.


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 the default NuGet package cache.


verbosityRestore - Verbosity
string. Optional. Use when command = restore. Allowed values: -, Quiet, Minimal, Normal, Detailed, Diagnostic. Default value: Detailed.

Specifies the amount of detail displayed in the output for the restore command.


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

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
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 internal or external.


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

Specifies a feed hosted in this organization. You must have Package Management installed and licensed to select a feed here.


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

The NuGet service connection that contains the external NuGet server's credentials.


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

The 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.


configuration - Configuration to Package
Input alias: configurationToPack. string. Optional. Use when command = pack. Default value: $(BuildConfiguration).

When using a .csproj file, this input specifies the configuration to package.


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

The folder where packages will be created. If this folder is empty, packages will be created alongside the csproj file.


nobuild - Do not build
boolean. Optional. Use when command = pack. Default value: false.

Specifies that the task will not build the project before packing. This task corresponds to the --no-build parameter of the build command.


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.

This task cannot be used with included 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 $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r).


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.


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, Minimal, Normal, Detailed, Diagnostic. Default value: Detailed.

Specifies the amount of detail displayed in the output for the pack command.


workingDirectory - Working Directory
string. Optional. Use when command != pack && command != push && command != restore.

The current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory).


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

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

Most dotnet commands, including build, publish, and 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 feedRestore or 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's recommended that an explicit dotnet restore step be used to restore packages. The restore command respects the feedRestore and vstsFeed arguments.

Why am I getting NU1507 warnings with Package Source Mapping although when building on my machine it has no warnings?

The the various commands that do a NuGet restore or access a NuGet feed build a special temporary NuGet.config file that add NuGet authentication for azure artifacts NuGet feeds. The way this is done is in conflict with the schema that Package Source Mapping uses to map the packages to the sources and breaks the Package Source Mappin configuration in the NuGet.config file in your repository.

# Authenticate Azure DevOps NuGet feed
- task: NuGetAuthenticate@1
  displayName: 'Authenticate Azure DevOps NuGet feed'

# Restore project
- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'restore'

# Build project
- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'build'
    arguments: '--no-restore'

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.

Troubleshooting

Project using Entity Framework has stopped working on Hosted Agents

.NET Core 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 the UseDotNetV2 task to your pipeline to install the required version. For more information, see Get the Entity Framework Core runtime.

Examples

Build examples

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 examples

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 external Feed
- task: DotNetCoreCLI@2
  inputs:
    command: 'push'
    nugetFeedType: 'external'
    externalEndPoint: 'MyNuGetServiceConnection'

Pack examples

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 examples

Publish projects to specified folder

# Publish projects to specified folder.
- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/*.csproj'
    arguments: '-o $(Build.ArtifactStagingDirectory)/Output'
    zipAfterPublish: true
    modifyOutputPath: true

Restore examples

#Restore packages with the .NET Core CLI task
- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    feedRestore: 'projectName/feedName'
    projects: '**/*.csproj'
    includeNuGetOrg: true

Test examples

Run tests in your repository

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

Requirements

Requirement Description
Pipeline types YAML, Classic build, Classic release
Runs on Agent
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.0.0 or greater
Task category Build