azure-devopsazure-pipelinesazure-pipelines-yaml

Azure Pipelines - Run a stage multiple times


I am working with azure pipelines and I have some stages with jobs inside it.

I am trying to figure it out how can I run a stage 3 times before moving to next stage.

stages:
- stage: SomeTests
  jobs:
  - job:
    ...

- stage: Installation 
  dependsOn: SomeTests
  jobs:
  - job:
    ...

I'd like to run stage SomeTests 3 times and then run stage Installation.

How can I do that?


Solution

  • You can declare a parameter of type object containing an array (where each element contains information about the stage to run) and then loop through the parameter and generate a stage for each element.

    Generating stages that run sequentially

    By default stages run sequentially, but if you need to define custom dependencies you can do the following:

    parameters:
      - name: testStages
        type: object
        default:
          - name: someTests1
            displayName: 'Some Tests 1'
            dependsOn: [] # run in parallel (first stage)
          - name: someTests2
            displayName: 'Some Tests 2'
            dependsOn:
              - someTests1
          - name: someTests3
            displayName: 'Some Tests 3'
            dependsOn:
              - someTests1
              - someTests2
    
    stages:
      - ${{ each stage in parameters.testStages }}:
        - stage: ${{ stage.name }}
          displayName: ${{ stage.displayName }}
          dependsOn: ${{ stage.dependsOn }}
          jobs:
            - job: job_${{ stage.name }}
              steps:
                - checkout: none
                - script: echo ${{ stage.name }}
      
      - stage: Installation
        displayName: Installation
        dependsOn: ${{ parameters.testStages.*.name }}
        jobs:
          - job: job_Installation
            steps:
              - checkout: none
              - script: echo Installation
    

    Queuing a new pipeline - stages:

    Choose pipeline stages

    Stages sequence:

    Stages sequence

    Generating stages that run in parallel

    As an alternative, you can run the test stages in parallel by setting dependsOn: []:

    parameters:
      - name: testStages
        type: object
        default:
          - name: someTests1
            displayName: 'Some Tests 1'
          - name: someTests2
            displayName: 'Some Tests 2'
          - name: someTests3
            displayName: 'Some Tests 3'
    
    stages:
      - ${{ each stage in parameters.testStages }}:
        - stage: ${{ stage.name }}
          displayName: ${{ stage.displayName }}
          dependsOn: [] # <----------------------- run stages in parallel (no dependencies)
          jobs:
            - job: job_${{ stage.name }}
              steps:
                - checkout: none
                - script: echo ${{ stage.name }}
      
      - stage: Installation
        displayName: Installation
        dependsOn: ${{ parameters.testStages.*.name }}
        jobs:
          - job: job_Installation
            steps:
              - checkout: none
              - script: echo Installation
    

    Queuing a new pipeline - stages:

    Choose pipeline stages

    Stages sequence:

    Stages sequence

    Recommended reading