YAML Templates in Azure Pipelines: Reusable and Secure Processes
In the world of DevOps, efficiency and security are paramount. YAML templates in Azure Pipelines address these needs by allowing you to define reusable content, logic, and parameters. This means you can avoid duplication and enforce best practices across your pipelines, leading to cleaner and more maintainable code.
Templates work by inserting reusable content into your pipeline. When you use an 'include' directive, it pulls in content from a specified template, similar to include statements in programming languages. The 'extends' functionality allows you to define a schema that pipelines must adhere to, ensuring consistency. For example, you can define a parameter like 'buildSteps' to control the steps executed in your pipeline, with default values to simplify configuration. This structure not only enhances readability but also minimizes the risk of errors in complex pipelines.
However, be cautious when implementing templates. They can lead to increased complexity and size in your pipeline configurations. Ensure that your templates are well-organized and that you avoid excessive nesting or overly complex logic. Additionally, remember that template files must exist on your filesystem at the start of a pipeline run; you cannot reference them in an artifact. This can be a common pitfall if you're not careful about your directory structure. Overall, YAML templates are a powerful tool for enhancing your Azure DevOps pipelines, but they require thoughtful implementation to avoid pitfalls.
Key takeaways
- →Use 'extends' to enforce a schema for your pipelines, ensuring consistency.
- →Define reusable parameters like 'buildSteps' to streamline your pipeline configuration.
- →Be aware that templates must exist on your filesystem at the start of a pipeline run.
- →Avoid excessive complexity in your templates to prevent pipeline bloat.
- →Leverage 'includes' to reduce redundancy and improve maintainability.
Why it matters
Implementing YAML templates can drastically reduce duplication in your pipelines, leading to faster development cycles and fewer errors. This not only improves team efficiency but also enhances the security posture of your CI/CD processes.
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: templates/insert-npm-steps.yml
2
3steps:
4- script: npm install
5- script: yarn install
6- script: npm run compileWhen NOT to use this
Templates and template expressions can cause explosive growth to the size and complexity of a pipeline. If your project is small or your team is just starting with Azure DevOps, consider simpler configurations before adopting templates.
Want the complete reference?
Read official docsMastering Durable Functions: Building Stateful Workflows in Azure
Durable Functions empower you to create stateful workflows in a serverless environment, solving the complexity of managing state and retries. With the Durable Functions runtime, you can ensure your workflows are resilient and reliable over long periods.
Mastering Azure Functions Scale: Choosing the Right Plan
Scaling Azure Functions effectively can make or break your serverless architecture. Understand the differences between the Flex Consumption plan and the Premium plan to optimize performance and cost. This knowledge is crucial for maintaining responsive applications in production.
Mastering Reliability in Azure Functions: Best Practices You Can't Ignore
Achieving reliability in Azure Functions is crucial for any production environment. Leveraging the right hosting plans, like the Flex Consumption plan, can significantly enhance your app's performance and scalability. Dive into the specifics that will keep your functions running smoothly.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.