Bash@3 - Bash v3 任务

使用此任务在 macOS、Linux 或 Windows 上运行 Bash 脚本。

备注

在 Windows 主机上,这会从 WSL 默认分发运行 bash。 必须安装 WSL,并且代理运行的用户必须具有分发设置。 WSL 安装在Microsoft托管的 Windows 代理映像上。 有关详细信息,请参阅 Microsoft托管代理 - 软件

语法

YAML
# Bash v3
# Run a Bash script on macOS, Linux, or Windows.
- task: Bash@3
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
  # Advanced
    #workingDirectory: # string. Working Directory. 
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #bashEnvValue: # string. Set value for BASH_ENV environment variable.
YAML
# Bash v3
# Run a Bash script on macOS, Linux, or Windows.
- task: Bash@3
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
  # Advanced
    #workingDirectory: # string. Working Directory. 
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #noProfile: true # boolean. Don't load the profile startup/initialization files. Default: true.
    #noRc: true # boolean. Don't read the `~/.bashrc' initialization file. Default: true.
YAML
# Bash v3
# Run a Bash script on macOS, Linux, or Windows.
- task: Bash@3
  inputs:
    #targetType: 'filePath' # 'filePath' | 'inline'. Type. Default: filePath.
    filePath: # string. Required when targetType = filePath. Script Path. 
    #arguments: # string. Optional. Use when targetType = filePath. Arguments. 
    #script: # string. Required when targetType = inline. Script. 
  # Advanced
    #workingDirectory: # string. Working Directory. 
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.

输入

targetType - 类型
string。 允许的值:filePath(文件路径),inline。 默认值:filePath

目标脚本类型:文件路径或内联。


filePath - 脚本路径
stringtargetType = filePath时是必需的。

要执行的脚本的路径。 这必须是完全限定的路径或相对于 $(System.DefaultWorkingDirectory)


arguments - 参数
string。 自选。 当 targetType = filePath时使用。

传递给 shell 脚本的参数。 序号参数或命名参数。


script - 脚本
stringtargetType = inline时是必需的。 默认值:# Write your commands here\n\necho 'Hello world'

脚本的内容。


script - 脚本
stringtargetType = inline时是必需的。 默认值:# Write your commands here\n\n# Use the environment variables input below to pass secret variables to this script

脚本的内容。


workingDirectory - 工作目录
string

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


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

如果为 true,则如果任何错误都写入 StandardError 流,则此任务将失败。


bashEnvValue - 设置BASH_ENV环境变量 的值
string

如果指定了输入,则其值将展开,并用作运行脚本之前要执行的启动文件的路径。 如果已定义环境变量 BASH_ENV,则任务将仅针对当前任务替代此变量。 详细了解 Bash 启动文件


noProfile - 不要加载配置文件启动/初始化文件
boolean。 默认值:true

不要 /etc/profile 或任何个人初始化文件加载系统范围的启动文件。


noRc - **请勿读取 ~/.bashrc' initialization file**<br> 布尔. Default value: true”。


任务控制选项

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

输出变量

没有。

言论

bash 任务在 YAML 中具有快捷方式:steps.bash

yml
steps:
- bash: string # Required as first property. An inline script. 
  ## Other task inputs

Bash 任务将在系统上找到第一个 Bash 实现。 在 Linux/macOS 上运行 which bash 或在 Windows 上运行 where bash 可让你了解将选择哪个版本。

有关 Bash 启动文件的信息

Bash 任务将 Bash 作为非交互式非登录 shell 调用。 当 Bash 以非交互方式启动时,若要运行 shell 脚本,Bash 会在环境中查找变量 BASH_ENV,如果出现该变量,则展开其值,并使用该值作为文件的名称进行读取和执行。

有多种选项可用于在管道中定义 BASH_ENV 环境变量。 首先,可以将 BASH_ENV 环境变量设置为管道变量。 在这种情况下,Bash 任务的每个实例将尝试展开 BASH_ENV 变量的值并使用其值。

YAML
variables:
  BASH_ENV: "~/.profile"

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: env

另一个选项是为 Bash 任务的一个特定实例设置 BASH_ENV,有两种方法可以执行此操作:

第一种方法是使用 bashEnvValue 任务输入,请参阅参考示例:

YAML
steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: env
    bashEnvValue: '~/.profile'

另一种方法是通过 env 关键字将 BASH_ENV 变量设置为管道任务的环境变量,例如:

YAML
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: env
  env:
    BASH_ENV: '~/.profile'

备注

请注意,如果在 Bash 任务中定义了 bashEnvValue 输入,则管道任务将使用 bashEnvValue 输入中的值替代 BASH_ENV 变量的值,以防环境中已定义 BASH_ENV 环境变量。

签入存储库的 Bash 脚本应设置为可执行文件(chmod +x)。 否则,任务将显示警告,并改为 source 文件。

例子

可以使用 env 参数在变量中进行映射,该参数在所有任务 通用,并且是要映射到进程环境的其他项的列表。 例如,机密变量不会自动映射。 如果你有一个名为 Foo的机密变量,则可以像这样映射它:

YAML
steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: echo $MYSECRET
  env:
    MYSECRET: $(Foo)

在 macOS 或 Linux 上,上述示例等效于:

YAML
steps:
- script: echo $MYSECRET
  env:
    MYSECRET: $(Foo)

要求

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