0

I have multiple layers of template for my DevOps pipeline. I am trying to do the following:

### template A ###
parameters:
- name: check
  type: boolean
  default: false

jobs:
- template: template B                         ### template B has param 'count'
- ${{ if ne(parameters.check, true) }}:   
  count: 1
- ${{ if eq(parameters.check, true) }}:
  count: 5


### template B ###
parameters:
- name: count
  type: number
  default: 0

jobs:
- some jobs

Now when I try to pass the parameter based on the condition, it fails with 'Expected a mapping' error. Is there a different way to do this? Thanks!

xertzer
  • 69
  • 6
  • Hi, I was able to fix the issue on my own. I just use the if block only if I need to overwrite the default values of the params in the template. That way I don't have to have if/else, but just an if. I haven't verified if your solution works yet. – xertzer Apr 09 '21 at 18:00

1 Answers1

0

The cause for your error is that you cannot assign a value to a variable directly in jobs. In addition, the two templates perform two different jobs, so variables or parameters of one template cannot simply be called to each other, but need to be passed.

My advice is to use depends to meet your needs. Here is an example.

templateA.yml:

parameters:
- name: check
  type: boolean
  default: false

jobs:
- job:jobA
  variables:
    checkVar: ${{ parameters.check }}
  steps:
  - pwsh: |
      if ($($env:CHECKVAR)) {
        echo "##vso[task.setvariable variable=Num;isOutput=true]1"
      }
      else {
        echo "##vso[task.setvariable variable=Num;isOutput=true]5"
      }
    name: outputVar
- template: templateB.yml
  parameters:
    count: $[dependencies.deployA.outputs['outputVar.Num']]

b.yml:

parameters:
- name: count
  default: 0

jobs:
- job
  dependsOn: jobA
  steps:
  ....

In addition, here is my test pipeline.yml and it works well.

trigger: none

pool:
  vmImage: ubuntu-latest

stages:
  - stage: A
    jobs:
    - template: templateA.yml
      parameters:
        check: true
Jane Ma-MSFT
  • 4,461
  • 1
  • 6
  • 12