0

I have an issue with a variable evaluating to Null on a Condition.

This is the pipeline:

I have a variable template that is pulled in by the main yml file:

variables:
  - ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/') }}:
    - name: is_sandbox
      value: true

  - ${{ if eq(variables['system.pullRequest.targetBranch'], 'refs/heads/master') }}:
    - name: is_sandbox
      value: true
    - name: bob
      value: a

Note that is_sandbox is not set anywhere else.

I then have a condition on job:

condition: or (eq(variables.is_sandbox, true), eq(variables.bob, 'a'))

But this fails to evaluate. In the log is see:

system.pullRequest.targetBranch : refs/heads/master

and:

Expanded: or(eq(Null, True), eq(Null, 'a'))
Result: False

So it appears that the variables template has not set correctly. Why would that be?

simon_dmorias
  • 2,343
  • 3
  • 19
  • 33
  • Can you provide the scoping of your variable? `is_sandbox` is that being set at pipeline, stage, job? – DreadedFrost Jun 11 '21 at 13:09
  • @DreadedFrost - it's at the Stage. I believe that two pre-defined variables are evaluating to Null when it runs. I would have thought these should be available anywhere? – simon_dmorias Jun 11 '21 at 13:59

1 Answers1

0

Stage variables will only be available to activities within the stage. This is beneficial when reusing variables with different values across different stages. Think potentially stage is tied to an environment and the environment values are different so thus can pass in different variables at the stage level.

I believe you are looking to define these at the root level so variable values will be available to anything in the pipelines unless it is overwritten by a lower level scope.

DreadedFrost
  • 2,602
  • 1
  • 11
  • 29
  • The job which the condition is on is in the same Stage. Surely that should inherit? – simon_dmorias Jun 11 '21 at 14:52
  • It should. It sounds like the path to the template file containing the variables might be incorrect. – DreadedFrost Jun 11 '21 at 15:04
  • The path must be correct because if I take out the if block the variables get set. I'm completely at a loss. A days of my life has been spent on this. I can't understand how debugging this can be so hard! – simon_dmorias Jun 11 '21 at 16:06
  • Can you share more of your YAML? – DreadedFrost Jun 11 '21 at 16:08
  • Argh - according to https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#system-variables-devops-services certain variables are not available in templates (last column on the tables). Including this one. Thanks for your help. – simon_dmorias Jun 11 '21 at 16:26
  • Learn something new everyday! One option might consider is to default the variables values at the root and then load the variable file at the stage thus overwriting it.....or store two variable files one for each condition and use something else to determine which one to load. – DreadedFrost Jun 11 '21 at 17:18