Lesson 2 of 5 Maintaining Pipelines

Optimizing Pipelines for Cost, Time, Performance and Reliability

This lesson works through the main optimization techniques for each goal.

Learning outcomes

After completing this lesson, you will be able to:

  • Configure dependency caching.
  • Distinguish caches from artifacts.
  • Identify ways of reducing cost.
  • Choose a technique for a symptom.

Measure first

A pipeline usually has one or two steps that dominate its duration, so effort elsewhere achieves little. Begin by finding the slowest steps from the logs or analytics, and record a baseline. Then decide which goal matters. Time, cost, performance and reliability sometimes conflict: running tests on four platforms in parallel improves time, and raises cost.

Reducing time

Caching

Caching reuses files between runs, usually downloaded dependencies. A cache key usually includes a hash of the lock file, so the cache changes only when dependencies do.

# Azure Pipelines
- task: Cache@2
  inputs:
    key: 'npm | "$(Agent.OS)" | package-lock.json'
    restoreKeys: |
      npm | "$(Agent.OS)"
    path: $(Pipeline.Workspace)/.npm
- script: npm ci --cache $(Pipeline.Workspace)/.npm

# GitHub Actions
- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
    restore-keys: ${{ runner.os }}-npm-
- run: npm ci

Cache and artifacts should not be confused. A cache speeds up later runs, and losing it does no harm. An artifact is the output of a run, and it passes results between jobs. Setup actions such as actions/setup-node also have a built-in cache option. GitHub provides 10 GB of cache per repository.

Parallelism

Splitting work across jobs shortens elapsed time, and test slicing across agents is often the largest gain. It does not reduce the total compute used, and it needs parallel capacity.

Doing less

Path filters skip builds unaffected by a change, Test Impact Analysis runs only related tests, and shallow clones reduce checkout time. Docker layer caching avoids rebuilding unchanged layers, so a Dockerfile should copy dependency manifests before source code. Incremental builds on self-hosted agents can reuse earlier work.

The following diagram shows where the time goes in an 18-minute Node.js pipeline run, and the cache step that shortens it.

Where an 18-minute Node.js run goes: 11 minutes of npm ci downloading packages that rarely change. A Cache@2 step keyed on package-lock.json restores them instead. A parallel job does not shorten one run, a self-hosted agent adds maintenance, and artifacts are for run outputs.
The bar is drawn to scale.
Spot the bug

A cache that never hits

Exam skills 3.6.2

Find the lines that stop this GitHub Actions cache from working.

Log in to try this activity Interactive activities are free with an account.

Reducing cost

  • Avoid unnecessary runs through branch and path filters, and batch: true on busy branches.
  • Cancel superseded runs with concurrency settings when a newer commit arrives.
  • Choose cheaper runners, because Linux minutes cost less than Windows, and much less than macOS.
  • Reduce storage by shortening retention for artifacts and logs.
  • Use self-hosted capacity for very heavy use, where it costs less than hosted minutes after including maintenance.
  • Delete temporary environments created by pipelines.
Select all that apply

Cutting pipeline cost

Exam skills 3.6.2

Pick the changes that reduce what a pipeline costs.

Log in to try this activity Interactive activities are free with an account.

Improving performance

If jobs are CPU-bound, a larger machine can cost less overall, since it finishes sooner. Managed DevOps Pools and GitHub larger runners offer such sizes. A custom image with tools preinstalled removes setup time. A private package feed close to the build reduces download time, and it reduces the risk of hitting public registry limits.

Improving reliability

  • Pin versions of tools and actions, as a floating version can change a build with no code change.
  • Use retries for transient failures with retryCountOnTaskFailure on a step, for known network-related steps.
  • Set timeouts with timeoutInMinutes or timeout-minutes, so a hung job does not hold an agent.
  • Reduce external dependencies with upstream feeds and private registries.
  • Fix flaky tests, which are the largest reliability problem in many pipelines.
  • Keep pipelines simple, since a pipeline that nobody understands will not be maintained.

The following table recaps how a symptom points to a technique.

SymptomFirst technique to try
Dependency restore takes minutes on every runCache keyed on the lock file
Many runs for documentation changesPath filters
Long test stageParallelize, or test slicing
Jobs wait in a queueMore parallel jobs or runners
Builds break with no code changePin tool and image versions
Bills are dominated by macOS minutesMove what you can to Linux
Match the pairs

Symptom to first technique

Exam skills 3.6.2

Match each pipeline symptom with the first technique to try.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing the optimization

A Node.js pipeline runs for 18 minutes on Microsoft-hosted agents, and 11 minutes of that is npm ci downloading packages. The lock file rarely changes. What should you do first?

Add a Cache@2 step keyed on package-lock.json, because the largest cost is a repeated download of unchanged content. Buying a parallel job would not shorten a single run. A self-hosted agent could hold a cache, but it adds maintenance, and an artifact is the wrong tool, as artifacts are for run outputs.

Check yourself

A file is needed by a later job in the same run. Should it be a cache or an artifact?

Show answer

An artifact. Caches speed up later runs, and losing one must do no harm.

Exam Tips

Remember for the exam

  • Find the slowest step before optimizing anything.
  • A cache speeds up later runs; an artifact passes output between jobs.
  • Key a cache on the lock file hash.
  • Path filters and cancelled superseded runs reduce cost.
  • Pin versions and set timeouts for reliability.