CmdLine@2 - 命令行 v2 任务

使用此任务可在 Windows 上使用 Bash 在 Linux、macOS 和 cmd.exe 上运行命令行脚本。

语法

# Command Line v2
# Run a command line script using Bash on Linux and macOS and cmd.exe on Windows.
- task: CmdLine@2
  inputs:
    script: # string. Required. Script. 
  # Advanced
    #workingDirectory: # string. Working Directory. 
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.

输入

script - 脚本
string。 必填。 默认值:echo Write your commands here\n\necho Hello world

要运行的脚本的内容。


workingDirectory - 工作目录
string

指定要运行命令的工作目录。 如果将其留空,则工作目录 $(Build.SourcesDirectory)


标准错误failOnStderr - 失败
boolean。 默认值:false

如果该值设置为 true,则任何错误写入标准错误流时,任务将失败。


任务控制选项

除任务输入之外,所有任务都具有控制选项。 有关详细信息,请参阅 控件选项和常见任务属性

输出变量

没有。

注解

命令行在 YAML 中具有快捷方式:steps.script

- script: # inline script
  workingDirectory: #
  displayName: #
  failOnStderr: #
  env: { string: string } # mapping of environment variables to add

运行批处理和 。CMD 文件

Azure Pipelines 将内联脚本内容放入临时批处理文件 (.cmd) 中,以便运行它。 如果要从 Windows CMD 中的另一个批处理文件运行批处理文件,则必须使用 call 命令,否则第一个批处理文件将终止。 这将导致 Azure Pipelines 运行预期脚本,直到第一个批处理文件为止,然后运行批处理文件,然后结束此步骤。 第一个脚本中的其他行不会运行。 在 Azure Pipelines 脚本步骤中执行批处理文件之前,应始终先 call

重要

你可能没有意识到你正在运行批处理文件。 例如,Windows 上的 npm 以及使用 npm install -g安装的任何工具实际上是批处理文件。 始终使用 call npm <command> 在 Windows 上的命令行任务中运行 NPM 命令。

例子

steps:
- script: date /t
  displayName: Get the date
- script: dir
  workingDirectory: $(Agent.BuildDirectory)
  displayName: List contents of a folder
- script: |
    set MYVAR=foo
    set
  displayName: Set a variable and then display all
  env:
    aVarFromYaml: someValue

要求

要求 说明
管道类型 YAML,经典版本,经典版本
运行时间 代理,DeploymentGroup
需求 没有
功能 此任务不满足作业中后续任务的任何要求。
命令限制 任何
Settable 变量 任何
代理版本 所有支持的代理版本。
任务类别 效用

另请参阅