I’m trying to use variables inside variables in azure pipelines.
Below is an example of the bash script:
#!/bin/bash
customer=google
environment=preprod
android_google_preprod_account_activation_url=preprod.google.com
echo "Customer is $customer"
echo "Environment is $environment"
var1=android_${customer}_${environment}_account_activation_url
echo "variable is $var1"
echo "original value is ${!var1}"
I get the expected output for the above bash script when I run it on my Ubuntu server, with NO errors:
Customer is google
Environment is preprod
variable is android_google_preprod_account_activation_url
original value is preprod.google.com
The yml code for azure pipelines is:
parameters:
- name: customer
displayName: 'select customer'
type: string
values:
- google
- name: environment
displayName: 'select environment'
type: string
values:
- preprod
variables:
- group: android-${{ parameters.customer }}-${{ parameters.environment }}
- name: var1
value: android-${{ parameters.customer }}-${{ parameters.environment }}-account-activation-url
script: |
echo "Customer is $(customer)"
echo "Environment is $(environment)"
echo "variable is $(var1)"
echo "original value is $(!var1)"
displayName: 'echo variables'
The value of android-google-preprod-account-activation-url is being taken from variable groups inside library.
It gives me an error for the 4th line:
invalid indirect expansion
The first 3 lines output is as expected.
Expected output is:
Customer is google
Environment is preprod
variable is android_google_preprod_account_activation_url
original value is preprod.google.com
Is there a different syntax that needs to be followed in azure pipelines?
2
Answers
I`m not a bash expert ))) however… you’re trying to use the parameters expansion What is indirect expansion? What does ${!var*} mean?
But it refers to the bash variables…. when you define variables in the devops pipeline, you have to use them as environment variables or through the macro.
or something like that:
The macro syntax "
$(varName)
" is a proprietary syntax in Azure Pipelines to interpolate variable values. It is processed during runtime and different with the syntax "${varName}
" in Bash scripts.For your case, you can try to use the compile time syntax "
${{ variables.varName }}
" to get the value in the pipeline.With above change, after you triggered the pipeline:
${{ variables.var1 }}
" will be replaced with the actual value "android_google_preprod_account_activation_url
". So, the expression "$(${{ variables.var1 }})
" will be changed to "$(android_google_preprod_account_activation_url)
".preprod.google.com
".Below is an example I have tested on my side.
YAML
Result
For more details, you can reference the related document "Understand variable syntax".