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/batch 腳本中的 Azure 訂用帳戶執行 Azure CLI 命令。

Syntax

# 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連線
輸入別名: connectedServiceNameARMstring. 必要。

選取部署的 Azure Resource Manager服務連線。


scriptType - 腳本類型
string. 必要。 允許的值: ps (PowerShell) 、 pscore (PowerShell Core) 、 batch 、 (bash Shell) 。

腳本的類型。 在 Linux 代理程式上執行時, bash 請選取 或 pscore 腳本。 或者,在 Windows 代理程式上執行時,請選取 batchpspscore 腳本。 pscore腳本可以在跨平臺代理程式上執行, (Linux、macOS 或 Windows) 。


scriptType - 腳本類型
string. 必要。 允許的值: ps (Powershell) 、 pscore (Powershell Core) 、 batch 、 (bash Shell) 。

腳本的類型。 在 Linux 代理程式上執行時, bash 請選取 或 pscore 腳本。 或者,在 Windows 代理程式上執行時,請選取 batchpspscore 腳本。 pscore腳本可以在跨平臺代理程式上執行, (Linux、macOS 或 Windows) 。


scriptLocation - 腳本位置
string. 必要。 允許的值: inlineScript (內嵌腳本) , scriptPath (腳本路徑) 。 預設值:scriptPath

腳本的路徑。


scriptPath - 腳本路徑
string. 當 scriptLocation = scriptPath 時為必要。

腳本的完整路徑。 使用 .ps1.bat 、 或 .cmd 使用以 Windows 為基礎的代理程式時。 .sh使用 .ps1 或 使用以 Linux 為基礎的代理程式或相對於預設工作目錄的路徑時。


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 - 腳本引數
輸入別名: scriptArgumentsstring.

傳遞至腳本的引數。


powerShellErrorActionPreference - ErrorActionPreference
string. 選擇性。 使用 時 scriptType = ps || scriptType = pscore 。 允許值:stopcontinuesilentlyContinue。 預設值:stop

在 PowerShell/PowerShell Core 腳本頂端加上這一行 $ErrorActionPreference = 'VALUE'


addSpnToEnvironment - 存取腳本中的服務主體詳細資料
boolean. 預設值:false

將服務主體識別碼、服務主體金鑰或工作負載身分識別同盟權杖,以及您選擇的 Azure 端點租使用者識別碼新增至腳本的執行環境。 您可以在腳本中使用 servicePrincipalIdservicePrincipalKeyidTokentenantId 變數。

只有在 Azure 端點具有服務主體驗證配置或工作負載身分識別同盟驗證配置時,才會接受此功能。

下列清單顯示根據腳本類型存取環境變數的語法。

  • PowerShell 腳本語法: $env:servicePrincipalId

  • 批次腳本語法: %servicePrincipalId%

  • 殼層腳本語法: $servicePrincipalId


useGlobalConfig - 使用全域 Azure CLI 組態
boolean. 預設值:false

如果此輸入為 false,此工作將會使用自己的 Azure CLI 組態目錄。 使用此工作以 平行 版本執行 Azure CLI 工作。


workingDirectory - 工作目錄
輸入別名: cwdstring.

執行腳本的目前工作目錄。 如果保留空白,則此輸入是 (組建) 或成品 (發行) 的存放庫根目錄,也就是 $(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 的結束代碼。 否則,此行不會附加至腳本的結尾。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制選項和一般工作屬性

輸出變數

無。

備註

版本工作 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
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任何
可設定變數 任何
代理程式版本 2.0.0 或更新版本
工作類別 部署

另請參閱