yaml - Azure Pipeline remove job from ui if job is conditionally skippedremoved - Stack Overflow

admin2025-04-17  3

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.

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.

Share edited Jan 31 at 12:17 Rui Jarimba 18.2k11 gold badges64 silver badges98 bronze badges Recognized by CI/CD Collective asked Jan 31 at 12:16 user29445754user29445754 131 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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:

Unfortunately this is part of the pipeline process. If you have the job in there as part of a condition it will fully expand no matter what in the pipeline and appear in the UI if it is skipped.

As outlined the way around this is to put an if statement around the job from being included in the pipeline expansion. This means, unfortunately, if you are running jobs leveraging a runtime variable/parameter that you cannot work around this as the value won't be known until execution.

转载请注明原文地址:http://anycun.com/QandA/1744862508a88670.html