AZ-400: Designing and Implementing Microsoft DevOps Solutions
Design and Implement Pipelines
Pipeline triggers
In this article, we dive deep into Azure Pipeline Triggers and demonstrate how they streamline and automate your CI/CD workflows. Learn how to enhance your development process by understanding the four primary trigger types in Azure Pipelines.
Overview
Pipeline triggers are critical components in any CI/CD process. They automate the initiation of build and deployment pipelines based on defined conditions, ensuring efficiency from code commit to production deployment. Azure Pipelines supports four primary trigger types:
- Continuous Integration (CI) Triggers
- Pull Request (PR) Triggers
- Scheduled Triggers
- Manual Triggers
Each trigger plays a unique role in automating your pipelines and maintaining a robust development lifecycle.
Continuous Integration (CI) Triggers
Continuous Integration Triggers automatically start a build whenever source code is pushed to a repository. This immediate reaction to code modifications is essential for maintaining a fast-paced and agile development environment.
In Azure DevOps, CI triggers monitor specific branches within your version control repository. For instance, when a developer pushes a commit to the main branch, the CI trigger initiates a build and testing sequence to quickly surface any issues.
Consider this YAML snippet for configuring a Continuous Integration Trigger in an Azure Pipeline:
trigger:
branches:
include:
- main
- feature/*
This configuration means:
- The
trigger
block specifies which branches will start a pipeline run when changes are pushed. - The
main
branch is explicitly included, ensuring that any push triggers the pipeline. - The
feature/*
wildcard pattern triggers the pipeline for any branch that starts with "feature" (e.g., feature-NewLogin, feature-UpdateDatabase).
This setup is widely used in development workflows to validate both production-ready code and ongoing feature developments continuously.
Pull Request (PR) Triggers
Pull Request triggers are designed to automatically validate code changes when a pull request is created or updated. When a pull request is submitted, Azure DevOps can run a build and execute tests against the proposed changes, helping to ensure that new code integrates seamlessly with the existing codebase while maintaining quality standards.
This automated validation plays a crucial role in enforcing code review policies and preserving overall code quality.
Scheduled Triggers
Scheduled triggers enable pipelines to run at predefined times or intervals without manual intervention. This feature is particularly useful for routine tasks such as nightly builds, weekly integration tests, or daily deployments to staging environments.
Below is an example configuration for a scheduled trigger in a YAML pipeline:
trigger: none # Disables the automatic CI trigger
schedules:
- cron: "0 0 * * *" # Cron format: minute, hour, day of month, month, day of week
displayName: Daily Midnight Build
branches:
include:
- main # Specifies that the schedule applies to the main branch
always: true # Runs the pipeline even if no code changes were detected
This cron-based schedule ensures that your pipeline executes at the designated time consistently.
In classic pipeline setups, the scheduled trigger configuration is similarly intuitive:
Manual Triggers
Manual triggers provide the flexibility to execute a pipeline on-demand. Users can start these triggers via the Azure DevOps interface or through the Azure CLI. Manual triggers are especially useful for operations that need human oversight, such as deployments to production, initiating rollbacks, or handling special scenarios that fall outside the regular automated processes.
Best Practices
Adopting best practices when configuring pipeline triggers can enhance the efficiency and reliability of your CI/CD workflows.
Best Practice Reminder
- Selective Branch Triggering: Limit triggers to relevant branches to avoid unnecessary pipeline executions.
- Conditional Execution: Use conditions within your pipeline configuration to finely control when pipelines run.
- Manual Approvals: Incorporate approvals, especially for sensitive or production environments, to add an extra layer of oversight.
By following these guidelines, you can ensure that your pipelines are both efficient and robust, supporting high-quality code and seamless operations.
For more detailed insights into Azure Pipelines, visit the Azure Pipelines Documentation.
Watch Video
Watch video content