Azure DevOps Pipelines

Understanding Stages, Jobs, and Steps in Azure Pipelines

Azure Pipelines are a powerful CI/CD orchestration tool. Understanding the hierarchical structure of pipelines—stages, jobs, and steps—is fundamental to building efficient and maintainable build and release processes.

1. Stages

Stages represent the highest level of division in a pipeline. They typically correspond to different phases of your software delivery process, such as:

Stages run sequentially by default, meaning the next stage only begins after the previous one has successfully completed. However, you can configure stages to run in parallel to speed up your pipeline.

Key Characteristics of Stages:

2. Jobs

Jobs are containers within a stage that execute a set of tasks. A stage can contain one or more jobs.

Job Execution:

By default, jobs within a stage run sequentially. To enable parallel execution, you define a matrix or explicitly specify dependencies between jobs.

Example: Parallel Jobs for Different OS

You can run the same set of steps on multiple operating systems concurrently:


stages:
- stage: Build
  jobs:
  - job: BuildLinux
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: echo Building on Linux...

  - job: BuildWindows
    pool:
      vmImage: 'windows-latest'
    steps:
    - script: echo Building on Windows...
                

3. Steps

Steps are the smallest units of work in a pipeline. They are individual tasks that perform a specific action, such as running a script, compiling code, publishing an artifact, or deploying a service.

Types of Steps:

Example: A Job with Multiple Steps

A job demonstrating build, test, and publish steps:


jobs:
- job: BuildAndTest
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - checkout: self
  - task: DotNetCoreCLI@2
    displayName: 'Restore Dependencies'
    inputs:
      command: 'restore'
      projects: '**/*.csproj'

  - task: DotNetCoreCLI@2
    displayName: 'Build Project'
    inputs:
      command: 'build'
      projects: '**/*.csproj'
      arguments: '--configuration Release'

  - task: DotNetCoreCLI@2
    displayName: 'Run Unit Tests'
    inputs:
      command: 'test'
      projects: '**/*Tests.csproj'
      arguments: '--configuration Release'

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact'
    inputs:
      pathToPublish: '$(Build.ArtifactStagingDirectory)'
                

Relationship Between Stages, Jobs, and Steps

The hierarchy is straightforward:

This structure provides a clear and organized way to define your CI/CD workflows, allowing for parallel execution, conditional logic, and dependency management at different levels.

Level Purpose Execution Parallelism
Stage Major phase (Build, Deploy) Sequential (by default) Across stages (optional), within stages (jobs can run parallel)
Job Container for tasks on an agent Sequential within stage (by default) Within a stage (can run parallel on different agents)
Step Single unit of work (task/script) Sequential within job No (steps are sequential within a job)

Best Practices

  • Organize logically: Use stages for distinct deployment environments or major process phases.
  • Leverage parallel jobs: Configure jobs within a stage to run in parallel to save time.
  • Keep steps focused: Each step should perform a single, well-defined action.
  • Use templates: For complex pipelines, consider using YAML templates to promote reusability and maintainability.

By mastering the concepts of stages, jobs, and steps, you can build robust, efficient, and scalable CI/CD pipelines in Azure DevOps.