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

Run jobs in `each` expression run in sequence in Azure Devops


In Azure DevOps one can run jobs in sequence as described here. However, in my case I am creating a list of jobs based on some input parameter and I want to know how I can run them in sequence:

jobs:
  - ${{ each weight in split(parameters.cutoverWeights, ',') }}:
    - template: internal/Cutover.job.yml
      parameters:
        name: CutTraffic_${{ weight }}
        dependsOn: ??

For example, if cutoverWeights is 1,5,100, this will create 3 jobs: CutTraffic_1, CutTraffic_5 and CutTraffic_100. How can I ensure CutTraffic_5 runs after CutTraffic_1 and CutTraffic_100 runs after CutTraffic_5?


Solution

  • The only solution I have found is as follows:

    jobs:
      - ${{ each weight in split(parameters.cutoverWeights, ',') }}:
        - template: internal/Cutover.job.yml
          parameters:
            name: CutTraffic_${{ weight }}
            dependsOn:
              - ${{ each otherWeight in split(parameters.cutoverWeights, ',') }}:
                - ${{ if lt(otherWeight, weight) }}:
                  - CutTraffic_${{ otherWeight }}
    

    Though this only works for ordinal ordering if there is some simple relation in the input list that is supported by the limited YAML pipeline expressions available. For example, 1,2,3,4 or region1,region2,region3.

    If you want integer ordering instead of ordinal string ordering (for example in case of input 1,5,10,24,100) you can use condition:

    - ${{ if or(lt(length(otherWeight),length(weight)), and(eq(length(otherWeight),length(weight)), lt(otherWeight,weight))) }}:
    

    This seems like a hacky solution. So if a better solution is provided in the future I will accept that one.