Caching & artifacts
Cut build times with caches and pass files between jobs.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.
In short
Caches reuse files like downloaded dependencies across workflow runs to save time. Artifacts are files a run produces, such as build output or reports, shared between jobs or downloaded later.
Explained simply
Every CI job starts on a clean machine, so it downloads all your dependencies again: hundreds of npm or pip packages, every time. A cache saves those downloads at the end of one run and restores them at the start of the next. Builds that took 4 minutes can drop to 1.
A cache needs a name, called the key. A good key changes only when your dependencies change, so it usually includes a fingerprint (hash) of your lockfile. Same lockfile, same key, reuse the cache. New lockfile, new key, build a fresh cache.
Artifacts are different. They are files your run produces and wants to keep or hand over: a built app, a test report, coverage results. One job uploads them, and a later job in the same run downloads them. People can also download them from the run page.
A simple way to remember: a cache is a speed-up for future runs and may disappear at any time. An artifact is a result of this run that you actually need. Never rely on a cache for correctness.
Press Play on a diagram to walk through it one step at a time.
- 1Compute key
OS plus a hash of the lockfile.
- 2Restore
Exact key hit, or nearest restore-keys match.
- 3Install deps
Fast when cached; only missing pieces download.
- 4Save on miss
New key saved at job end for next run.
- Back to step 1
- Speeds up later runs
- Dependencies, build caches
- Keyed and may be evicted
- Not for build outputs
- Output of this run
- Pass files between jobs
- Download from run page
- Kept for a retention period
- Step 1Exact key
npm-Linux- plus the lockfile hash: full hit
- Step 2restore-keys prefix
npm-Linux-: newest partial match
- Step 3Nothing found
Install from the network
- Step 4Job succeeds
Save under the exact key
In depth
What actions/cache does, exactly
At the start, actions/cache looks for an entry whose key matches exactly. If there is none, it tries each restore-keys prefix in order and takes the newest entry that starts with it. On an exact hit, the output cache-hit is 'true' and nothing is saved at the end. On a miss or a partial (prefix) hit, a post step at the end of the job saves the path under the new key, but only if the job succeeded.
Cache entries are immutable: you cannot overwrite a key, which is why the key must change when the content should. To save even when tests fail, or to save from a different step, split it into actions/cache/restore and actions/cache/save.
- key: deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- restore-keys: deps-${{ runner.os }}- gives a partial hit after a lockfile change
- Check steps.ID.outputs.cache-hit to skip an install on an exact hit
Scope, size limits and eviction
Caches are scoped to branches for safety. A run can restore caches created on its own branch or on the default branch. A pull_request run can also use caches from the PR's base branch. Runs on one feature branch cannot see caches made by another feature branch. So a cache saved on main warms up every new branch, which is why it pays to run the workflow on push to main too.
Each repository has a cache limit (10 GB by default). When it is full, GitHub evicts the least recently used entries, and entries not accessed for 7 days are removed anyway. gh cache list shows entries and their sizes, and gh cache delete removes one, handy when a broken cache keeps getting restored.
- Allowed
- Caches from this PR's own runs
- Caches from base branch main
- Caches from the default branch
- Not allowed
- Caches from feature/y
- Caches from other PRs
- Saved by this run
- Scoped to refs/pull/42/merge
Built-in caching in setup actions
Most setup actions can cache for you with one line. actions/setup-node takes cache: npm, yarn or pnpm. actions/setup-python takes cache: pip, pipenv or poetry. actions/setup-java takes cache: maven, gradle or sbt. actions/setup-go caches modules and the build cache by default. They build the key from your lockfile automatically; for a monorepo, set cache-dependency-path to the right lockfile or a glob.
Note what is cached: setup-node caches the package manager's download cache (like ~/.npm), not node_modules. That is deliberate. npm ci deletes node_modules before installing, so caching it would be wasted work, while the download cache still saves the network time. Use actions/cache directly for anything else, such as Playwright browsers or a compiler cache.
Artifacts between jobs and after the run
actions/upload-artifact@v4 zips the files and stores them with the run; actions/download-artifact@v4 fetches them in a later job. In v4, artifacts are immutable and names must be unique within a run, so in a matrix give each upload a name with the matrix values, then download with pattern: and merge-multiple: true to combine them.
Artifacts are kept for 90 days by default; set retention-days lower for big or temporary files to save storage. Zip does not keep file permissions, so an executable comes back without its x bit; tar it first if that matters. To download an artifact from a different run, pass run-id and a github-token with actions: read.
- build job to Artifact storage: upload-artifact name: dist1upload-artifact name: dist
- test job to Artifact storage: download-artifact name: dist2download-artifact name: dist
- 3Run end-to-end teststest job to test job: Run end-to-end tests
- deploy job to Artifact storage: download-artifact name: dist4download-artifact name: dist
- 5Deploy the exact tested filesdeploy job to deploy job: Deploy the exact tested files
Learn it in 3 short steps
About 40m each. Track them with a timer in your plan.
- 1Cache dependencies with actions/cache~40m
- 2Use built-in setup-* caching~40m
- 3Upload and download artifacts between jobs~40m
Key points
- Base cache keys on lockfile hashes so they change only when dependencies change.
- setup-node, setup-python and others have a built-in cache option.
- Use upload-artifact and download-artifact to hand files from build to deploy jobs.
- A cache miss must still work; caches are a speed-up, not a guarantee.
Key terms
Tap a card to flip it. 0/4 known
Examples
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-${{ hashFiles('requirements.txt') }}
restore-keys: pip-${{ runner.os }}-
- run: pip install -r requirements.txt && python -m build
- uses: actions/upload-artifact@v4
with: { name: dist, path: dist/ }Cache pip downloads, then upload the built package for a later job.
Cache npm and Playwright browsers
Your end-to-end tests spend 90 seconds downloading browsers. Step 1: let setup-node cache npm downloads. Step 2: read the installed Playwright version and use it in a cache key, because browsers only change when Playwright changes. Step 3: cache ~/.cache/ms-playwright with actions/cache. Step 4: install browsers only on a miss; on a hit, install just the system packages they need.
- uses: actions/setup-node@v4
with: { node-version: 22, cache: npm }
- run: npm ci
- id: pw
run: echo "version=$(npx playwright --version | awk '{print $2}')" >> "$GITHUB_OUTPUT"
- id: cache
uses: actions/cache@v4
with:
path: ~/.cache/ms-playwright
key: pw-${{ runner.os }}-${{ steps.pw.outputs.version }}
- if: steps.cache.outputs.cache-hit != 'true'
run: npx playwright install --with-deps
- if: steps.cache.outputs.cache-hit == 'true'
run: npx playwright install-deps
- run: npx playwright testCombine coverage reports from a matrix
Each of three matrix jobs writes a coverage file, and a final job should merge them. Step 1: each matrix job uploads with a unique name that includes the matrix value. Step 2: a report job needs the matrix job. Step 3: it downloads every artifact whose name matches coverage-* into one folder with merge-multiple. Step 4: it merges and publishes the combined report.
# in each matrix job
- uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.python }}
path: .coverage.*
include-hidden-files: true
retention-days: 5
report:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with: { pattern: coverage-*, merge-multiple: true }
- run: pip install coverage && coverage combine && coverage reportQuick reference
- Key on the lockfile: hashFiles('package-lock.json') or hashFiles('requirements.txt')
- Exact hit means cache-hit is 'true' and no save happens at the end
- Caches are immutable, scoped to branch, 10 GB per repo by default, removed after 7 unused days
- setup-node with cache: npm caches ~/.npm, not node_modules
- gh cache list and gh cache delete KEY manage caches from the terminal
- upload-artifact v4 names must be unique per run; use the matrix value in the name
- retention-days: 5 on uploads saves storage; the default is 90 days
- Artifacts drop file permissions; tar files first to keep executable bits
Common mistakes
- Caching build outputs instead of passing them as artifacts between jobs.
- A static cache key that never changes, so new dependencies are never saved.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Turn on built-in caching for a Python project that uses requirements.txt and check the logs for a cache hit on the second run.
- Exercise 2medium
Build once in a build job and run the tests in a separate job using the same built files.
- Exercise 3hard
Save a compiler cache even when tests fail, so the next run is still fast.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why is my cache never restored on a new branch?
- A new branch can only use caches from itself and the default branch. Run the workflow on push to main as well, so main has a warm cache to share.
- How long are caches and artifacts kept?
- Caches not used for 7 days are removed, and older ones can be evicted when the repo hits its size limit. Artifacts are kept for 90 days by default, or the retention-days you set.
- Can I share a cache between workflows?
- Yes. Caches are per repository and branch, not per workflow, so any workflow on an allowed branch can restore an entry by its key.
- How do I clear a broken cache?
- Delete it on the Actions tab under Caches, or run gh cache delete with its key. Changing a version prefix in the key, like v2-, also forces a fresh cache.