Protego field desk
CI Pipelines: Packages and Testing · part of 50-55% of exam

L9. YAML Pipelines: Triggers, Templates & Multi-Stage Builds

Course outlineLesson 9 of 18
AZ-400 Exam Prep: course overview

CI, PR, scheduled, and pipeline-resource triggers, how stages/jobs/steps compose a multi-stage pipeline, and how YAML templates and Key Vault-linked variable groups eliminate duplication.

Quick answer: An Azure Pipelines YAML file is organized as stages, which contain jobs, which contain steps. Triggers decide when it runs: CI on push, PR validation, schedules, or another pipeline finishing. Templates reuse shared steps, variable groups linked to Key Vault share secrets, and environment checks such as approvals guard each deployment.

Pipeline Triggers

A pipeline starts when an event occurs. Azure Pipelines and GitHub Actions support multiple trigger types: CI triggers fire on pushes to specified branches or paths. You can configure "run this pipeline whenever someone pushes to main or any feature branch." PR validation runs on pull request creation or updates. Azure Pipelines YAML pr: triggers support GitHub and Bitbucket Cloud. For Azure Repos Git, configure a build validation branch policy on the target branch instead of relying on pr: in YAML. Scheduled triggers run on a cron-like schedule, useful for nightly builds, weekly security scans, or daily deployments to staging. Pipeline resource triggers (Azure DevOps) fire when another specified pipeline completes, enabling chained workflows where a build pipeline triggers a deployment pipeline.

Multi-Stage YAML Pipeline Structure

A YAML pipeline is hierarchical: stages contain jobs, which contain steps.

A stage groups related work. A build stage compiles code. A test stage runs tests. A deploy stage pushes to an environment. Stages use dependsOn for dependencies and condition for execution criteria. The default condition requires dependency success; a custom condition can change that behavior. Jobs within a stage can run in parallel if they do not depend on each other.

This structure lets you express the full build-to-deploy flow in a single file:

  • Stage 1: Build (runs first)
  • Stage 2: Test (waits for Build to succeed)
  • Stage 3: Deploy to Dev (waits for Test to succeed)
  • Stage 4: Deploy to Prod (waits for Deploy to Dev, may require approval)

YAML Templates and Variable Groups

YAML templates are reusable, parameterized pipeline fragments. Instead of copying the same security scanning or build steps into every pipeline file, you define them once in a template and include them via template: in each pipeline. This eliminates duplication: a template change reaches every pipeline on its next run, unless a pipeline pins the template repository to a fixed ref. An extends template goes further and makes a pipeline run inside an approved structure; a Required template check on a protected resource enforces that. Variable groups centralize variables and secrets across multiple pipelines. A variable group can be linked to Azure Key Vault, so secret values (passwords, API keys, tokens) are stored securely and referenced by name. Without this, secrets would be duplicated across many pipeline YAML files or stored insecurely.

YAML Environments and Deployment Checks

A YAML environment represents a deployment target (dev, staging, production). An environment can have checks attached, such as manual approvals, business hours and a query of Azure Monitor alerts.

When a deployment job targets an environment, it automatically inherits all checks for that environment, regardless of which pipeline YAML references it. This means approval requirements and safeguards are enforced consistently across all pipelines deploying there.

Trigger TypeWhen It FiresUse Case
CI triggerOn push to specified branches/pathsRun on every code change
PR triggerOn pull request create/updateValidate before merge
ScheduledOn a cron scheduleNightly builds, weekly scans
Pipeline resourceWhen another pipeline completesChain build to deploy
Exam tip: Multi-stage YAML with stages, jobs, and steps is the standard pipeline model tested on AZ-400; understand the stages/jobs/steps hierarchy and how dependsOn and condition control flow.

Frequently Asked Questions

Why does my YAML pr: trigger not run for Azure Repos?

YAML pr: triggers only apply to GitHub and Bitbucket Cloud repositories. For Azure Repos Git, add a build validation branch policy on the target branch, which queues the pipeline for each pull request.

What is the difference between a stage, a job and a step?

A step is one task or script. A job is a group of steps that runs on one agent. A stage groups jobs into a phase such as Build or Deploy, and stages run one after another by default unless dependsOn says otherwise.

Should I use a template or a variable group?

A template reuses pipeline logic, such as the steps of a security scan. A variable group reuses values, such as service connection names and secrets, and can read secrets from Azure Key Vault at run time. Most teams use both.

Where do approvals go in a YAML pipeline?

Not in the YAML file. Approvals and other checks are configured on the environment or another protected resource in Azure DevOps, so every pipeline that deploys there has to pass them.

References

Continue Learning

Exam Focus Points
  • ✓CI triggers fire on pushes to specified branches or paths; PR validation uses YAML pr: for GitHub/Bitbucket Cloud and build validation policies for Azure Repos Git; scheduled triggers run on a cron-like schedule; pipeline resource triggers fire when another specified pipeline completes
  • ✓In a multi-stage YAML pipeline, stages contain jobs which contain steps, and a stage can use dependsOn and condition to control both ordering and whether it runs at all
  • ✓YAML templates let common steps be defined once in a separate parameterized file and included via template: across many pipelines, avoiding duplication that task groups solved for classic (non-YAML) pipelines
  • ✓Variable groups can be linked to Azure Key Vault, letting secret values be centrally managed and shared across multiple pipeline YAML definitions rather than duplicated as pipeline-specific secrets
  • ✓A YAML environment (used in deployment jobs) represents a deployment target and can have checks like manual approval attached to it directly, so those checks apply to any pipeline that deploys to that environment
Knowledge Check

1. A team wants build, test, and deployment to three environments all expressed in one pipeline file, with each stage only running after the previous one succeeds. What YAML pipeline structure supports this?

2. Ten pipelines across different repositories all need the same three-step security scanning sequence, currently copy-pasted into every YAML file. What reduces this duplication?

3. A deployment job needs to require manual approval before deploying to production, and that same approval requirement should apply no matter which pipeline is deploying there. What is the right mechanism?