YAML Can local variables be mixed with group-variables AND have their naming simplified?

Viewed 384

This is my first time working with YAML, but I am running into an issue where it seems like if I want to include a variable-group (i.e., signing certificate password) with local pipeline-related variables then I cannot use the simplified naming convention where the variable's name and value can both be defined and set on the same line.

For example, wat I want is something similar to this (I made sure spacing is correct in the YAML):

variables:
  solutionName: Foo.sln
  projectName: Bar
  buildPlatform: x64
  buildConfiguration: development
  major: '1'
  minor: '0'
  build: '0'
  revision: $[counter('rev', 0)]
  vhdxSize: '200'
- group: legacy-pipeline
  signingCertPwd: $[variables.SigningCertificatePassword]

But, this results in a parsing error. As a result, I have to use a denser, but more bloated looking, format of:

variables:
- name: solutionName
  value: Foo.sln
- name: projectName
  value: Bar
- name: buildPlatform
  value: x64
- name: buildConfiguration
  value: development
- name: major
  value: '1'
- name: minor
  value: '0'
- name: build
  value: '0'
- name: revision
  value: $[counter('rev', 0)]  
- name: vhdxSize
  value: '200'
- group: legacy-pipeline
- name: signingCertPwd
  value: $[variables.SigningCertificatePassword]

It seems like the simplified naming format is only available if I use it for local variables, but if I add a variable-group then the simplified format goes away. I have tried searching the web for a solution for this, but I am not able find anything useful for this. Is what I am trying to achieve possible or no? If yes, how can it be done?

1 Answers

Unfortunately mixing the styles is not possible, but you can work around that using templates:

# pipeline.yaml

stages:
- stage: declare_vars
  variables:
  - template: templates/vars.yaml
  - group: my-group
  - template: templates/inline-vars.yaml
    parameters:
      vars:
        inline_var: yes!
        and_more: why not
  jobs:
  - job:
    steps:
    - pwsh: |
        echo 'foo=$(foo)'
        echo 'bar=$(bar)'
        echo 'var1=$(var1)'
        echo 'inline_var=$(inline_var)'
# templates/vars.yaml

variables:
  foo: bar
  bar: something else
# templates/inline-vars.yaml

parameters:
- name: vars
  type: object
  default: {}

variables:
  ${{ each var in parameters.vars}}:
    ${{var.key}}: ${{var.value}}

templates/vars.yaml is just simply moving variables to another file. templates/inline-vars.yaml lets you define inline variables using the denser syntax together with referencing groups, but there's additional ceremony of writing template:, parameters:, vars:.

Related