# Faster Pipelines

Course: [CI/CD](https://www.devtutorials.ai/learn/cicd) · Section: Continuous Integration
Source: https://www.devtutorials.ai/learn/cicd/continuous-integration/faster-pipelines

> Take a pipeline from about 11 minutes to under 5 with the same checks: cache packages, run checks as parallel jobs, test several versions with a matrix and cancel stale runs.

## Faster Pipelines

A slow pipeline hurts the whole team.

- People wait 10 minutes for each check.
- They push less often, in bigger chunks.
- Bigger chunks are harder to review and break more.

In this lesson you take a pipeline from about **11 minutes to under 5**, with the same checks.

**Simple lesson:** a fast pipeline gets run often. A slow one gets skipped.

### A Real-World Example

Think about making tea for ten guests.

- **Slow way:** boil water, then make tea, then wash cups, one after another.
- **Faster:** wash cups while the water boils.
- **Fastest:** keep milk and sugar ready from yesterday.

Now map it:

- **Washing cups while water boils** = parallel jobs.
- **Milk and sugar ready from yesterday** = cache.
- **Ten guests, two kinds of tea** = matrix.

**Simple lesson:** do things at the same time, and keep what you can reuse.

## Where the Time Goes

Typical times in this lab:

| Step | Time |
|---|---|
| Install packages (no cache) | 4m 00s |
| Install packages (from cache) | 0m 25s |
| Lint | 1m 00s |
| Type check | 1m 10s |
| Build | 1m 30s |
| Tests | 3m 00s |

- Install is the slowest step, and it does the same work every run.
- Lint, type check and tests do not need each other.

**Simple lesson:** first fix the step that repeats the same work. Then run independent steps side by side.

## Cache

**Cache** = files saved from one run and restored in the next.

- The **key** decides when to reuse. It is made from your lock file.
- Same lock file = same key = cache hit.
- Change a package = new key = one slow run, then fast again.

| Platform | React (npm) | ASP.NET Core (NuGet) |
|---|---|---|
| GitHub Actions | `cache: npm` on setup-node | `cache: true` on setup-dotnet |
| Azure Pipelines | `Cache@2` task | `Cache@2` task |

**Real tools vs this lab:** setup-dotnet's cache needs a `packages.lock.json` file in your repo. Turn it on with `RestorePackagesWithLockFile` in your project. In this lab, the lock file is already there.

**Simple lesson:** cache what you download. Key it on the lock file.

## Parallel Jobs

- Jobs with no `needs` or `dependsOn` start at the same time.
- Each gets its own runner.
- The run takes as long as the **slowest** job, not the sum.

**Critical path** = the longest chain of work. It sets the total time.

- After the run, look at the timeline.
- The bars should start together.
- The longest bar is your critical path.

Parallel jobs each install packages. Without a cache, every job pays the 4-minute install. That is why you need both a cache and parallel jobs.

**Simple lesson:** parallel jobs cut waiting. The slowest job decides the time.

## Matrix

**Matrix** = run the same job several times with different settings.

- Test on Node 20 and Node 22 together.
- Or .NET 8 and .NET 10.
- Each copy runs on its own runner, at the same time.

- GitHub stops the other copies when one fails. This is called **fail-fast**.
- Set `fail-fast: false` to see every result.

**Simple lesson:** a matrix checks every version you support, without waiting longer.

## Cancel Old Runs

- You push. Then you see a typo and push again. And again.
- Three full runs start. Only the last one matters.
- Cancel the old ones and save runner time.

| Platform | How |
|---|---|
| GitHub Actions | `concurrency:` with `cancel-in-progress: true` |
| Azure Pipelines | `autoCancel: true` under `pr:` (on by default for pull requests) |

Do not cancel in-progress runs on deploy jobs to production. A half-finished deploy is worse than a slow one. Use cancelling for checks, not for releases.

**Simple lesson:** only the newest push matters. Cancel the rest.

1. Open two runs of your starter repo: one with a cold cache and one after it.
2. Compare the time of the install step.
3. Push two commits quickly to the same branch.

**What you should see:** the second install is faster, and the first run is cancelled when the second one starts (with `concurrency`).

## Quick Check

## Cheat Sheet

GitHub Actions:

```yaml
concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node-version: [20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm
      - run: npm ci
      - run: npm test
```

Azure Pipelines:

```yaml
pr:
  autoCancel: true
  branches:
    include: ['*']

jobs:
  - job: test
    strategy:
      matrix:
        dotnet8:
          dotnetVersion: '8.0.x'
        dotnet10:
          dotnetVersion: '10.0.x'
    steps:
      - task: UseDotNet@2
        inputs:
          version: $(dotnetVersion)
      - task: Cache@2
        inputs:
          key: 'nuget | "$(Agent.OS)" | **/packages.lock.json'
          path: $(NUGET_PACKAGES)
      - script: dotnet restore
      - script: dotnet test --no-restore
```

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Cache key is a fixed text | One cache is enough | Key it on the lock file | Old packages come back after an upgrade |
| Parallel jobs, no cache | Parallel is always faster | Each job installs again | Little or no time saved |
| Dropping checks to go faster | Speed matters most | Same checks, smarter layout | Bugs reach main |
| Cancelling production deploys | Newer is always better | Let a release finish | Half-deployed production |
| One job for many versions | Test the newest only | A matrix tests each one | Users on the older version break |

## What to Remember

- Install repeats the same work: cache it.
- Independent checks: run them in parallel.
- The slowest job sets the time.
- Matrix: one job, many versions.
- Cancel old check runs, never half-done releases.

## Exercises

Next: **Artifacts and Versions**
