Blog

How to Share Azure Devops CI/CD Templates Across Teams

16 Apr, 2024
Xebia Background Header Wave

Introduction

As Timo and Jeroen explained in their blog about bringing your Azure Devops CI/CD setup to the next level using templates to re-use your pipeline steps is a good way to accelerate your development of your CI/CD pipelines. In this short blog I want to explain how you can share your templates with other teams.

Extract your templates into a separate repository

To be able to share your templates with other teams the first step is to extract your templates from your repo into a specific repo for your templates.

Current situation

In the current (soon to be old) situation the templates are placed in a subdirectory called templates inside the same repository as where they are used.

templates inside own repo

Templates moved to a separate repository

When we want to share the templates and use them from within multiple other repositories, without copy pasting them, we need to give them their own place to live. Therefore we extract the templates into their own repository. The new repository will contain multiple types of templates. We separate the types into different subdirectories so the new repository contains our templates inside the templates/python subdirectory as shown below.

templates in own shared repo

In the repository where we extracted the templates from we need to get rid of them so that the new structure looks like this:

templates removed from repo

I admit that there is not much that is exciting about a removed directory.

Using the external templates

To use these templates we need to refer to the repository that contains them from within our pipeline yaml. In this blogpost we show you how to do this when both repositories are in the same organization. It is however possible to reference templates from another organization. This is explained pretty well in the documentation.

To be able to reference the templates inside our pipeline we first need to create a reference to the templates repository. This is done in the resource section of the pipeline yaml.

resources:
  repositories:
    - repository: sharedDevopsPipelineTemplates # Name to reference this repository at a later step
      type: git
      name: "better-devops-pipelines-blogpost/better-devops-pipelines-blogpost-external-templates"
      ref: refs/tags/1.0.0

The ref can be changed to any git reference so you can decide yourself when to update to a newer version or use a branch reference to test out new/changed templates before they are released.

The only thing that remains is update the template reference itself (notice the use of @sharedDevopsPipelineTemplates to tell azure where to get the template from).

    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: $(python.version)

    - script: |
        pip install .[dev]
      displayName: 'Install dependencies'

    - template: templates/python/code-quality.yml@sharedDevopsPipelineTemplates

Conclusion

That’s all folks. We demonstrated how to improve your Azure DevOps CI/CD setup by extracting your templates, making it even more reusable.

the example repository containing the improved pipeline is available at:

better-devops-pipelines-blogpost – Repos

the example repository containing the shared templates is available at:

better-devops-pipelines-blogpost-external-templates – Repos

Cheers 🙏

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts