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

10,984

Solution 1

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://docs.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/

Solution 2

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:

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.

Share:
10,984
MoonHorse
Author by

MoonHorse

MS Certified Azure Architect Expert Google Cloud Certified Architect

Updated on June 11, 2022

Comments

  • MoonHorse
    MoonHorse almost 2 years

    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?