2

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 ?

sblive
  • 105
  • 9

2 Answers2

4

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?

enter image description here

which says no for System.PullRequest.TargetBranch

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
0

Perhaps would be enough to evaluate Build.Reason as its available in templates?

Also you could check pull request source branch during the runtime and assign variables based on this. I.e.:

Write-Host “##vso[task.setvariable variable=testvar;]testvalue”).
Jakub Pawlinski
  • 442
  • 3
  • 16