I have defined a variable called tst-my–variable, and another one uat-my–variable in my azure pipeline like below.
When I’m running the azure-pipelines.yml, I’m checking the environment in which the pipeline is executing and defining a variable "environmentName".
After that I want to get one of those "tst-my–variable" "uat-my–variable" by concatenating the variable "environmentName" value with the text "-my–variable".
but nothing is working.
steps:
- script: |
echo $MY_ENV
env:
MY_ENV: $($(environmentName)-my--variable)
I already checked this article but didn’t help.
2
Answers
This is not a direct answer to your question, but allow me to suggest a different approach to solve your problem.
1. Create variables template for each environment
As per Variables reuse, create a separate template to store each environment’s variables:
TEST variables:
UAT variables:
2. Reference the variables template based on the environment
The below example pipeline uses a pipeline parameter for the environment, as opposed to a pipeline variable.
The parameter is then used to dynamically reference the right variables template:
Assuming the value for your variable
$(environmentName)
is defined during pipeline runtime rather than at compile time, which may prevent the pipeline from including the variable template in template expression syntax, it is suggested you directly use output variables during runtime. Here is a sample for your refernce.See more samples to Set an output variable for use in future jobs and Set an output variable for use in future stages.