Delen via


AzureCLI@2 - Azure CLI v2-taak

Voer Azure CLI-opdrachten uit voor een Azure-abonnement in een PowerShell Core/shell-script wanneer u deze uitvoert op de Linux-agent. Of voer Azure CLI-opdrachten uit tegen een Azure-abonnement in een PowerShell-/PowerShell Core-/batchscript wanneer u een Windows-agent uitvoert.

Voer Azure CLI-opdrachten uit voor een Azure-abonnement in een PowerShell Core/shell-script wanneer u deze uitvoert op de Linux-agent. Of voer Azure CLI-opdrachten uit voor een Azure-abonnement in een PowerShell-/Powershell Core-/batchscript wanneer u een Windows-agent uitvoert.

Syntaxis

# 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.

Invoer

azureSubscription - Azure Resource Manager-verbinding
Invoeralias: connectedServiceNameARM. string. Verplicht.

Selecteer een Azure Resource Manager-serviceverbinding voor de implementatie.


scriptType - scripttype
string. Verplicht. Toegestane waarden: ps (PowerShell), pscore (PowerShell Core), batch, bash (Shell).

Type script. Selecteer een bash of pscore script wanneer u deze uitvoert op de Linux-agent. U kunt ook een batch, psof pscore script selecteren wanneer u een Windows-agent uitvoert. Een pscore-script kan worden uitgevoerd op platformoverschrijdende agents (Linux, macOS of Windows).


scriptType - scripttype
string. Verplicht. Toegestane waarden: ps (Powershell), pscore (Powershell Core), batch, bash (Shell).

Type script. Selecteer een bash of pscore script wanneer u deze uitvoert op de Linux-agent. U kunt ook een batch, psof pscore script selecteren wanneer u een Windows-agent uitvoert. Een pscore-script kan worden uitgevoerd op platformoverschrijdende agents (Linux, macOS of Windows).


scriptLocation - scriptlocatie
string. Verplicht. Toegestane waarden: inlineScript (inlinescript), scriptPath (scriptpad). Standaardwaarde: scriptPath.

Pad naar het script.


scriptPath - scriptpad
string. Vereist wanneer scriptLocation = scriptPath.

Volledig gekwalificeerd pad van het script. Gebruik .ps1, .batof .cmd bij het gebruik van een Windows-agent. Gebruik .ps1 of .sh bij het gebruik van een Linux-agent of een pad ten opzichte van de standaardwerkmap.


inlineScript - inlinescript
string. Vereist wanneer scriptLocation = inlineScript.

U kunt uw scripts hier inline schrijven. Wanneer u een Windows-agent gebruikt, gebruikt u PowerShell, PowerShell Core of batchscripting. Gebruik PowerShell Core- of shellscripting wanneer u agents op basis van Linux gebruikt. Gebruik voor batchbestanden het voorvoegsel call vóór elke Azure-opdracht. U kunt ook vooraf gedefinieerde en aangepaste variabelen aan dit script doorgeven met behulp van argumenten.

Hier volgt een voorbeeld voor PowerShell/PowerShellCore/shell.

az --version 
az account show 

Hier volgt een voorbeeld voor batch.

call  az --version 
call az account show

arguments - scriptargumenten
Invoeralias: scriptArguments. string.

Argumenten die zijn doorgegeven aan het script.


powerShellErrorActionPreference - ErrorActionPreference
string. Facultatief. Gebruiken wanneer scriptType = ps || scriptType = pscore. Toegestane waarden: stop, continue, silentlyContinue. Standaardwaarde: stop.

Hiermee wordt de regel $ErrorActionPreference = 'VALUE' boven aan uw PowerShell-/PowerShell Core-script voorbereid.


details van addSpnToEnvironment - Access-service-principal in script-
boolean. Standaardwaarde: false.

Hiermee voegt u de service-principal-id, de service-principalsleutel of het federatietoken van de workloadidentiteit en tenant-id van het Azure-eindpunt toe die u hebt gekozen in de uitvoeringsomgeving van het script. U kunt de variabelen servicePrincipalId, servicePrincipalKey of idTokenen tenantId in uw script gebruiken.

