Azure DevOps: Understanding Stages and Jobs

A detailed overview of stages and jobs in Azure DevOps pipelines.

Azure DevOps Pipelines provide a powerful way to implement CI/CD for your applications. To fully harness its capabilities, it is essential to understand two core building blocks: Stages and Jobs. These components enable you to structure your pipeline for flexibility, scalability, and maintainability.

What Are Stages?

Stages represent the major divisions in a pipeline. They allow you to logically group jobs that perform related tasks. For example, a typical pipeline might include stages such as Build, Test, and Deploy.

Key Characteristics of Stages

  • Logical Grouping:
    Each stage encapsulates a set of related jobs.

  • Execution Flow:
    Stages can execute sequentially or in parallel, depending on dependencies.

  • Approval Gates:
    Stages can include pre-approval and post-approval gates to enforce governance.

  • Isolation:
    Stages typically represent different environments (e.g., Development, QA, Production).

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo "Building the application..."

- stage: Test
  dependsOn: Build
  jobs:
  - job: TestJob
    steps:
    - script: echo "Running tests..."

- stage: Deploy
  dependsOn: Test
  jobs:
  - job: DeployJob
    steps:
    - script: echo "Deploying the application..."

In this example, the Test stage depends on the successful completion of the Build stage, and the Deploy stage depends on the Test stage.

What Are Jobs?

Jobs are units of work executed by an agent. A job consists of a series of steps, which can include scripts, tasks, or other pipeline actions.

Key Characteristics of Jobs

  • Atomic Units:
    Jobs are the smallest schedule units within a pipeline.

  • Parallelism:
    Multiple jobs can run concurrently in a single stage.

  • Agent Assignment:
    Each job is assigned to an agent for execution.

  • Dependencies:
    Jobs within a stage can depend on each other, but this dependency is defined at the job level.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
jobs:
- job: BuildJob
  steps:
  - task: UseDotNet@2
    inputs:
      packageType: sdk
      version: '6.x'
  - script: dotnet build

- job: TestJob
  dependsOn: BuildJob
  steps:
  - script: dotnet test

- job: DeployJob
  dependsOn: TestJob
  steps:
  - script: echo "Deploying to Production"

In this example, TestJob depends on BuildJob, and DeployJob depends on TestJob. All jobs are defined within a single stage.

When to Use Stages and Jobs

Stages: Use stages when you need to:

Represent different environments (Dev, QA, Prod).

Add approval gates between parts of the pipeline.

Manage high-level dependencies and logical grouping.

Jobs: Use jobs when you need to

Run tasks in parallel to reduce execution time.

Define detailed workflows within a stage.

Share artifacts or variables between steps in the same job.

Best Practices

  • Use Clear Names:
    Name your stages and jobs descriptively to make the pipeline easy to understand.

  • Leverage Dependencies:
    Define dependsOn carefully to control execution flow.

  • Implement Approvals:
    Use stage-level approvals for production deployments to ensure compliance.

  • Use Templates:
    Modularize reusable jobs or stages using YAML templates.

  • Monitor Parallelism:
    Optimize job parallelism to balance speed and resource usage.

Differences Between Stages and Jobs

FeatureStagesJobs
PurposeHigh-level grouping of tasksGranular units of work
ScopeEncompasses multiple jobsContains steps (scripts/tasks)
Execution OrderSequential or parallelSequential or parallel within a stage
IsolationRepresents environments or milestonesLimited to the assigned agent
ApprovalsSupports manual intervention gatesNot applicable

Wrap Up

Understanding and effectively using stages and jobs in Azure DevOps pipelines allows you to design robust, scalable, and maintainable workflows. By logically separating responsibilities, you can improve clarity, enforce governance, and optimize execution time. Whether you’re deploying to production or running tests, mastering these concepts is key to successful CI/CD automation.

Share with your network!

Built with Hugo - Theme Stack designed by Jimmy