Azure CLI task

Azure Pipelines

Use this task to run a shell or batch script containing Azure CLI commands against an Azure subscription. This task is used to run Azure CLI commands on cross-platform agents running on Linux, macOS, or Windows operating systems.

What's new in Version 2.0

  • Supports running PowerShell and PowerShell Core script.
  • PowerShell Core script works with Xplat agents (Windows, Linux or OSX), make sure the agent has PowerShell version 6 or higher.
  • PowerShell script works only with Windows agent, make sure the agent has PowerShell version 5 or lower.

Prerequisites

  • A Microsoft Azure subscription.

  • Azure Resource Manager service connection to your Azure account.

  • Microsoft hosted agents have Azure CLI pre-installed. However if you are using private agents, install Azure CLI on the computer(s) that run the build and release agent. If an agent is already running on the machine on which the Azure CLI is installed, restart the agent to ensure all the relevant stage variables are updated.

Task Inputs

Parameters Description
azureSubscription
Azure subscription
(Required) Name of the Azure Resource Manager service connection
scriptType
Script Type
(Required) Type of script:
If you are using a Linux agent, select one of the following types:
  • bash
  • pscore
If you are using a Windows agent, select one of the following types:
  • batch
  • ps
  • pscore

* PowerShell Core scripts can run on cross-platform agents (Linux, macOS, or Windows).
scriptLocation
Script Location
(Required) select scriptPath to use a script file or inlineScript if you want to write your script inline
Default value: scriptPath
scriptPath
Script Path
Required when scriptLocation = scriptPath. Fully qualified path of your script file or a path relative to the default working directory
inlineScript
Inline Script
Required when scriptLocation = inlineScript. Use this option if you want to paste your scripts inline. Use PowerShell, PowerShell Core, or batch scripting for Windows agents and bash or PowerShell core when using Linux-based agents. Use the call prefix before every Azure command when you are using batch. You can also pass predefined and custom variables to your script using arguments.
Examples:
PowerShell/PowerShellCore/shell: az --version az account show
batch: call az --version call az account show
arguments
Script Arguments
(Optional) Arguments passed to your script
powerShellErrorActionPreference
ErrorActionPreference
(Optional) Prepends the line $ErrorActionPreference = 'VALUE' at the top of your PowerShell/PowerShell Core script
Default value: stop
Options are stop, continue, and silentlyContinue
addSpnToEnvironment
Access service principal details in script
(Optional) Adds service principal id and key of the Azure endpoint you chose to the script's execution environment. You can use these variables: $env:servicePrincipalId, $env:servicePrincipalKey and $env:tenantId in your script. This is honored only when the Azure endpoint has Service Principal authentication scheme
Default value: false
useGlobalConfig
Use global Azure CLI configuration
(Optional) If this is false, this task will use its own separate Azure CLI configuration directory. This can be used to run Azure CLI tasks in parallel releases"
Default value: false
workingDirectory
Working Directory
(Optional) Current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory)
failOnStandardError
Fail on Standard Error
(Optional) If this is true, this task will fail when any errors are written to the StandardError stream. Unselect the checkbox to ignore standard errors and rely on exit codes to determine the status
Default value: false
powerShellIgnoreLASTEXITCODE
Ignore $LASTEXITCODE
(Optional) If this is false, the line if ((Test-Path -LiteralPath variable:\\LASTEXITCODE)) { exit $LASTEXITCODE } is appended to the end of your script. This will cause the last exit code from an external command to be propagated as the exit code of PowerShell. Otherwise the line is not appended to the end of your script
Default value: false

Example

Following is an example of a YAML snippet that lists the version of Azure CLI and gets the details of the subscription.

- 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

The following example illustrates how to pass arguments to your script.

- task: AzureCLI@2
  displayName: Azure CLI
  inputs:
    azureSubscription: <Name of the Azure Resource Manager service connection>
    scriptType: ps
    scriptLocation: inlineScript
    arguments:
      -Arg1 val1 `
      -Arg2 val2 `
      -Arg3 val3
    inlineScript: |
      az login --allow-no-subscription

Open-source

This task is open source on GitHub. Feedback and contributions are welcome.

FAQ

Do I need an agent?

You need at least one agent to run your build or release.

I'm having problems. How can I troubleshoot them?

See Troubleshoot Build and Release.

I can't select a default agent pool and I can't queue my build or release. How do I fix this?

See Agent pools.

My NuGet push task is failing with the following error: "Error: unable to get local issuer certificate". How can I fix this?

This can be fixed by adding a trusted root certificate. You can either add the NODE_EXTRA_CA_CERTS=file environment variable to your build agent, or you can add the NODE.EXTRA.CA.CERTS=file task variable in your pipeline. See Node.js documentation for more details about this variable. See Set variables in a pipeline for instructions on setting a variable in your pipeline.