top of page
  • Writer's pictureMax Morrow

Azure DevOps: Using Azure DevOps YAML Templates

Updated: Oct 13, 2021

For those of you who use Azure DevOps day-to-day for your source control and CI/CD pipelines you've probably had the conversation regarding to use, or not use, YAML pipelines come up. In this post I'm going to talk about why you should start using YAML and how to use YAML templates to improve your ability to manage your, potentially many, pipelines.

If you're already using YAML and just need some help leveraging YAML templates jump to The How section below.

The Why: Why You Should Use YAML Templates

  1. Don't Repeat Yourself: Templates allow you to create custom tasks, jobs, and stages that you can reuse elsewhere in your pipelines. What this means is you can create easily reusable templates designed to cover your most common release platforms.

  2. Security: A lesser known benefit of leveraging YAML templates is that you're able to secure your pipelines from manipulation. Azure DevOps allows you to establish approval gates within your pipelines that prevent deployment unless the pipeline specifically inherits from a pipeline template that you specify. If you keep your pipeline template in a repository separate from your code, team members are unable to manipulate the logic and force deployments (worst case scenario).

The How: Let's Talk About How to Use YAML Pipeline Templates


Think of pipeline templates similar to object inheritance. When you use them, you're inheriting the behavior of the child (inherited) pipeline. Optionally, you're able to add parameters that you can pass into your YAML templates to customize the behavior based on dynamic values.


Let's walk through a simply example of how to set up your Azure DevOps organization to use pipelines templates. You can find the completed solution here:

  1. Create a Repository within Azure DevOps to house your templates (I'll use the name Pipeline-Templates in this example)

  2. Within your repository, create a file called "task-output-message.yml". Add the following code:

  3. parameters: message: "" steps: - script: | echo "${{ parameters.message }}" displayName: "Example: Output a Message"

  4. Within the same repository, create a file called "stage-output-message.yml". Add the following code:

  5. parameters: - name: "message" type: string stages: - stage: output_message_stage displayName: "Output Message Stage" jobs: - job: output_message_job displayName: "Output Message Job" pool: vmImage: "ubuntu-latest" steps: - template: "task-output-message.yml" parameters: message: ${{ parameters.message }}

  6. Now that we have our templates set up, let's use them within a build/release pipeline. Add a file called "example-azure-pipeline.yml" in to your repository. This is where we'll inherit the behavior of stage-output-message.yml. Add the following content:

  7. name: CI-Output-Message-$(Date:yyyyMMdd-HHmmss) trigger: - master - main extends: template: stage-output-message.yml parameters: message: "Hello World"

  8. We're good to go with the YAML templates. Now push your changes and create a pipeline within your Azure DevOps organization pointing to example-azure-pipeline.yml.

  9. Run your pipeline and you should see the "Example: Output a Message" stage output "Hello World".

  10. You're done! You've created your first pipeline with YAML templates. You can find the completed example code here:

Bonus: Using Templates from A Different Repo

The example above is very simple. In most cases, if you're leveraging YAML templates, you probably don't want to use them within the same repository. If you're looking to have a repository of templates, the same concepts apply, but you'll want to add "resources" that allow you to access the pipeline templates stored elsewhere.

In the example above, we'll want to change the code we used in step 7 to:

name: CI-Output-Message-$(Date:yyyyMMdd-HHmmss)

resources: repositories: - repository: Templates trigger: - master - main extends: template: stage-output-message.yml parameters: message: "Hello World"

934 views0 comments


bottom of page