AzureCLI@2 - Azure CLI v2 工作
在 Linux 代理程式上執行時,針對 PowerShell Core/Shell 腳本中的 Azure 訂用帳戶執行 Azure CLI 命令。 或者,在 Windows 代理程式上執行時,在 PowerShell/PowerShell Core/批次腳本中針對 Azure 訂用帳戶執行 Azure CLI 命令。
在 Linux 代理程式上執行時,針對 PowerShell Core/Shell 腳本中的 Azure 訂用帳戶執行 Azure CLI 命令。 或者,在 Windows 代理程式上執行時,在 PowerShell/Powershell Core/批次腳本中針對 Azure 訂用帳戶執行 Azure CLI 命令。
語法
# Azure CLI v2
# Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
- task: AzureCLI@2
inputs:
azureSubscription: # string. Alias: connectedServiceNameARM. Required. Azure Resource Manager connection.
scriptType: # 'ps' | 'pscore' | 'batch' | 'bash'. Required. Script Type.
scriptLocation: 'scriptPath' # 'inlineScript' | 'scriptPath'. Required. Script Location. Default: scriptPath.
scriptPath: # string. Required when scriptLocation = scriptPath. Script Path.
#inlineScript: # string. Required when scriptLocation = inlineScript. Inline Script.
#arguments: # string. Alias: scriptArguments. Script Arguments.
#powerShellErrorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. Optional. Use when scriptType = ps || scriptType = pscore. ErrorActionPreference. Default: stop.
# Advanced
#addSpnToEnvironment: false # boolean. Access service principal details in script. Default: false.
#useGlobalConfig: false # boolean. Use global Azure CLI configuration. Default: false.
#workingDirectory: # string. Alias: cwd. Working Directory.
#failOnStandardError: false # boolean. Fail on Standard Error. Default: false.
#powerShellIgnoreLASTEXITCODE: false # boolean. Optional. Use when scriptType = ps || scriptType = pscore. Ignore $LASTEXITCODE. Default: false.
#visibleAzLogin: true # boolean. az login output visibility. Default: true.
# Azure CLI v2
# Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
- task: AzureCLI@2
inputs:
azureSubscription: # string. Alias: connectedServiceNameARM. Required. Azure Resource Manager connection.
scriptType: # 'ps' | 'pscore' | 'batch' | 'bash'. Required. Script Type.
scriptLocation: 'scriptPath' # 'inlineScript' | 'scriptPath'. Required. Script Location. Default: scriptPath.
scriptPath: # string. Required when scriptLocation = scriptPath. Script Path.
#inlineScript: # string. Required when scriptLocation = inlineScript. Inline Script.
#arguments: # string. Alias: scriptArguments. Script Arguments.
#powerShellErrorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. Optional. Use when scriptType = ps || scriptType = pscore. ErrorActionPreference. Default: stop.
# Advanced
#addSpnToEnvironment: false # boolean. Access service principal details in script. Default: false.
#useGlobalConfig: false # boolean. Use global Azure CLI configuration. Default: false.
#workingDirectory: # string. Alias: cwd. Working Directory.
#failOnStandardError: false # boolean. Fail on Standard Error. Default: false.
#powerShellIgnoreLASTEXITCODE: false # boolean. Optional. Use when scriptType = ps || scriptType = pscore. Ignore $LASTEXITCODE. Default: false.
# Azure CLI v2
# Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/Powershell Core/Batch script when running on Windows agent.
- task: AzureCLI@2
inputs:
azureSubscription: # string. Alias: connectedServiceNameARM. Required. Azure Resource Manager connection.
scriptType: # 'ps' | 'pscore' | 'batch' | 'bash'. Required. Script Type.
scriptLocation: 'scriptPath' # 'inlineScript' | 'scriptPath'. Required. Script Location. Default: scriptPath.
scriptPath: # string. Required when scriptLocation = scriptPath. Script Path.
#inlineScript: # string. Required when scriptLocation = inlineScript. Inline Script.
#arguments: # string. Alias: scriptArguments. Script Arguments.
#powerShellErrorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. Optional. Use when scriptType = ps || scriptType = pscore. ErrorActionPreference. Default: stop.
# Advanced
#addSpnToEnvironment: false # boolean. Access service principal details in script. Default: false.
#useGlobalConfig: false # boolean. Use global Azure CLI configuration. Default: false.
#workingDirectory: # string. Alias: cwd. Working Directory.
#failOnStandardError: false # boolean. Fail on Standard Error. Default: false.
#powerShellIgnoreLASTEXITCODE: false # boolean. Optional. Use when scriptType = ps || scriptType = pscore. Ignore $LASTEXITCODE. Default: false.
輸入
azureSubscription
-
Azure Resource Manager 連線
輸入別名:connectedServiceNameARM
。
string
。 必須的。
選取部署的 Azure Resource Manager 服務連線。
scriptType
-
文稿類型
string
。 必須的。 允許的值:ps
(PowerShell)、pscore
(PowerShell Core)、batch
、bash
(Shell)。
腳本的類型。 在Linux代理程式上執行時,選取 bash
或 pscore
腳本。 或者,在 Windows 代理程式上執行時,請選取 batch
、ps
或 pscore
腳本。
pscore
腳本可以在跨平臺代理程式 (Linux、macOS 或 Windows) 上執行。
scriptType
-
文稿類型
string
。 必須的。 允許的值:ps
(Powershell)、pscore
(Powershell Core)、batch
、bash
(Shell)。
腳本的類型。 在Linux代理程式上執行時,選取 bash
或 pscore
腳本。 或者,在 Windows 代理程式上執行時,請選取 batch
、ps
或 pscore
腳本。
pscore
腳本可以在跨平臺代理程式 (Linux、macOS 或 Windows) 上執行。
scriptLocation
-
腳本位置
string
。 必須的。 允許的值:inlineScript
(內嵌腳本),scriptPath
(腳本路徑)。 預設值:scriptPath
。
腳本的路徑。
scriptPath
-
文稿路徑
string
。
scriptLocation = scriptPath
時為必要項。
腳本的完整路徑。 使用 Windows 代理程式時,請使用 .ps1
、.bat
或 .cmd
。 使用以 Linux 為基礎的代理程式或相對於預設工作目錄的路徑時,請使用 .ps1
或 .sh
。
inlineScript
-
內嵌腳本
string
。
scriptLocation = inlineScript
時為必要項。
您可以在這裏內嵌撰寫文稿。 使用 Windows 代理程式時,請使用 PowerShell、PowerShell Core 或批次腳本。 使用以Linux為基礎的代理程式時,請使用PowerShell Core或殼層腳本。 針對批處理檔,請在每個 Azure 命令之前,使用前置詞 call
。 您也可以使用自變數,將預先定義的和自定義變數傳遞至此腳本。
以下是 PowerShell/PowerShellCore/shell 的範例。
az --version
az account show
以下是批次的範例。
call az --version
call az account show
arguments
-
腳本自變數
輸入別名:scriptArguments
。
string
。
傳遞至腳本的自變數。
powerShellErrorActionPreference
-
ErrorActionPreference
string
。 選擇性。
scriptType = ps || scriptType = pscore
時使用 。 允許的值:stop
、continue
、silentlyContinue
。 預設值:stop
。
在 PowerShell/PowerShell Core 腳本頂端加上行 $ErrorActionPreference = 'VALUE'
。
在腳本中 addSpnToEnvironment
- Access 服務主體詳細數據
boolean
。 預設值:false
。
將服務主體標識碼、服務主體密鑰或工作負載身分識別同盟令牌,以及您選擇的 Azure 端點租使用者標識元新增至腳本的執行環境。 您可以在腳本中使用 servicePrincipalId
、servicePrincipalKey
或 idToken
變數,以及 tenantId
變數。
只有在 Azure 端點具有服務主體驗證配置或工作負載身分識別同盟驗證配置時,才會接受這項作業。
下列清單顯示根據文稿類型存取環境變數的語法。
PowerShell 腳本語法:
$env:servicePrincipalId
批次文本語法:
%servicePrincipalId%
殼層腳本語法:
$servicePrincipalId
useGlobalConfig
-
使用全域 Azure CLI 組態
boolean
。 預設值:false
。
如果此輸入為 false,此工作將會使用自己的 Azure CLI 組態目錄。 使用此工作在平行 版本中 執行 Azure CLI 工作。
workingDirectory
-
工作目錄
輸入別名:cwd
。
string
。
執行文稿的目前工作目錄。 如果保留空白,則此輸入是存放庫(組建)或成品(release)的根目錄,也就是 $(System.DefaultWorkingDirectory)
。
標準錯誤failOnStandardError
- 失敗
boolean
。 預設值:false
。
如果此輸入為 true,當任何錯誤寫入 StandardError 數據流時,此工作將會失敗。 清除複選框以忽略標準錯誤,而是依賴結束代碼來判斷狀態。
powerShellIgnoreLASTEXITCODE
-
忽略$LASTEXITCODE
boolean
。 選擇性。
scriptType = ps || scriptType = pscore
時使用 。 預設值:false
。
如果此輸入為 false,則會將行 if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE }
附加至腳本結尾。 這會從外部命令傳播最後一個結束代碼作為PowerShell的結束代碼。 否則,此行不會附加至腳本的結尾。
visibleAzLogin
-
az login output visibility
boolean
。 預設值:true
。
如果此值設定為 true,az login 命令會輸出至工作。 將它設定為 false 會隱藏 az login 輸出。
工作控制選項
除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制項選項和一般工作屬性。
輸出變數
沒有。
備註
版本工作 2.0 版的新功能
- 支援 PowerShell 和 PowerShell Core 腳本。
- PowerShell Core 適用於跨平臺代理程式(Linux、macOS 或 Windows),請確定代理程式具有 PowerShell 第 6 版或更新版本。
- Powershell 腳本僅適用於 Windows 代理程式,請確定代理程式具有 PowerShell 第 5 版或更新版本。
先決條件
- Microsoft Azure 訂用帳戶。
- Azure Resource Manager 服務連線 到您的 Azure 帳戶。
- Microsoft 裝載的代理程式已預先安裝 Azure CLI。 然而,若您使用的是私人代理程式,請在執行組建及發行代理程式的電腦上安裝 Azure CLI。 若代理程式已在安裝 Azure CLI 的電腦上執行,請重新啟動代理程式,以確保所有相關的階段變數都已更新。
範例
下列範例會列出 Azure CLI 的版本,並取得訂用帳戶的詳細數據。
- task: AzureCLI@2
displayName: Azure CLI
inputs:
azureSubscription: <Name of the Azure Resource Manager service connection>
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az --version
az account show
下列範例說明如何將自變數傳遞至您的腳本。
將自變數傳遞至內嵌文稿:
- task: AzureCLI@2 inputs: azureSubscription: <Azure_Resource_Manager_Service_Connection> scriptType: 'ps' scriptLocation: 'inlineScript' arguments: '$(AZURE_STORAGE_ACCOUNT) $(AZURE_STORAGE_KEY)' inlineScript: './scripts/publish.ps1 $1 $2'
使用文稿路徑傳遞自變數:
- task: AzureCLI@2 inputs: azureSubscription: <Azure_Resource_Manager_Service_Connection> scriptType: 'ps' scriptLocation: 'scriptPath' arguments: '$(AZURE_STORAGE_ACCOUNT) $(AZURE_STORAGE_KEY)' scriptPath: './scripts/publish.ps1'
需求
要求 | 說明 |
---|---|
管線類型 | YAML、傳統組建、傳統版本 |
執行於 | Agent、DeploymentGroup |
需求 | 沒有 |
功能 | 此工作不符合作業中後續工作的任何需求。 |
命令限制 | 任意 |
Settable 變數 | 任意 |
代理程式版本 | 2.0.0 或更新 |
工作類別 | 部署 |