azure-devopsazure-yaml-pipelines

Way to add condition on Running a Stage on Azure DevOps Multi stage YAML pipeline


I am trying to implement a multi-stage pipeline in azure devops that is running everytime a push is committed all branches.

But, I would like to skip a certain stage in executing a pipeline stage using condition, given certain branches

Here is the initial script:

jobs:
- job: JOB A
  variables:
    protected_branches: master, main
  condition: containsValue(variables['protected_branches'], variables['Build.SourceBranchName'])

Seems this script is not working

Thank you!


Solution

  • Since variables in Azure DevOps pipelines can only be defined as strings, you should use contains instead of containsValue.

    Here is a code sample where I trigger the pipeline from a branch named test-pipeline:

    jobs:
    - job: Job_A
      variables:
        protected_branches: 'master, main'
      condition: contains(variables['protected_branches'], variables['Build.SourceBranchName'])
      steps:
      - powershell: |
          Write-Host "Will only if source branch is: master or main"
    
    - job: Job_B
      variables:
        protected_branches: 'test-pipeline'
      condition: contains(variables['protected_branches'], variables['Build.SourceBranchName'])
      steps:
      - powershell: |
          Write-Host "Will only if source branch is: test-pipeline"
    

    Output: enter image description here