Azure CLI task

Azure Pipelines

Use this task in a build or release pipeline 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) Select an Azure resource manager subscription for the deployment. This parameter is shown only when the selected task version is 0.* as Azure CLI task v1.0 supports only Azure Resource Manager (ARM) subscriptions
scriptType
Script Type
(Required) Type of script: PowerShell/PowerShell Core/Bat/Shell script. Select bash/pscore script when running on Linux agent or batch/ps/pscore script when running on Windows agent. PowerShell Core script can run on cross-platform agents (Linux, macOS, or Windows)
scriptLocation
Script Location
(Required) Path to script: File path or Inline script
Default value: scriptPath
scriptPath
Script Path
(Required) Fully qualified path of the script(.ps1 or .bat or .cmd when using Windows based agent else .ps1 or .sh when using linux based agent) or a path relative to the default working directory
inlineScript
Inline Script
(Required) You can write your scripts inline here. When using Windows agent, use PowerShell or PowerShell Core or batch scripting whereas use PowerShell Core or shell scripting when using Linux based agents. For batch files use the prefix \"call\" before every azure command. You can also pass predefined and custom variables to this script using arguments.
Example for PowerShell/PowerShellCore/shell: az --version az account show
Example for batch: call az --version call az account show
arguments
Script Arguments
(Optional) Arguments passed to the script
powerShellErrorActionPreference
ErrorActionPreference
(Optional) Prepends the line $ErrorActionPreference = 'VALUE' at the top of your powershell/powershell core script
Default value: stop
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: $servicePrincipalId, $servicePrincipalKey and $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 which 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 subscription>
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
        az --version
        az account show

Open source

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

Q & A

Do I need an agent?

You need at least one agent to run your build or release. Get an agent for Linux, macOS, or Windows.

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.