Can Azure DevOps build variable Build.Reason be used in YAML template compile time conditions?

Viewed 740

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?

1 Answers

Instead of

variables.Build.Reason

try to use

variables['Build.Reason']

According to https://docs.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://docs.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)

Related