Understanding Pipeline Triggers

Pipeline triggers automate the execution of your build or release pipelines. They allow your pipelines to start automatically when certain events occur, such as code commits, work item updates, or scheduled times. This significantly improves efficiency and reduces manual intervention.

Types of Triggers

Azure Pipelines supports several types of triggers:

Configuring CI Triggers (YAML Pipelines)

In YAML pipelines, triggers are defined directly within the pipeline definition file. The most common trigger is the CI trigger.

trigger:
  - main  // Trigger on commits to the 'main' branch
  - develop // Also trigger on commits to the 'develop' branch

  branches:
    include:
      - main
      - release/*
    exclude:
      - feature/*

  paths:
    include:
      - src/app/*   // Only trigger if changes are in src/app directory
      - pipelines/*
    exclude:
      - docs/*
      - *.md

Explanation:

Note: By default, if no trigger section is specified in a YAML pipeline, the pipeline will trigger for all branches. Explicitly defining triggers is a best practice.

Configuring PR Triggers (YAML Pipelines)

Pull Request triggers are crucial for code quality. They ensure that changes are validated before being merged.

pr:
  branches:
    include:
      - main
      - develop
    exclude:
      - feature/*

  paths:
    include:
      - src/backend/*
    exclude:
      - tests/*
      - README.md

This configuration ensures that a build is automatically triggered whenever a pull request targeting the main or develop branches is created or updated, provided the changes are within the src/backend/ directory.

Configuring Scheduled Triggers (YAML Pipelines)

Scheduled triggers are useful for nightly builds, periodic cleanups, or re-running failed builds.

schedules:
  - cron: "0 0 * * *"  // Runs daily at midnight UTC
    displayName: Daily midnight build
    branches:
      include:
        - main
      exclude:
        - release/*
    always: true  // Run even if no code changes

  - cron: "0 8 * * 1-5" // Runs every weekday at 8 AM UTC
    displayName: Weekday morning build
    branches:
      include:
        - develop
    always: false // Only run if there are pending changes

Explanation:

Tip: When defining cron schedules, remember that the time is in UTC by default. You can use online cron expression generators to help create your schedules.

Configuring Triggers in Classic UI Pipelines

For pipelines created using the Classic editor, triggers are configured through the pipeline's UI settings.

  1. Navigate to your pipeline.
  2. Click on the "Edit" button.
  3. For build pipelines, go to the "Triggers" tab.
  4. For release pipelines, select a specific stage and go to its "Pre-deployment conditions" or "Post-deployment conditions" to configure triggers.

You'll find options to enable/disable CI triggers, specify branch filters, configure PR triggers, and set up scheduled triggers with similar parameters as in YAML.

Important: While Classic UI pipelines offer a visual way to configure triggers, using YAML pipelines for defining both your code and your CI/CD infrastructure is the recommended modern approach for its benefits in version control, reusability, and consistency.

Triggers in Release Pipelines

Release triggers are configured per stage in the release pipeline editor:

Understanding and correctly configuring triggers is essential for building efficient and responsive CI/CD workflows in Azure DevOps.