使用此工作在macOS、Linux或 Windows 上執行 Bash 腳本。
注意
在 Windows 主機上,這會從 WSL 預設散發套件執行 bash。 必須安裝 WSL,且代理程式執行身分的用戶必須具有散發設定。 WSL 會安裝在Microsoft裝載的 Windows 代理程式映像上。 如需詳細資訊,請參閱 Microsoft 裝載的代理程式 - 軟體。
語法
# 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.
輸入
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'。
腳本的內容。
標準錯誤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 上執行 which bash 或在 Windows 上執行 where 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'
注意
請注意,如果在Bash工作中定義 bashEnvValue 輸入,管線工作會以 BASH_ENV 輸入的值來覆寫 bashEnvValue 變數的值,以防環境中已定義 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 |
| 需求 | 沒有 |
| 功能 | 此工作不符合作業中後續工作的任何需求。 |
| 命令限制 | 任何 |
| Settable 變數 | 任何 |
| 代理程式版本 | 2.115.0 或更新 |
| 工作類別 | 效用 |