Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Azure Devops, how can i use pipeline variables in a "Azure CLI task v.2" running shell script?

I am trying to execute a task using Azure CLI and bash in Azure Devops. I am using Azure CLI task v.2 and choosing shell as script type as below. enter image description here I want to use pipeline variables in bash script. I run the command below inside the script:

#!/bin/bash
az role assignment create --role "Lab Admin" --assignee $(ownerEmail) -g $(rgName)

and i got the error below:

 line 2: ownerEmail: command not found
 line 2: rgName: command not found

I don't understand. Normally, i should be able to use azure cli in a bash script. Does anybody have an idea?

like image 534
MoonHorse Avatar asked Dec 23 '22 17:12

MoonHorse


2 Answers

There are a couple of ways you could read the pipeline variables in your bash script:

  1. (Suggested) Read them straight as environment variables in the script. Azure DevOps pipeline variables are added as environment variables that can be accessed by your bash script. So just by defining/setting the pipeline variables you can access them from the bash script. i.e in your script reference them as $OWNERNAME and $RGNAME.
    See > https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables#environment-variables

  2. Reference the direct argument in the bash script. Bash arguments are numbered and can be referenced as such. e.g for your aruments $1 is the string "-labOwner" $2 is the value contained within the ownerEmail pipeline variable.
    See > https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/

like image 145
Dom Avatar answered Jan 13 '23 16:01

Dom


Variables are passed in as environment variables. The variables are all uppercased and case-sensitive on linux.

Thus, $OWNERNAME and $RGNAME are likely the values you're after.

See the section on variable usage in scripts:

  • https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#environment-variables

There are some tasks that don't populate the environment at all, these have an environment section where you can manually pass in the environment variables.

like image 25
jessehouwing Avatar answered Jan 13 '23 16:01

jessehouwing