Search code examples
azureazure-devopsazure-pipelinespipeline

Azure pipeline - Stage condition dependson


I have three environments: dev, hml and qa.

In my pipeline depending on the branch the stage has a condition to check whether it will run or not:

- stage: Project_Deploy_DEV
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/dev')
  dependsOn: Project_Build

- stage: Project_Deploy_HML
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/hml')
  dependsOn: Project_Build

I'm doing the qa stage, and I'd like to put a condition, depending on the branch, the dependson parameter will change:

- stage: Project_QA
   condition:
     ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
       dependsOn: 'Project_Deploy_DEV'
     ${{ if eq(variables['Build.SourceBranchName'], 'hml') }}:
       dependsOn: 'Project_Deploy_HML'

However, the condition above is not working, would anyone know the best way to perform this condition?

Thanks


Solution

  • From your YAML sample, it seems that there is a format issue. When you use if expression, there is no need to add condition field in YAML.

    You can try the following sample and check if it can work.

    stages:
      - stage: A
        jobs:
          - job: test
            steps:
              - xxx
    
      - stage: B
        jobs:
          - job: test
            steps:
              - xxx
      - stage: C
        ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
          dependsOn: A
        ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
          dependsOn: B  
        jobs:
          - job: test
            steps:
              - xxx