2
votes

I have multiple pipelines that are being built from a multistage yaml (extends: template:) and i want to set conditions on which jobs and steps should run when i pass a variable. So in this case i want to set the variable 'Migrations=false' and skip some jobs (Migration job) and some steps (npm run integration-tests) on my integration Job

I have tried to use (to skip migration jobs on migration.yaml)

stages:
- stage: dev
  displayName: dev
  jobs:
  - ${{if ne(variables.migrations, 'false')}}:
    - template: /Dotnet/Release/migration.yaml

and also (to skip integration script on integration.yaml)

 jobs:
 - template: /Dotnet/Release/integration.yaml
   parameters:
     migrations: ${{ variables.Migrations }}

###integration.yaml###
- name: 'migrations'
  default: 'true'
  type: string
jobs:
- job: Integration
  steps:  
   - script: "echo step1"
   - ${{if ne(parameters.migrations, 'false')}}:
     - script: npm run integration-tests
1

1 Answers

0
votes

Check the following example:

- stage: A
  jobs:
  - job: A
    steps:
    - bash: echo "A"


- stage: B
  variables:
    someVar: true
  jobs:
  - job: B
    steps:
    - ${{ if ne(variables['someVar'], 'false') }}:
      - template: resource-template.yml
    - script: echo hi
  - template: test.yml
    parameters:
      migrations: ${{ variables.someVar }}


#test.yml

parameters:
- name: 'migrations'
  default: 'false'
  type: string

jobs:
- job: Integration
  steps:  
  - script: "echo step1"
  - ${{ if ne(parameters.migrations, 'true') }}:
      - script: echo hello

Note:

You may get red wavy line (like the screenshot below) when you create the yaml file. But the schema is correct, you can run it without issue.

enter image description here