Search code examples
azure-devopscontinuous-integrationazure-pipelinesazure-pipelines-release-pipelineazure-pipelines-yaml

Azure DevOps YAML: Conditional template call


I want to check if Fetch_Commits succeeded in order to run jobs in microservice-calls.yaml file. I don't want to use Stages since there are no semantic for separating among jobs.

Are there any similar syntax to

- ${{ if succeeded('Fetch_Commits') }}:
     - template: templates/microservices-calls.yaml

The YAML file:

trigger:
- trigger-branch

jobs:
    - job: Fetch_Commits
      displayName: Checkout
      pool:
       name: MyPool
      steps:
      - checkout: self
        fetchDepth: 2
    - ${{ if succeeded('Fetch_Commits') }}:
      - template: templates/microservices-calls.yaml

Solution

  • In Azure Pipelines, dependsOn and condition cannot be used directly in template references. So, these properties need to be set on every job inside the template. If your templates/microservices-calls.yaml file contains multiple jobs, you need to set dependsOn and condition separately in each job. Assume your microservices-calls.yaml is as follows:

    #  templates/microservices-calls.yaml
    jobs:
    - job: Microservice_Job_1
      dependsOn: Fetch_Commits
      condition: succeeded('Fetch_Commits')
      steps:
      - script: echo Running Microservice Job 1
    
    - job: Microservice_Job_2
      dependsOn: Fetch_Commits
      condition: succeeded('Fetch_Commits')
      steps:
      - script: echo Running Microservice Job 2
    
    

    Then you can use the template as the following yaml. Microservice_Job will only run when the Fetch_Commits job succeeds.

    trigger:
    - trigger-branch
    
    jobs:
      - job: Fetch_Commits
        displayName: Checkout
        pool:
          name: MyPool
        steps:
        - checkout: self
          fetchDepth: 2
    
      - template: templates/microservices-calls.yaml