Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a variable group conditionally using YAML on Azure DevOps

I'm trying to create a variable (group) on my YAML template, only if a certain parameter has a value. I'm using the following snippet:

variables:
  - name: variableGroupName
    value: app-${{parameters.environmentInitials}}
  - template: /pipelines/variables/${{parameters.environmentInitials}}.yml
  - group: ${{variables.variableGroupName}}
  
    ${{if parameters.parentEnvironmentInitials}}:
      - group: app-${{parameters.parentEnvironmentInitials}}

Yet, when I try to trigger the pipeline I get the error:

Encountered error(s) while parsing pipeline YAML: Expected a mapping - Unexpected state while attempting to read the mapping end. State:

SequenceState:
  IsStart: False
  Index: 3
  IsEnd: True

MappingState:
  IsStart: False
  Index: 3
  IsKey: False
  IsEnd: False

SequenceState:
  IsStart: False
  Index: 0
  IsEnd: False

MappingState:
  IsStart: False
  Index: 0
  IsKey: False
  IsEnd: False

I've already tried several combinations since I can't find an example declaring groups with conditions without any success, any idea?

Thanks

like image 285
Ricardo Avatar asked Nov 26 '25 12:11

Ricardo


1 Answers

Make sure you've got the indentation right. Also, the ${{if ...}} is a part of the list, the line should start with a hyphen:

variables:
  - name: variableGroupName
    value: app-${{parameters.environmentInitials}}
  - template: /pipelines/variables/${{parameters.environmentInitials}}.yml
  - group: ${{variables.variableGroupName}}
  
  - ${{if parameters.parentEnvironmentInitials}}:
    - group: app-${{parameters.parentEnvironmentInitials}}

Another possibility: the error might be coming from inside of the included template (- template: /pipelines/variables/${{parameters.environmentInitials}}.yml).

like image 177
qbik Avatar answered Nov 29 '25 23:11

qbik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!