GitHub Actions Caching: Dependencies & Build Artifacts
GitHub Actions Caching stores frequently downloaded files between workflow runs, dramatically reducing pipeline execution time for dependency installation and build outputs.
What You'll Learn
In this tutorial, you'll learn how to use the actions/cache action to cache npm, pip, Maven, and Docker dependencies, how to upload and download build artifacts, and how to set cache keys for optimal hit rates.
Why It Matters
Installing dependencies from scratch on every workflow run is slow and wasteful. A Node.js project with 500 dependencies takes 2-3 minutes to npm ci on every push. Caching cuts that to seconds. Artifacts let you share build outputs between jobs without rebuilding.
Real-World Use
Durga Antivirus Pro's CI pipeline caches Rust crate dependencies (~/.cargo and target/), Rust toolchain installers, and Docker image layers. A full build that originally took 25 minutes now completes in 8 minutes with Caching enabled.
Caching Dependencies
Use actions/cache to save and restore dependency directories:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci
- run: npm test
Cache Key Strategy
The key determines when a cache is invalidated:
# Restore cache if possible, rebuild if lockfile changed
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
Language-Specific Caching
Python (pip)
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
Java (Maven)
- uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
Uploading and Downloading Artifacts
Share build outputs between jobs or download them later:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: mkdir dist && echo "built" > dist/output.txt
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
- run: cat dist/output.txt
Practice Questions
1. What does actions/cache do?
It saves specified directories after a job and restores them on subsequent runs using a cache key.
2. How do you invalidate a cache? Change the cache key. When the key does not match any existing cache, a new cache is created.
3. What is the difference between hashFiles and a static key?
hashFiles generates a hash from file content, so the cache invalidates when dependencies change. A static key is never invalidated.
4. When should you use artifacts instead of Caching? Artifacts are for sharing build outputs between jobs or preserving them after the workflow finishes. Caching is for speeding up repeated operations like dependency installation.
5. Challenge: Create a workflow that caches npm dependencies, builds a project, uploads the build directory as an artifact, and downloads it in a deploy job.
Mini Project: Optimized Build Pipeline
Set up a workflow for a Python project. Cache pip dependencies, install packages, run tests, build a wheel distribution, upload the wheel as an artifact, and download it in a separate job that runs integration tests. Measure the time difference with and without Caching.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro