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

Trigger pipeline on different branches conditionally, according to different pipeline parameters, from a single YAML file


I have a single YAML config file for an Azure DevOps pipeline which has the following trigger:

trigger:
  branches:
    include:
     - develop
     - release/*

And I would like to be able to conditionally run it on develop, according to what pipeline parameters are selected when the pipeline is run, something like:

trigger:
  branches:
   (if parameter.Foo = true trigger develop)
    include:
     - develop
     (if parameter.Bar = true trigger release/*)
     - release/*

I looked at official Microsoft Azure DevOps documentation but could find anything similar, only that maybe something related to what I want could be achieved with Branch Policies in Azure DevOps.

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/ci-build-git?view=azure-devops&tabs=yaml


Solution

  • The triggers do not support IF statements. However, you may try to use conditions on the job level:

    Example:

    trigger:
      branches:
        include:
         - develop
         - release/*
    jobs:
    - job: MyJob
      displayName: My First Job
      condition: and(succeeded(), or( and( eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq('${{ parameters.Foo }}', true)), and( startsWith(variables['Build.SourceBranch'], 'refs/heads/release'), eq('${{ parameters.Bar }}', true))))
      workspace:
        clean: outputs
      steps:
      - script: echo My first job