I am trying to set group dynamically in a pull request pipeline using azure devops. Yaml file looks like as below:
variables:
- ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/dev') }}:
- group: dev-var-group
This does not work and condition is evaluated to 'false'. Just to confirm it I reversed the condition as below
variables:
- ${{ if ne(variables['System.PullRequest.TargetBranch'], 'refs/heads/dev') }}:
- group: dev-var-group
After which group was set correctly and all the required variables are read and pipeline works.
Can anyone please assist here to get this working with the correct condition ?
I'm afraid that this is not possible. If you look here at Template expression syntax
:
You can use template expression syntax to expand both template parameters and variables (${{ variables.var }}). Template variables are processed at compile time, and are replaced before runtime starts. Template expressions are designed for reusing parts of YAML as templates.
Template variables silently coalesce to empty strings when a replacement value isn't found. Template expressions, unlike macro and runtime expressions, can appear as either keys (left side) or values (right side). The following is valid: ${{ variables.key }} : ${{ variables.value }}.
and to have this working you need a template variable and System.PullRequest.TargetBranch
is rather runtime variable, thus it is replaced to empty string. If you check predefined variables here you will notice last column Available in templates?
which says no
for System.PullRequest.TargetBranch