Can parameters be defined in one YAML file and reused in another?

Viewed 6787

TL;DR

Is it possible to have a parameter and its values in a template YAML and have the main YAML read the list and present them in a drop-down when preparing to run a pipeline?

Very similar to the MS example of "Variable reuse", shown here.

# File: vars.yml
variables:
  favoriteVeggie: 'brussels sprouts'
# File: azure-pipelines.yml

variables:
- template: vars.yml  # Template reference

steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.

Details

I have a list of some 50 values that grow from week to week. When an Ops person runs my pipeline, I would like them to select a value from a drop-down.

This can be achieved simply by specifying a parameter block in the pipeline YAML file(azure-pipeline.yml):

parameters:
- name: subscription
  displayName: Subscription Name
  type: string
  default: sub A
  values:
  - sub A
  - sub B
  - sub C
  - sub D

This gives me the drop-down in the UI:

Run pipeline drop-down

In my example above, there are only four values, however, we have over 50 values (subscriptions) and growing. I am trying to devise a way where 1) Ops won't have to edit the pipeline 2) keeping the main pipeline file concise.

I know there is currently no way to dynamically populate the list of values, but what I am looking at doing is to use a template YAML file that will have just the values and it will read the template, populating the drop-down. It is then a fairly straight forward task to have a script/pipeline dynamically update the values in the template file.

Reading the docs and some blogs, I believe this may be possible, but I haven't managed to crack it.

It appears that the parameter block can take "any YAML structure", although I haven't been able to find a suitable example of this. Amongst other things, this is what I have tried:

templates/subscriptions.yml

- name: subscription
  displayName: Subscription Name
  type: string
  default: sub A
  values:
  - sub A
  - sub B
  - sub C
  - sub D``

azure-pipelines.yml

...
template: templates/subscriptions.yml
  parameters:
    subscription: []

stages:
- stage: Example
  displayName: 1.0 Demo Stage
  jobs:
...

I wonder if my issue is that I need the parameters to be read right at the start and this isn't supported. It only works in a stage/job etc.

If anyone has any ideas, I'd be grateful if you could point me in the right direction.

T.I.A.

1 Answers

Based on your requirement, I am afriad that there is no such mehtod could meet your requirements for the time being.

The yaml template in Azure Devops needs to be referenced by the main yaml(e.g. azure-pipelines.yml) to pass the value.

Please refer to this doc: Yaml schema.

The parameters field in YAML cannot call the parameter template in yaml. Therefore, if only pure parameters are defined, they cannot be called in the main yaml.

This method is only supported by variables.

But your requirement makes sense. I suggest that you could submit a suggestion ticket in Our UserVoice Site.

Related