0

I would like to use bash to determine a variable name by parsing a YAML file for the variable name, and then retrieve the value from a variable group on Azure Pipelines. Is this possible?


In a variable group named, I have MY_VAR_1=MY_VALUE_1 and MY_VAR_2=MY_VALUE_2.

In a pipeline using the AzureCLI@2 task, I parse a YAML file and save the values in a variable.

SECRETS=$(yq r $(Pipeline.Workspace)/helm-chart/common-secrets.yml secrets.[*].keys | sed 's/- //')

I then run a for loop on the variable and use the output in a file.

This works when I run outside of Azure Pipelines with the variables defined.

# TEST VARIABLES
MY_VAR_1="MY_VALUE_1"
MY_VAR_2="MY_VALUE_2"
for i in $SECRETS
do
    SECRET_VALUE=$(echo ${!i} | base64)
    echo "  $i: $SECRET_VALUE" >> secrets.yml
done

What syntax can I use in Azure Pipelines to refer to a pipeline variable in a variable group?

I've tried a few things like using macro expression $($i) which failed with a command not found as bash is trying to run it as a command rather than using a pipeline variable.

2
  • Did you manage to solve it? I'm trying to do the same thing Commented Dec 18, 2020 at 16:32
  • No, I never got this working. I ended up with a much less elegant solution to specify which variables needed to be retrieved from the variable group creating an array and looping through the array to build out my secrets.yml file. Commented Dec 18, 2020 at 19:24

2 Answers 2

1

Ok so I think I've found a way to do it, Azure automatically adds all of your variables to the environment of the script:

System and user-defined variables also get injected as environment variables for your platform. When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable name any.variable becomes the variable name $ANY_VARIABLE.

source

for i in $SECRETS
do
    
    SECRET_VALUE=$(printenv $i | base64)
    echo "  $i: $SECRET_VALUE" >> secrets.yml
done

you might need to do $($(printenv $i) | base64), i wasn't sure

Sign up to request clarification or add additional context in comments.

1 Comment

I marked this as the answer as it will likely help others in the future. I didn't test it (as I am done with this project) but looking at the approach, it looks like this will work.
0

I'm not sure if you be able to get this in that way. But for sure you can use az cli. To get all variables in variable group you shoul use this command:

az pipelines variable-group variable list 
--group-id 5 --organization https://dev.azure.com/thecodemanual/ --project "DevOps Manual"

For this I got this response:

{
  "armConnection": {
    "isSecret": null,
    "value": "rg-the-code-manual"
  },
  "myhello": {
    "isSecret": null,
    "value": "somesome"
  }
}

and to narrow results to single value you need to use --query

az pipelines variable-group variable list --group-id 5 --organization https://dev.azure.com/thecodemanual/ --project "DevOps Manual" --query "armConnection.value"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.