Streamlining Build Pipelines with YAML Template Extension: A Practical Guide
In modern development workflows, maintaining consistency across build pipelines is crucial. A well-organized build process ensures reliability and minimizes repetitive configuration. For developers using YAML-based pipelines (e.g., Azure DevOps or GitHub Actions), template extension is a powerful approach to achieve this. This blog explores how to use YAML templates effectively to manage build stages for multiple functions in your project.
What is Template Extension in YAML?
Template extension allows you to define reusable configurations in one place and extend them for specific use cases. Instead of repeating the same build steps for every function or service, you can create a single template with customizable parameters.
Why Use Templates in Build Pipelines?
– Scalability: Add new services or functions without duplicating code.
– Maintainability: Update logic in one place instead of modifying multiple files.
– Consistency: Ensure uniform processes across different builds.
Step-by-Step Implementation
Here’s how you can set up a build pipeline using template extension.
1. Create a Reusable Template
A template defines the common steps in your build process. For example, consider the following file named buildsteps-template.yml
:
parameters: - name: buildSteps # the name of the parameter is buildSteps type: stepList # data type is StepList default: [] # default value of buildSteps stages: - stage: secure_buildstage pool: name: Azure Pipelines demands: - Agent.Name -equals Azure Pipelines x jobs: - job: steps: - task: UseDotNet@2 inputs: packageType: 'sdk' version: '8.x' performMultiLevelLookup: true - ${{ each step in parameters.buildSteps }}: - ${{ each pair in step }}: ${{ pair.key }}: ${{ pair.value }}
2. Reference the Template in the Main Pipeline
This is your main pipeline file:
trigger: branches: include: - TEST {Branch name} paths: include: - {Repository Name}/{Function Name} variables: buildConfiguration: 'Release' extends: template: ..\buildsteps-template.yml {Template file name} parameters: buildSteps: - script: dotnet build {Repository Name}/{Function Name}/{Function Name}.csproj --output build_output --configuration $(buildConfiguration) displayName: 'Build {Function Name} Project' - script: dotnet publish {Repository Name}/{Function Name}/{Function Name}.csproj --output $(build.artifactstagingdirectory)/publish_output --configuration $(buildConfiguration) displayName: 'Publish {Function Name} Project' - script: (cd $(build.artifactstagingdirectory)/publish_output && zip -r {Function Name}.zip .) displayName: 'Zip Files' - script: echo "##vso[artifact.upload artifactname={Function Name}]$(build.artifactstagingdirectory)/publish_output/{Function Name}.zip" displayName: 'Publish Artifact: {Function Name}' condition: succeeded()
Benefits in Action
1. Simplified Updates
When you need to modify the build process (e.g., change the .NET SDK version), you only update the template.yml
. The changes automatically apply to all functions.
2. Customization
Each function can have its own build configuration without duplicating the pipeline logic.
3. Improved Collaboration
By centralizing common configurations, teams can work independently on their functions while adhering to the same build standards.
Best Practices
- Parameterize Extensively: Include parameters for configurable aspects like build configurations, target environments, or runtime versions.
- Version Control Templates: Keep templates under version control to track changes and maintain consistency.
- Validate Templates: Test the templates with different parameter combinations to ensure flexibility.
Final Thoughts
YAML template extension is a game-changer for developers managing multiple services or functions in a project. It simplifies pipeline creation, reduces duplication, and enhances scalability. By adopting this approach, you can focus on building great software while your pipelines handle the heavy lifting.
If you haven’t already, try applying template extension in your next project—it’s a small investment with a big payoff.
We hope you found this article useful, and if you would like to discuss anything, you can reach out to us at transform@cloudfronts.com