Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

Dynamically select a template based on an environment variable in Azure YAML Pipeline


Following script works if BuildReason is defined as user defined variable within the yml file. Would it be possible to get its value if it is set as environment variable directly on the UI?

trigger: none

name: $(Date:yyyy).$(Date:MMdd).$(Rev:r)

variables:

- name: templateName
  ${{ if eq( variables['BuildReason'], 'PR' ) }}:
    value: 'pipelines/prvalidation-template.yml'
  ${{ else }}:
    value: 'pipelines/build-template.yml'

extends:

  template: ${{ variables.templateName }}

  parameters:
      Parameter1: MyProject

Thank you!


Solution

  • There is predefined variable Build.Reason which you can use in expressions.

    enter image description here