Reputation: 2899
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.
Upvotes: 0
Views: 1198
Reputation: 904
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.
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
Upvotes: 1
Reputation: 40939
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"
Upvotes: 0