azure-devopsazure-pipelinesazure-pipelines-yaml

Using templates to pass paramters in an Azure Devops 2020 yaml pipeline


I would like to implement a yaml pipeline such that I can set certain solution parameters once and then use it in multiple pipelines. Currently I have:

azure-pipelines.yml

trigger:
- master

name: $(BuildVersionPrefix).$(DayOfYear)$(Date:HH)

extends:
  parameters:
    buildTemplate: build.yml
  template: solutions.yml

solutions.yml

parameters:
  - name: buildTemplate
    type: string
    default: ""

extends:
  template: ${{parameters.buildTemplate}}
  parameters:
    solutions: 
      - name: a
        SomeOtherProperty: x
      - name: b
        SomeOtherProperty: y
      - name: c
        SomeOtherProperty: z
      - name: d
        SomeOtherProperty: u
      - name: e
        SomeOtherProperty: v

build.yml

parameters:
  - name: solutions
    type: object
    default: []

steps:
 - ${{ each solution in parameters.solutions}}:
    - script: echo ${{solution.name}}
      displayName: build ${{solution.name}}

With this configuration I need to have two yml files per pipeline and one that can be shared across multiple pipelines. Is there a way to have one yml file per pipeline while abstracting out the solutions into a single shared file?

EDIT 1

To emphasize that we need full syntax for the solutions I added SomeOtherProperty with different values


Solution

  • Is there a way to have one yml file per pipeline while abstracting out the solutions into a single shared file?

    You could try the following sample:

    BuildandSolution.yml:

    parameters:
      - name: solutions
        type: object
        default: [a,b,c,d,e]
    
    steps:
    - ${{ each solution in parameters.solutions }}: 
        - script: echo ${{ solution }}
          displayName: build ${{ solution }}
    

    Azure-Pipelines.yml:

    trigger:
    - none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
      - template: buildandsolution.yml
    

    You could directly add the value to the solution paramters(Object type).

    Update:

    I could understand your requirement, but I am afraid that we couldn't specify the solution parameters separately from from the build steps.

    The following are the two blocks:

    1.The format to define the object type parameters couldn't be used directly when defining parameters:

    solutions: 
      - name: a
        SomeOtherProperty: x
    

    This method of definition needs to depend on another template.

    2.We can't just define parameters in the template without adding any build steps.

    This method is only supported by variables.

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