A couple of months ago I had an exchange with Donovan Brown on Twitter about using AzureCLI & PowerShell in Azure DevOps pipelines on Linux hosted agents. As is often the way I didn’t have the time to take the advice I received and experiment straight away. As we ramp into a new project I realized that I had project where trying this approach made sense.
So, I was eager to try this out and dived in, adding an Azure PowerShell task to my pipeline to try blending the lovely Json responses from Azure CLI with the power of proper objects in my scripts. So, attempt #1:


So, a quick change to toggle the task to use Task version 4 (aka Preview)

And, much after anticipation….. ERROR: Please run 'az login' to setup account.
😦 Much disappointment. But Donovan said he does it, so how does this work?
Taking a look at the logs for an Azure CLI task shows me some thing interesting.
[command]/usr/bin/az cloud set -n AzureCloud [command]/usr/bin/az login --service-principal -u *** -p *** --tenant *** [ { "cloudName": "AzureCloud", "id": "0ff1ce000-1d10-t500-86f4-28b59b7dbf13", "isDefault": true, "name": "Not really the name :P", "state": "Enabled", "tenantId": "***", "user": { "name": "***", "type": "servicePrincipal" } } ]
And then inspecting the task options I can see a way of accessing those variable in an Azure CLI task.

Couple this with some cunning use of the task.setvariable
command and I can bubble these parameters out for use in the rest of my pipeline:
# Echo the authentication detail out to variables for use in a PowerShell step echo "##vso[task.setVariable variable=servicePrincipalId]$servicePrincipalId" echo "##vso[task.setVariable variable=servicePrincipalKey]$servicePrincipalKey" echo "##vso[task.setVariable variable=tenantId]$tenantId"
Which then get passed into PowerShell script as arguments, $(servicePrincipalId) $(servicePrincipalKey) $(tenantId)
and used like so:
az login --service-principal ` -u $servicePrincipalId ` -p $servicePrincipalKey ` --tenant $tenantId
Hey presto, Azure CLI calls from PowerShell on a Linux hosted build agent 😀
Hopefully someone out there will find this useful.
Pingback: Vsts Az Login - LoginCrunch