Home > Mobile >  How to create dependency in azure devops pipeline stages generated in loop?
How to create dependency in azure devops pipeline stages generated in loop?

Time:11-23

Got one piepline where i create stages dynamically (basing o parameters dictionary). The problem is the stages run in random order instead the defined one. I wanted to create dependency, but I would need to use for in range loop so I could create some logic. Maybe there is another way to make it working in proper order?

trigger: none
parameters:
  - name: Stages
    type: object
    default: {
      Development: d-rg,
      Test: t-rg,
      Acceptance: a-rg,
      Production: p-rg
    }

stages:
  - stage: Tests
    jobs:
      - job: Run_tests
        steps:
          - script: |
              python -m pip install --upgrade pip
              python -m pip install -r requirements.txt
            displayName: Install modules
          - script: |
              python -m pytest -v tests/*_tests.py
            displayName: Execute tests
  - ${{ each stage in parameters.Stages }}:
    - stage: ${{stage.Key}}
      dependsOn: 
        - Tests
      condition: succeeded('Tests')
      jobs:
        - job: Run_Production_Code
          steps:
            - task: PythonScript@0
              inputs:
                scriptSource: 'filePath' # Options: filePath, inline
                scriptPath: 'solution/main.py'
                arguments: $(directory)/$(base_name)-${{stage.Value}}.yml $(pat) $(organization) $(project)
              displayName: 'Creating $(base_name)-${{stage.Value}}'

CodePudding user response:

You'll need to do this with explicit dependencies. The best way is to have each stage know which other stage it depends on – i.e. needs to be run after – which you can do with a little restructure of your stages parameter.

parameters:
  - name: Stages
    type: object
    default:
      - name: Development
        flag: d-rg
      - name: Test
        flag: t-rg
        dependsOn: Development
      - name: Acceptance
        flag: a-rg
        dependsOn: Test
      - name: Production
        flag: p-rg
        dependsOn: Acceptance

stages:
- stage: Tests
  # etc
- ${{ each stage in parameters.Stages }}:
    - stage: ${{stage.name}}
      dependsOn:
        - Tests
        - ${{ if ne(stage.dependsOn) }}:
            - ${{ stage.dependsOn }}
      jobs:
        - job: Run_Production_Code
          # etc

This will give you, for the Production stage for example, a dependsOn of both the 'Tests' stage and the 'Acceptance' stage - so it will run after Acceptance.

  • Related