Search code examples
azure-devopsyamlazure-pipelinescicdazure-pipelines-yaml

Azure Pipeline remove job from ui if job is conditionally skipped/removed


I have a template that loop out three stages.

- ${{ each environment in parameters.environments }}:

If we are in test environments I want to run a test-suite. But not for QA and Prod.

I kind of solved it, but the test steps are showing in UI, I want them to not appear at all if I run the QA and Prod stages. And not appear as "skipped".

jobs:
  - job: Test
    displayName: Test
    steps:
      - template: test.yaml

One frustrating thing is that doing conditional things in a loop is not allowed.


Solution

  • You can use an if statement to prevent a specific stage, job or step from being generated in QA or Prod environments:

    - ${{ if eq(environment, 'test') }}:
      # ...
    

    Example pipeline:

    parameters:
      - name: environments
        type: object
        default:
          - test
          - qa
          - prod
    
    pool:
      vmImage: ubuntu-latest
    
    trigger: none
    
    stages:
      - ${{ each environment in parameters.environments }}:
        - stage: ${{ environment }}
          displayName: ${{ environment }}
          jobs:
            - ${{ if eq(environment, 'test') }}: # <-------------- check environment here
              - job: Testing
                displayName: Run tests
                steps:
                  - checkout: none
                  - script: echo "Running tests in ${{ environment }}"
                    displayName: Run tests in ${{ environment }}
    
            - job: deploy_${{ environment }}
              displayName: Deploy to ${{ environment }}
              steps:
                - checkout: none
                - script: echo "Deploying to ${{ environment }}"
                  displayName: Deploy to ${{ environment }}
    

    Running the pipeline:

    Pipeline stages and jobs