YAML Templates in Azure Pipelines: Reusable and Secure Processes
YAML templates in Azure Pipelines exist to streamline your CI/CD processes by promoting reusability and security. They solve the problem of repetitive code and potential errors by allowing you to define common steps and logic in a centralized manner. This means you can maintain a clean and efficient pipeline configuration while ensuring that your processes adhere to defined standards.
Templates work by letting you insert reusable content into your pipeline through 'includes' and 'extends'. The 'includes' templates allow you to insert common steps, while 'extends' templates define the schema and logic that your pipeline must follow. For example, you can define parameters like 'buildSteps' and set defaults for any unspecified parameters, such as 'vmImage'. Azure imposes limits, such as a maximum of 100 separate YAML files and 100 levels of template nesting, to prevent complexity from spiraling out of control.
In production, you need to be aware of a few critical points. Ensure that your template files are present on your filesystem at the start of a pipeline run; referencing templates in an artifact won’t work. Also, be cautious with the parameters you define and the logic you enforce, as improper configurations can lead to syntax errors that halt your pipeline. Remember, you need an Azure DevOps project and basic YAML knowledge to get started effectively.
Key takeaways
- →Define reusable content using 'includes' templates to simplify your pipelines.
- →Control pipeline structure with 'extends' templates to enforce security and logic.
- →Set default parameters like 'buildSteps' and 'vmImage' for streamlined configurations.
- →Be aware of Azure's limits on YAML files and nesting to avoid complexity.
- →Ensure template files are accessible at the start of a pipeline run to prevent errors.
Why it matters
Using YAML templates can significantly reduce duplication and errors in your CI/CD processes, leading to faster and more reliable deployments. This approach enhances security by enforcing a defined structure and logic within your pipelines.
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 →Unlocking Security: The Power of Azure Integrated HSM
Azure Integrated HSM is a game-changer for securing cryptographic keys directly in hardware. By ensuring keys never leave the hardware boundary, it mitigates key exfiltration risks that plague traditional software-based solutions. Dive in to understand how this impacts your security posture.
Mastering API Governance with Azure API Management
Microsoft's Azure API Management is a game-changer for organizations looking to govern their APIs and AI workloads. With its robust governance layer, you can enforce security policies and monitor usage seamlessly. Dive into how this platform can streamline your API management processes.
Mastering Azure Event Hubs: The Backbone of Real-Time Data Streaming
Azure Event Hubs is your go-to solution for real-time data streaming, capable of handling massive data ingestion with ease. With support for multiple protocols like Apache Kafka and AMQP 1.0, it decouples event producers from consumers, enabling scalable architectures.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.