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.
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.