# Caching & Matrix Builds — CI/CD Fundamentals: Pipelines, Testing and Deployment

Source: https://www.geekswithgeeks.com/en/cicd/cicd-cache-matrix

> Speed up GitHub Actions with dependency caching and test across Node versions or operating systems with a build matrix.

## Cache dependencies

Caching the package manager's store avoids re-downloading dependencies on every run. `setup-node` can do it for you.

```yaml
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with:
      node-version: 22
      cache: npm          # restores ~/.npm keyed by package-lock.json
  - run: npm ci
  - run: npm test
```

## Build matrix

A matrix runs the same job across several combinations in parallel.

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

Key caches on lockfile hashes so they refresh when dependencies change. A matrix multiplies run time, so only include combinations you actually support.

Quick check

**Quiz:** What does a build matrix do?

- [x] Runs a job across several combinations in parallel
- [ ] Encrypts secrets
- [ ] Deploys to production
- [ ] Deletes caches

*Answer:* Runs a job across several combinations in parallel. Each matrix combination becomes its own job run.
