Bash@3 - Bash v3 工作

使用此工作在macOS、Linux或 Windows 上執行 Bash 腳本。

Syntax

# 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.
# 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.
# 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 - 腳本路徑
string. 當 targetType = filePath 時為必要。

要執行的腳本路徑。 這必須是完整路徑,或相對於 $(System.DefaultWorkingDirectory)


arguments - 參數
string. 選擇性。 使用時機 targetType = filePath

傳遞至殼層腳本的自變數。 序數參數或具名參數。


script - 腳本
string. 當 targetType = inline 時為必要。 預設值:# Write your commands here\n\necho 'Hello world'

腳本的內容。


script - 腳本
string. 當 targetType = 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

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

Bash 工作會在您的系統上找到第一個Bash實作。 在 Linux/macOS 或 where bash Windows 上執行which bash,可讓您瞭解它將選取哪一個。

Bash 啟動檔案的相關信息

Bash 工作會將Bash叫用為非互動式的非登入殼層。 當Bash以非互動方式啟動時,若要執行殼層腳本,Bash 會在環境中尋找變數 BASH_ENV 、在其出現時展開其值,並使用值做為檔案的名稱來讀取和執行。

有數個選項可用來定義 BASH_ENV 管線中的環境變數。 首先,可以將環境變數設定 BASH_ENV 為管線變數。 在此情況下,Bash 工作的每個實例都會嘗試展開變數的值 BASH_ENV ,並使用其值。

variables:
  BASH_ENV: "~/.profile"

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

另一個選項是為Bash工作的一個特定實例設定 BASH_ENV ,有兩種方式可以完成此作業:

第一種方式是使用 bashEnvValue 工作輸入,請參閱參考範例:

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

另一種方式是透過 關鍵詞將變數設定 BASH_ENV 為管線工作的 env 環境變數,例如:

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

注意

請注意,如果bashEnvValue輸入是在Bash工作中定義,管線工作會以輸入中的值覆寫變數的值BASH_ENVbashEnvValue,以防BASH_ENV環境變數已在環境中定義。

簽入存放庫的Bash腳本應該設定可執行檔 (chmod +x) 。 否則,工作將會改為顯示警告和 source 檔案。

範例

您可以使用所有工作通用的參數來對應變數env,而且是對應至進程環境的其他項目清單。 例如,秘密變數不會自動對應。 如果您有一個名為 Foo的秘密變數,則可以將它對應如下:

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

在macOS或Linux上,上述範例相當於:

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

規格需求

需求 描述
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任何
Settable 變數 任何
代理程式版本 2.115.0 或更新版本
工作類別 公用程式