what can I put into azure build pipeline yml file in the 'trigger' section
- User
what can I put into azure build pipeline yml file in the 'trigger' section
- Assistant
what can I put into azure build pipeline yml file in the 'trigger' section
In Azure Pipelines, the trigger section of your YAML file defines the conditions under which your pipeline is automatically triggered. This typically involves specifying which branches or paths should initiate a build when changes are pushed. Below are the key elements you can configure within the trigger section, along with examples to guide you.
To trigger the pipeline on changes to specific branches, define the branch names under branches:
trigger:
branches:
include:
- main
- develop
exclude:
- feature/*
Explanation:
include: Specifies branches that will trigger the pipeline when changes are pushed.exclude: Specifies branches that should not trigger the pipeline, even if they match the include patterns.You can further refine triggers by specifying path filters. This ensures that the pipeline only runs when changes are made to specific files or directories.
trigger:
branches:
include:
- main
paths:
include:
- src/*
- docs/*
exclude:
- src/tests/*
Explanation:
paths.include: Specifies the file paths that must be changed to trigger the pipeline.paths.exclude: Specifies the file paths that should not trigger the pipeline, even if they are included otherwise.By default, Azure Pipelines will batch changes and run a single build for multiple commits that occur in quick succession. You can control this behavior with the batch option.
trigger:
branches:
include:
- main
batch: true
Explanation:
batch: true: Enables batching, meaning multiple commits are combined into a single build.batch: false: Disables batching, triggering a separate build for each commit.If you want the pipeline to trigger on all branches except those explicitly excluded, you can use an asterisk (*):
trigger:
branches:
include:
- '*'
exclude:
- 'docs/*'
Explanation:
include: ['*']: Includes all branches.exclude: ['docs/*']: Excludes any branch that starts with docs/.Azure Pipelines does not natively support tag-based triggers within the trigger section. However, you can achieve similar functionality using pipeline triggers or schedules combined with conditions.
You can combine branch and path filters to have more granular control over when the pipeline is triggered.
trigger:
branches:
include:
- feature/*
- release/*
paths:
include:
- src/
- tests/
exclude:
- src/experimental/
Explanation:
feature/* or release/* will trigger the pipeline.src/ or tests/ directories will trigger the pipeline, excluding any changes in src/experimental/.Here’s an example that combines several of the above elements:
trigger:
branches:
include:
- main
- develop
- feature/*
exclude:
- 'release/*'
paths:
include:
- src/**
- docs/**
exclude:
- src/legacy/**
batch: true
Explanation:
main, develop, and any branch starting with feature/, but excludes branches starting with release/.src/ or docs/, excluding changes in src/legacy/.Pull Request Triggers: While the trigger section handles continuous integration (CI) triggers, you can define pull request (PR) triggers separately using the pr keyword.
pr:
branches:
include:
- main
- develop
paths:
include:
- src/**
Scheduled Triggers: You can define scheduled runs independently of code changes, which is useful for periodic builds or maintenance tasks.
schedules:
- cron: "0 0 * * 0" # Every Sunday at midnight
displayName: Weekly midnight build
branches:
include:
- main
always: true
YAML Schema Reference: For more advanced configurations, refer to the Azure Pipelines YAML schema documentation.
The trigger section in your Azure Pipelines YAML file is highly flexible and allows you to define when and how your pipeline should run based on branch and path changes. By carefully configuring the trigger section, you can optimize your CI/CD process to only build when necessary, saving resources and streamlining your development workflow.
Example Full YAML Pipeline with Trigger:
trigger:
branches:
include:
- main
- develop
- feature/*
exclude:
- 'release/*'
paths:
include:
- src/**
- docs/**
exclude:
- src/legacy/**
batch: true
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
- script: |
pip install -r requirements.txt
pytest
displayName: 'Install dependencies and run tests'
This example sets up triggers based on branch and path changes, uses an Ubuntu agent, and defines steps to install dependencies and run tests.
Not yet included in a published release.