Using Simple Pipeline Templates in Azure DevOps

Using Simple Pipeline Templates in Azure DevOps

Implementing DRY practices

How to create a simple Pipeline Template

Create a file in a new or existing pipelines folder in your repository. I like to store these files in a directory of devops/pipelines/templates. Name the file based on the template action you would like to take. For my example, I will create a simple template for running a docker container called Super Linter, so I will call my file super-linter.yaml.

My template will have one step and will look like this:

    - script: |
        docker pull github/super-linter:latest
        docker run -e RUN_Local=true -e VALIDATE_GITLEAKS=true -v $(System.DefaultWorkingDirectory):/tmp/lint github/super-linter
      displayName: "Run Super Linter"

Save the file when ready.

How to use a template within the same Repository

In your existing pipeline file or a new one (not the template file), you can select the template file so it can be reused. To do this, you will need to add a self checkout step first:

- checkout: self

Then you will need to add the template file path as a next step to run the template:

- template: devops/pipeline/templates/super-linter.yaml

Remember, the template file path will differ from mine depending on where you locate your pipeline file.

How to use a template stored in a different Repository

Suppose you want to make this a reusable file across your entire repository stack, giving you a central location for templates across all projects. In that case, you will need to store this file in a repository and then complete the following in the pipeline file of the repo you want to run the pipeline from:

    - repository: name
      type: type
      endpoint: endpoint
      name: repo name
      ref: branch name

- checkout: self
- template: path/to/template/repo

The repositories section is the connection details to the repo where the template is stored. Follow these instructions to connect to your service connection/provider

Please make sure you change the template path to the path of the template repo.

Did you find this article valuable?

Support James Cook by becoming a sponsor. Any amount is appreciated!