Speed up a pipeline with caching

DevOps, Hands On20 minutesFreeSteps checked 23 November 2026By Cloud Hands On

In this task you'll stop downloading the same packages on every run.

Every GitHub-hosted runner starts empty, so each run downloads its packages again. A cache saves a folder at the end of a run and restores it at the start of the next, matched by a key. Make the key from a hash of your lock or requirements file: when the file changes, the key changes and the cache is rebuilt.

restore-keys gives a fallback: if there's no exact match, restore the newest cache whose key starts with a prefix, then top it up. Many setup actions (setup-python, setup-node, setup-java) can cache for you with one line. Caches are per branch, with pull requests able to read the default branch's cache, and unused ones are removed after seven days.

Example. Harbourline Design's site build spent two of its three minutes installing packages. Caching pip downloads keyed on requirements.txt cut the install to 15 seconds on most runs.

Before you start

You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 20 minutes.

Where to look

  1. Repository
  2. .github/workflows/pipeline.yml
  3. Actions tab
  4. Caches

Steps

  1. In the test job, set cache: pip on setup-python and cache-dependency-path: requirements-dev.txt (see Commands).
  2. Push and open the run. The setup step reports that no cache was found, and a post step saves one.
  3. Re-run the job. This time setup-python reports a cache hit and the install is quicker.
  4. Open Actions, Caches to see the saved cache and its key.
  5. Add a package to requirements-dev.txt and push: the key changes and a new cache is saved.That's why the key includes a hash of the file.

Commands

      - uses: actions/setup-python@v6
        with:
          python-version: '3.13'
          cache: pip
          cache-dependency-path: requirements-dev.txt

# Or the general cache action, for any folder
      - uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: pip-${{ runner.os }}-${{ hashFiles('requirements-dev.txt') }}
          restore-keys: pip-${{ runner.os }}-

gh cache list

Watch out for

Cost
Free. Each repository gets 10 GB of cache; the oldest caches are removed when it's full.
Security
Never cache folders holding secrets or credentials, and don't trust caches written by pull requests from forks for release builds.
Common mistakes
A key with no file hash (the cache never refreshes) or with the commit SHA (it never hits).

Clean up

Caches expire by themselves after seven days unused, or delete them from Actions, Caches.

Words to know

Cache key
The name a cache is saved and found by, usually including a file hash.
Cache hit
An exact key match: the folder is restored as saved.
restore-keys
Prefixes to try when there's no exact match.
hashFiles
A function that returns a hash of the files you name.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going