Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 342
pooya13 Avatar asked Nov 22 '25 13:11

pooya13


2 Answers

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.

like image 59
pooya13 Avatar answered Nov 25 '25 09:11

pooya13


How can I ensure CutTraffic_5 runs after CutTraffic_1 and CutTraffic_100 runs after CutTraffic_5?

In each expression, we are not able to automatically assign the value of the current element to the name field and then assign the next element to the DependsOn field. In one loop, the value of the weight parameter remains unchanged.

To meet your requirement, we need to use object type parameters to explicitly define the job name and the name of the dependsOn job.

Here is an example:

Main YAML:

parameters:
- name: cutoverWeights
  type: object
  default: 
    - name: CutTraffic_1
      dependsOn: 
    - name: CutTraffic_5
      dependsOn: CutTraffic_1
    - name: CutTraffic_100
      dependsOn: CutTraffic_5


pool:
  vmImage: ubuntu-latest


jobs:
  - ${{ each weight in parameters.cutoverWeights }}:
    - template: internal/Cutover.job.yml
      parameters:
        name: ${{ weight.name }}
        dependsOn: ${{ weight.dependsOn }}

Template YAML:

parameters:
- name: name
  type: string
- name: dependsOn
  type: string


jobs:
- job: ${{ parameters.name }}
  dependsOn: ${{ parameters.dependsOn }}
  steps:
    - script: echo "test"

Result:

enter image description here

like image 20
Kevin Lu-MSFT Avatar answered Nov 25 '25 09:11

Kevin Lu-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!