Search code examples
azure-devopsazure-pipelinesdevopsazure-pipelines-yamlazure-devops-extensions

Azure pipelines shows skipped stages


I have below azure pipeline yaml file. I have tried the below setup which runs the test stage , but its also shows me the build & deploy stages, which I don't want.

variables:
  vmImageName: "ubuntu-latest"

trigger:
  - production

stages:
  - stage: Test
    displayName: Test stage
    condition: eq(variables['Build.Reason'], 'IndividualCI')
    jobs:
      - job: Test
        displayName: Test job
        pool:
          vmImage: $(vmImageName)
        steps:
          - script: |
              echo "Testing ..."
            displayName: 'Test job script'

  - stage: Build
    displayName: Build stage
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/production'), ne(variables['Build.Reason'], 'PullRequest'))
    jobs:
.............SAME AS ABOVE ..........
          - script: |
              echo "Building ..."
            displayName: 'Build job script'

  - stage: Deploy
    displayName: Deploy to Dev
    dependsOn: Build
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/production'), ne(variables['Build.Reason'], 'PullRequest'))
.............SAME AS ABOVE ..........
        steps:
          - script: |
              echo "Deploying ..."
            displayName: 'Deploy job script'

In gitlab it's very easy. Gitlab pipelines shows only the stages which is mentioned like only on main or merge_request. I am expecting the same here in azure pipelines.

I am trying in every possible way, But still I could see all the stages in my pipelines.

enter image description here

I want:

Test -> to be triggered & visible only on commits & PR

Build & Deploy -> Build & Deploy stages only visible on when merged to default branch

What changes should I make in azure pipeline yaml for the same ?


Solution

  • condition parameters show that the stage (or job, or step) exist, but weren't run.

    If you want to not have a stage/job/step appear at all, you'll need to use compile-time logic to not even include that structure in the final YAML document in the first place.

    i.e.

    - ${{ if and(eq(variables['Build.SourceBranch'], 'refs/heads/production'), ne(variables['Build.Reason'], 'PullRequest')) }}:
      - stage: Build
        ...etc...
      - stage: Deploy
        ...etc...
    

    Of course, if you do that, you can remove the condition as well.