0

Is it possible to store parameters in templates and call them from pipeline? I just want to have a several templates with different set of parameters and have possibility to call them from main pipeline.

For example: template1.yml

parameters:
  - name: solutions
    type: object
    default: [a,b,c,d,e]

template2.yml

parameters:
  - name: solutions
    type: object
    default: [a1,b1,c1,d1,e1]

azure-pipeline.yml

trigger:
- none

pool:
  vmImage: 'ubuntu-latest'

steps:
  - template: template.yml
  - script: echo ${{ each solution in parameters.solutions }}
  - template: template2.yml
  - script: echo ${{ each solution in parameters.solutions }}
  • You'd put them in variables, then pass the variables to the template as parameters. Or you could use conditions to choose which template to run. There are a lot of options, your desired outcome isn't really clear. – Daniel Mann Apr 17 '21 at 16:28
  • @DanielMann I mean that I want to use templates only for storing parameters and nothing else. And then call template to another pipeline to inject these parameters to pipeline – Yurii Paneiko Apr 18 '21 at 17:11
  • Just checking to see if the information provided was helpful. If my reply helped or gave a right direction. Appreciate for marking it as an answer which will also help others in the community. – Walter Apr 27 '21 at 07:43

2 Answers2

0

Is it possible to store parameters in templates and call them from pipeline?

I'm afraid we can not store parameters in templates and reuse them. Currently we only support reuse of variables with templates. For example:

# 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 }}.

If you would like that feature, please create a request for this feature in the Developer Community. This link is directly monitored by the product team and they will look into this request and share their views on the same. If lot of users request for the same feature, they will add this item to backlog to implement in the future sprints.

By the way, I found a suggestion ticket with similar requests. You can vote and track this ticket.

Walter
  • 2,640
  • 1
  • 5
  • 11
0

Not having variables support complex structure and not being able to store parameters as a template prevents what would be simple solutions for sure.

One way that I've worked around this which is reasonable in the end, is having layered templates.

  1. Have your primary template that does the core work, with whatever complex parameters you need.

  2. Create another secondary template that simply calls the core template, but actually specifying the complex objects to be passed in and just passing through the other parameters.

  3. In the pipeline, reference the secondary template, where it would have the same parameters as the primary, with the exception it wouldn't have the complex parameters.

Example:

primary template - primary_template.yaml

parameters:
- name: environments
  type: object
- name: releaseName
  type: string
- name: imageTag
  type: string

stages:
- stage: MyStage
  jobs:
  - deployment: Deployment
    pool:
      name: myPoolName
    environment: MyEnvironment
    strategy:
      runOnce:
        deploy:
          steps:
             XXXXXX

Secondary template (note the complex parameter is removed) - secondary_template.yaml

parameters:
- name: imageTag
  type: string  
- name: releasename
  type: string

stages:
  - template: primary_template.yaml@AzDoTemplates
    parameters:
      imageTag: ${{ parameters.imageTag }}
      releaseName: ${{ parameters.releaseName }}
      environments:
        - name: Development
          mylist:
            - Something1
            - Something2

Then in your pipeline(s):

- template: secondary_template.yaml@AzDoTemplates
  parameters:
    releaseName: ${{ variables.releaseName }}
    imageTag: $(Build.BuildNumber)

So in the end, you are able to store the parameters in a template. You can then have multiple secondary templates for different sets of reusable parameters, without having to actually define them in the pipeline. If you have a change, it's in one place, the secondary template versus all of the pipelines.

Dave H
  • 31
  • 2