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

Not expected stage skipping


I have doubt with stage condition. Using https://stackoverflow.com/a/78262966/28447778 post as refence I want to achieve such scenario, when someone checked "Run on production mode" in pipeline run it should run from "Build" stage to the "PRD" stage directly, otherwise when someone doesn't check "Run on production mode" it should goes through all stages.

In my current code the behavior is that when I check "Run on production mode" it works correctly: enter image description here

But when I leave it unchecked it directly runs to DEV, however it should run Build -> DEV -> PRD. enter image description here

Where is my mistake?

parameters:
  - name: runOnProductionMode
    displayName: 'Run on production mode'
    type: boolean
    default: false

stages:
  - stage: Build
    displayName: 'Build'
    jobs:
      - template: templates/manual.yml@Pipelines
  - ${{ if not(parameters.runOnProductionMode) }}:
    - stage: DEV
      dependsOn: GateApproval
      condition: succeeded()
      displayName: 'DEV'
      pool:
        name: ${{ variables.dev }}
      jobs:                
        - deployment: DEV
      # Rest of the code

  - ${{ if parameters.runOnProductionMode }}:
    - stage: PRD
      displayName: 'PRD'
      pool:
        name: ${{ variables.prd }}
      jobs: 
        - deployment: PRD
      # Rest of the code

Solution

  • when someone checked Run on production mode in pipeline run it should run from Build stage to the PRD stage directly, otherwise when someone doesn't check Run on production mode it should goes through all stages.

    If PROD stage should always run then you should remove the ${{ if ... }} condition on that stage.

    Instead of:

    - ${{ if parameters.runOnProductionMode }}:
      - stage: PRD
        displayName: 'PRD'
        pool:
          name: ${{ variables.prd }}
        jobs: 
          - deployment: PRD
        # Rest of the code
    

    Do:

    - stage: PRD
      displayName: 'PRD'
      pool:
        name: ${{ variables.prd }}
      jobs: 
        - deployment: PRD
      # Rest of the code