4

I would like to have something like this:

  - ${{ if or(eq(parameters.RunTestsOnPRBuildOnly, false), eq(variables.Build.Reason, 'PullRequest')) }}:
      - template: ps-module-run-tests.yml

This does not work, as variables.Build.Reason is empty. Is it possible at all?

Note that I know how to modify the ps-module-run-tests.yml template to express my desire as a runtime condition. In other words I know how to arrive at this:

enter image description here

What I am curious is whether Build.Reason can be used in a compile time condition, so that these steps are not even rendered. On the surface, there is no inherent problem with that, because the value is known right at the start, but it depends on when the template is compiled. If too early, then it is impossible, but I am unaware of such details. Maybe I cannot do it, because I am missing something.

So, is it possible?

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

5

Instead of

variables.Build.Reason

try to use

variables['Build.Reason']

According to https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services Build.Reason is available in template expressions at compile time

I have looked at https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditional-insertion there is one example also there is note that if you want to use property dereference syntax it can contain only enter image description here and so dot sign is not acceptable (dot between Build and Reason)

Kontekst
  • 946
  • 8
  • 17
  • I will test it, but could you point where in the documentation it is said? – mark Dec 14 '21 at 05:34
  • It does work. Please, include the quote from the document. – mark Dec 14 '21 at 05:46
  • I have looked at https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditional-insertion there is one example also there is note that if you want to use dereference syntax it can contain only [a-Z 0-9 and _ ] and dot sign is not acceptable (dot between Build and Reason) – Kontekst Dec 14 '21 at 12:18