Azure DevOps – Tips and Tricks – 19 – How to reuse Templates across Projects

In this article, we are going to learn how to refer the reusable YAML templates across projects.

Advertisements

Introduction

Azure DevOps allows us to re-use the Azure DevOps – Pipeline Tasks using YAML Templates. It’s very common that a DevOps Engineer is responsible for developing similar Pipelines in multiple projects. In such scenarios, it makes sense to re-use the YAML Templates in multiple projects by placing the common template in a special Project. Below is how the Project design would look like.

Azure DevOps - Tips and Tricks - 19 - How to reuse Templates across Projects-Arch Diagram
Azure DevOps – Tips and Tricks – 19 – How to reuse Templates across Projects-Arch Diagram

Create a Template file in the Shared Project

Let’s create a simple re-usable Template File in a project name Shared Project. As shown below, the template file contains a Stage, a Job and a simple PowerShell Task.

Advertisements
parameters:
  - name: ProjectName
    type: string
stages:
  - stage: StageName
    displayName: Reusable Stage
    jobs:
      - job: JobName
        steps:
          - task: PowerShell@2
            displayName: Shared Project - Task
            inputs:
              targetType: 'inline'
              script: |
                Write-Host "PowerShell Task - Invoked from ${{ parameters.ProjectName }}"

The above Template file will be referred in multiple projects. So, you need to make sure that it is as generic as possible. As shown above, we take the Project Name as the Input Parameter and we display the same in the PowerShell Task.

Advertisements

Refer the Template File in the Project

Let’s now refer the reusable template in an Individual Project name Project1 which acts as client who would like to leverage the functionality available in the reusable template file. In order to refer a file from another project, we need to use the resources-repositories element supported by Azure DevOps. Let’s learn how to do that by creating the Azure DevOpsresourcesrepositories element as shown below.

2.Azure DevOps - Tips and Tricks - 19 - How to reuse Templates across Projects- Main Pipeline
Azure DevOps – Tips and Tricks – 19 – How to reuse Templates across Projects- Main Pipeline

Let’s run the Pipeline and review the output logs as shown below.

Azure DevOps - Tips and Tricks - 19 - How to reuse Templates across Projects- View Logs
Azure DevOps – Tips and Tricks – 19 – How to reuse Templates across Projects- View Logs
Advertisements

Points to Ponder:

  1. The Pipeline is located in the Project1
  2. The PowerShell task named Shared Project – Task is available in the Shared Project – Template file.
  3. The Logs are viewable in the Project1 itself.

Below is the complete code for reference.

Shared Project – Reusable YAML Template file – template.yml

parameters:
  - name: ProjectName
    type: string
stages:
  - stage: StageName
    displayName: Reusable Stage
    jobs:
      - job: JobName
        steps:
          - task: PowerShell@2
            displayName: Shared Project - Task
            inputs:
              targetType: 'inline'
              script: |
                Write-Host "PowerShell Task - Invoked from ${{ parameters.ProjectName }}"

Project1 – Pipeline – which refers to Reusable YAML Template file – azure-pipelines.yml

parameters:
  - name: ProjectName
    displayName: Project Name
    type: string
    default: Project1
    values:
      - Project1
resources:
 repositories:
   - repository: shared-repo
     name: Shared-Project/Shared-Project
     type: git
     #ref: main
     
stages:
- template: './Templates/template.yml@shared-repo'
  parameters: 
   ProjectName: ${{ parameters.ProjectName}}

Summary

In this article, we have learnt how to create a Reusable Template file in a Shared Project and also learnt how to refer the shared re-usable templates from multiple individual projects.

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Advertisements
Advertisements

Leave a comment