In Azure DevOps pipelines, how do I login to AZ CLI using the service principle details returned from the addSpnToEnvironment parameter?

Viewed 2082

I essentially have to run a Python script that has AZ CLI commands baked into the script and get spawned as processes from the subprocess module. In order for these commands to run, I need to be logged in to AZ CLI.

I have a AZ CLI task in my pipeline of which setting the addSpnToEnvironment = true provides me the $env:servicePrincipalId, $env:servicePrincipalKey and $env:tenantId. My question is, how can I use these details to now sign in to AZ CLI so that the context is maintained in the task and the AZ CLI commands in my Python script recognize this and can execute?

1 Answers

You can log into az cli using the service principal. See below:

az login --service-principal --username $env:servicePrincipalId --password $env:servicePrincipalKey --tenant $env:tenantId

See document here for more information.

Related