Mastering YAML Templates in Azure Pipelines for Reusable Processes
YAML templates in Azure Pipelines exist to streamline your CI/CD processes. They allow you to define reusable content, logic, and parameters, which can significantly reduce redundancy and enhance maintainability. When you include a template, it acts like an include directive in programming, injecting predefined content into your pipeline. Extending templates goes a step further by enforcing a structure and logic that your pipeline must adhere to, which is crucial for maintaining security and compliance.
The configuration parameters you can define include buildSteps, which is a list of steps to execute, and name, which provides defaults for unspecified parameters. For example, in a template file, you might define buildSteps as a step list with a default value of an empty array. This allows you to customize the steps in your pipeline while keeping the core logic intact. The use of templates can help manage complexity, but be cautious: Azure imposes limits on template nesting and file inclusion to prevent runaway growth in pipeline size and complexity.
In production, leveraging YAML templates effectively can lead to cleaner, more maintainable pipelines. However, remember that template files must exist on your filesystem at the start of a pipeline run; you can't reference them in artifacts. Additionally, keep an eye on the limits imposed by Azure to avoid performance issues. Templates can make your pipelines more powerful, but they can also introduce complexity if not managed properly.
Key takeaways
- →Define reusable content using includes templates to reduce redundancy.
- →Enforce pipeline structure with extends templates for better security.
- →Utilize parameters like buildSteps to customize your pipeline execution.
- →Be aware of Azure's limits on template nesting and file inclusion to avoid performance issues.
- →Ensure template files are present at runtime to prevent pipeline failures.
Why it matters
Using YAML templates can significantly enhance the maintainability and security of your CI/CD processes, allowing teams to scale their operations without sacrificing quality or consistency.
Code examples
1# File: start-extends-template.yml
2parameters:
3- name: buildSteps # the name of the parameter is buildSteps
4 type: stepList # data type is StepList
5 default: [] # default value of buildSteps
6stages:
7- stage: secure_buildstage
8 pool:
9 vmImage: windows-latest
10 jobs:
11 - job: secure_buildjob
12 steps:
13 - script: echo This happens before code
14 displayName: 'Base: Pre-build'
15 - script: echo Building
16 displayName: 'Base: Build'
17
18 - ${{ each step in parameters.buildSteps }}:
19 - ${{ each pair in step }}:
20 ${{ if ne(pair.value, 'CmdLine@2') }}:
21 ${{ pair.key }}: ${{ pair.value }}
22 ${{ if eq(pair.value, 'CmdLine@2') }}:
23 # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
24 '${{ pair.value }}': error
25
26 - script: echo This happens after code
27 displayName: 'Base: Signing'1# File: azure-pipelines.yml
2trigger:
3- main
4
5extends:
6 template: start-extends-template.yml
7 parameters:
8 buildSteps:
9 - bash: echo Test #Passes
10 displayName: succeed
11 - bash: echo "Test"
12 displayName: succeed
13 # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
14 - task: CmdLine@2
15 inputs:
16 script: echo "Script Test"
17 # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
18 - script: echo "Script Test"1# File: azure-pipelines.yml
2
3jobs:
4- job: Linux
5 pool:
6 vmImage: 'ubuntu-latest'
7 steps:
8 - template: templates/insert-npm-steps.yml # Template reference
9- job: Windows
10 pool:
11 vmImage: 'windows-latest'
12 steps:
13 - template: templates/insert-npm-steps.yml # Template referenceWhen NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsSimple, affordable cloud — VMs, Kubernetes, and managed databases in minutes. Trusted by 600,000+ developers. Spin up a Droplet in 60 seconds.
Try DigitalOcean →Mastering Azure Pipelines Environments: A Practical Guide
Azure Pipelines environments are essential for managing your deployment resources effectively. By targeting environments, you can ensure traceability of commits and control security. Dive in to learn how to create and utilize these environments in your CI/CD workflows.
Mastering YAML Schema in Azure Pipelines: What You Need to Know
YAML schemas in Azure Pipelines are crucial for defining your CI/CD processes effectively. Understanding how to structure pipelines, jobs, and resources can significantly streamline your deployments. Dive into the specifics that can make or break your pipeline configurations.
Building Agentic Apps with Microsoft Fabric: A Game Changer for Developers
Microsoft Fabric is transforming how developers build applications by integrating enterprise-grade backends seamlessly. With tools like Rayfin, you can deploy directly to Microsoft Fabric, ensuring robust security and scalability from day one.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.