I defined a YAML build-pipeline in azure:
variables:
test: \'${{ variables.Environment }}\'
pool:
vmImage: \'ubuntu-latest\'
steps:
- script: |
echo $(
I expect it to echo abc but instead abc is replaced with nothing - variables.Environment seems to be undefined.
According to this document:
Runtime happens after template expansion. Template variables are processed at compile time, and are replaced before runtime starts. Template variables silently coalesce to empty strings when a replacement value isn't found.
So in your case echo $(test)
print out nothing but empty string. Cause the queue variables are used for runtime. For this, you can consider using macro or runtime expression which is for runtime. Both test: $(Environment)
and test: $[variables.Environment]
work well on my side.
Later on I want to load a different Variable Group depending on the Environment variable, which is why I do not echo $(Environment) directly in the script. It's just a simplified example.
As I know, linking different variable groups depending on the dynamic Environment
variable is not supported yet, here's one discussion about that topic. And this is one good workaround in that scenario.
Nowadays Azure Devops Service is rolling out the new feature runtime parameters, I think it can meet most of your requirements. It could be a better choice for you, use runtime parameters instead of not supported dynamic Environment
variable.
My simple test about this option:
1.Content in yaml:
parameters:
- name: group
displayName: Group Name
type: string
default: TestGroup
values:
- TestGroup
- Group2
- Group3
- Group4
variables:
- group: ${{ parameters.group }}
steps:
- script: |
echo $(Name)
displayName: 'Show group name'
2.My variable group TestGroup:
3.Click run pipeline:
4.The pipeline runs well and the displays the variable defined in variable group:
Hope it helps :)