Dit wordt alleen uitgevoerd wanneer het Azure-eindpunt een verificatieschema voor service-principal of een verificatieschema voor de federatie van de workloadidentiteit heeft.

In de volgende lijst ziet u de syntaxis voor toegang tot omgevingsvariabelen op basis van het scripttype.

  • Syntaxis van PowerShell-script: $env:servicePrincipalId

  • Syntaxis van Batch-script: %servicePrincipalId%

  • Syntaxis van Shell-script: $servicePrincipalId


useGlobalConfig - globale Azure CLI-configuratie gebruiken
boolean. Standaardwaarde: false.

Als deze invoer onwaar is, gebruikt deze taak een eigen Azure CLI-configuratiemap. Gebruik deze taak om Azure CLI-taken uit te voeren in parallelle releases.


workingDirectory - werkmap
Invoeralias: cwd. string.

Huidige werkmap waarin het script wordt uitgevoerd. Als u niets opgeeft, is deze invoer de hoofdmap van de opslagplaats (build) of artefacten (release), die $(System.DefaultWorkingDirectory).


failOnStandardError - mislukt bij standaardfout
boolean. Standaardwaarde: false.

Als deze invoer waar is, mislukt deze taak wanneer er fouten naar de StandardError-stroom worden geschreven. Schakel het selectievakje uit om standaardfouten te negeren en vertrouw in plaats daarvan op afsluitcodes om de status te bepalen.


powerShellIgnoreLASTEXITCODE - $LASTEXITCODE negeren
boolean. Facultatief. Gebruiken wanneer scriptType = ps || scriptType = pscore. Standaardwaarde: false.

Als deze invoer onwaar is, wordt de regel if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE } toegevoegd aan het einde van het script. Hiermee wordt de laatste afsluitcode van een externe opdracht doorgegeven als de afsluitcode van PowerShell. Anders wordt de regel niet toegevoegd aan het einde van het script.


visibleAzLogin - az login output visibility
boolean. Standaardwaarde: true.

Als dit is ingesteld op true, wordt de opdracht az login uitgevoerd naar de taak. Als u deze instelt op false, wordt de az login-uitvoer onderdrukt.


Opties voor taakbeheer

Alle taken hebben besturingsopties naast hun taakinvoer. Zie Opties en algemene taakeigenschappenvoor meer informatie.

Uitvoervariabelen

Geen.

Opmerkingen

Wat is er nieuw in versietaak versie 2.0

  • Ondersteuning voor PowerShell- en PowerShell Core-script.
  • PowerShell Core werkt met platformoverschrijdende agents (Linux, macOS of Windows), zorg ervoor dat de agent PowerShell versie 6 of meer heeft.
  • PowerShell-script werkt alleen met Windows-agent, zorg ervoor dat de agent PowerShell versie 5 of lager heeft.

Benodigdheden

  • Een Microsoft Azure-abonnement.
  • Azure Resource Manager-serviceverbinding met uw Azure-account.
  • Door Microsoft gehoste agents hebben Azure CLI vooraf geïnstalleerd. Als u echter privéagents gebruikt, installeert u Azure CLI op de computers waarop de build- en releaseagent wordt uitgevoerd. Als een agent al wordt uitgevoerd op de computer waarop de Azure CLI is geïnstalleerd, start u de agent opnieuw om ervoor te zorgen dat alle relevante fasevariabelen worden bijgewerkt.

Voorbeelden

In het volgende voorbeeld wordt de versie van Azure CLI vermeld en worden de details van het abonnement opgehaald.

- 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

In het volgende voorbeeld ziet u hoe u argumenten doorgeeft aan uw script.

  • Argumenten doorgeven aan inlinescripts:

    - 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'
    
  • Argumenten doorgeven met scriptpad:

    - 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'
    

Vereisten

Voorwaarde Beschrijving
Pijplijntypen YAML, klassieke build, klassieke release
Wordt uitgevoerd op Agent, DeploymentGroup
eisen Geen
mogelijkheden Deze taak voldoet niet aan de vereisten voor volgende taken in de taak.
opdrachtbeperkingen Welk dan ook
variabelen instellen Welk dan ook
Agentversie 2.0.0 of hoger
Taakcategorie Implementeren

Zie ook