Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a YAML template that extends another

Back in the days I created a yaml template which encapsulates the deployment steps for one application. There are several pipelines which are extending from this template (passing the specific parameters to the template to deploy an specific app) working like a charm.

Now it is necessary to create a pipeline which deploys not one but n applications exactly in the same way, but my template only works for one. The obvious answer would be "take the deployment steps, put them in a different template, include the new template and call it n times". But I need to extend (my current template is the required template to access a protected resource).

So, Any suggestion to deal with this? My first thought is: Can I, in some way, include a template which extends from another?

like image 576
Josto Avatar asked Jan 22 '26 13:01

Josto


1 Answers

Based on your description, you need to use required template feature in Azure DevOps. In this case, the extend template is required to be used in YAML sample.

it is necessary to create a pipeline which deploys not one but n applications exactly in the same way, but my template only works for on

If you need to add include template in the main yaml and use the extend template in the child yaml, this is not supported.

To meet your requirement, you can use the extend template in the main yaml and use include template in the child yaml template file.

Here is an example:

Main yaml:

parameters:
  - name: APPs
    type: object
    default: [1,2,3]
pool:
  vmImage: ubuntu-latest

extends:
  
  template: test1.yml
  parameters:
    APPname: ${{ parameters.APPs }}

Child YAML template:

parameters:
  - name: APPname
    type: object

jobs: 
- ${{ each parameter in parameters.Appname }}:  
  - template: test.yml

Yaml template which encapsulates the deployment steps

jobs:
- job: 
  steps:
  - xxx

Result: You can input the names of the apps and the each expression will loop the value and run the templates.

enter image description here

Note: In this case, you need to change the required template to the child Yaml template.

Update:

Refer to the following sample to create object type parameters with three params:

Main yaml:

parameters:
  - name: APPs
    type: object
    default:
      - Appname: test
        Pipelinename: test
        JobName: test
      - Appname: test1
        PipelineName: test1
        JobName: test1
pool:
  vmImage: ubuntu-latest
variables:
- group:  test
extends:
  
  template: test1.yml
  parameters:
    APPObject: ${{ parameters.APPs }}

Child YAML template:

parameters:
  - name: APPObject
    type: object

jobs: 
- ${{ each parameter in parameters.APPObject }}:  
  - template: test.yml
    parameters:
     appname: ${{ parameter.Appname }}
     PipelineName: ${{ parameter.Pipelinename }}
     JobName: ${{ parameter.JobName }}

Yaml template which encapsulates the deployment steps

parameters:
  - name: appname
    type: string
  - name: PipelineName
    type: string
  - name: JobName
    type: string
jobs:
- job: 
  steps:
  - script: echo "${{parameters.appname}}"
like image 79
Kevin Lu-MSFT Avatar answered Jan 25 '26 07:01

Kevin Lu-MSFT



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!