simon_dmorias
simon_dmorias

Reputation: 2483

Yaml If blocks causes missing mapping

I'm trying to add an optional set of parameters when the build running is not on the master branch. The idea is that azure-pipelines-template-variables-private.yml overrides just some of the parameters contained in the group.

This is my template file:

stages:
  - stage: "Tests"
    variables:
      - group: MyLibrary
    ${{ if ne(variables['Build.SourceBranchName'], 'master') }}:
      - template: azure-pipelines-template-variables-private.yml

This returns the error "Expected a mapping"

Without the if block the behaviour is as expected - the parameters are overwritten. I'm struggling to understand how if blocks work. The docs show how to use variables with Group and variables with templates - but not both together. Though it seems to work together, so I would expect the if statement to work.

Edit: For information I'm including the variable template contents:

variables:
  - name: MyVar1
    value: 'TEST-$(Build.BuildId)'
  - name: MyVar2
    value: '/Builds/$(BUILD.BUILDID)'

Upvotes: 4

Views: 17033

Answers (1)

Hugh Lin
Hugh Lin

Reputation: 19461

I tested the script below and it works well. Hope this helps you :

- stage: build
  jobs:
  - job: run_build
        
    pool:
      vmImage: 'Ubuntu 16.04' 
    variables:
    - group: xxx
    - ${{ if ne(variables['Build.SourceBranchName'], 'refs/heads/master') }}:
      - template: var.yml
    steps:      
      - script: echo ${{ variables.test }}

Upvotes: 4

Related Questions