0
0
AzureHow-ToBeginner · 4 min read

How to Use Stages in Azure Pipeline: Simple Guide

In Azure Pipelines, use stages to divide your workflow into distinct phases like build, test, and deploy. Each stage can run jobs and steps independently, helping you organize and control your pipeline flow.
📐

Syntax

An Azure Pipeline stage groups jobs and steps. It has a stage keyword, a name, and a list of jobs. Each job contains steps that run commands or scripts.

  • stage: Defines the stage block.
  • displayName: Friendly name shown in the pipeline UI.
  • jobs: List of jobs to run in this stage.
  • job: Defines a job inside a stage.
  • steps: Commands or tasks executed in the job.
yaml
stages:
- stage: Build
  displayName: 'Build Stage'
  jobs:
  - job: BuildJob
    steps:
    - script: echo Building...
      displayName: 'Run build script'
- stage: Test
  displayName: 'Test Stage'
  dependsOn: Build
  jobs:
  - job: TestJob
    steps:
    - script: echo Testing...
      displayName: 'Run tests'
💻

Example

This example shows a pipeline with two stages: Build and Test. The Test stage waits for Build to finish before starting.

yaml
trigger:
- main

stages:
- stage: Build
  displayName: 'Build Stage'
  jobs:
  - job: BuildJob
    steps:
    - script: echo "Building the project..."
      displayName: 'Build Step'

- stage: Test
  displayName: 'Test Stage'
  dependsOn: Build
  jobs:
  - job: TestJob
    steps:
    - script: echo "Running tests..."
      displayName: 'Test Step'
Output
Building the project... Running tests...
⚠️

Common Pitfalls

Common mistakes when using stages include:

  • Not specifying dependsOn, causing stages to run in parallel unexpectedly.
  • Using the same stage name twice, which causes errors.
  • Forgetting to indent jobs and steps properly, leading to YAML parsing errors.

Always check your YAML indentation and use unique stage names.

yaml
stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo "Build step"

- stage: Build  # Wrong: duplicate stage name
  jobs:
  - job: TestJob
    steps:
    - script: echo "Test step"
📊

Quick Reference

KeywordDescription
stagesTop-level list grouping all stages
stageDefines a single stage with a name
displayNameFriendly name shown in UI
dependsOnSpecifies stage dependencies
jobsList of jobs inside a stage
jobDefines a job inside a stage
stepsCommands or tasks run in a job

Key Takeaways

Use stages to organize pipeline into clear phases like build, test, and deploy.
Define dependencies between stages with dependsOn to control execution order.
Each stage contains jobs, and each job contains steps that run commands or scripts.
Ensure unique stage names and correct YAML indentation to avoid errors.
Stages improve pipeline clarity and allow parallel or sequential execution control.