0Pricing
DevOps Bootcamp · Lesson

Caching Dependencies for Speed

Implement caching to significantly speed up your workflow execution by reusing generated dependencies and build outputs.

Caching Dependencies for Speed is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Speed Up Workflows with Caching

Ever notice your workflow re-downloading the same dependencies every time? It slows things down!

Caching in GitHub Actions lets you store and reuse files from previous runs. This dramatically speeds up subsequent workflow executions.

Meet the Cache Action

GitHub provides a special action called actions/cache@v3. This action is designed to save and restore files to make your workflows faster.

It's smart: it checks if the cache already exists before trying to restore, and saves it if it doesn't.

Cache Key and Path

Two crucial parameters for actions/cache are key and path:

  • key: A unique string that identifies your cache. If a cache with this key is found, it's restored.
  • path: The file paths or directories you want to cache. This tells the action *what* to save or restore.

The key often includes a hash of dependency files to ensure the cache is invalidated when dependencies change.

Caching Node.js Dependencies

Let's cache node_modules for a Node.js project. We'll use hashFiles to create a dynamic key based on package-lock.json.

Try running this simple workflow snippet:

name: Node.js CI with Cache

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Cache Node.js modules
      uses: actions/cache@v3
      with:
        path: node_modules
        key: npm-${{ hashFiles('package-lock.json') }}
        restore-keys: |
          npm-
    - name: Install dependencies
      run: npm ci
    - name: Run tests
      run: npm test

Fallback with Restore Keys

What if your exact key doesn't match a cache? That's where restore-keys come in handy!

restore-keys provide a list of fallback keys to search for. If the primary key (e.g., npm-${{ hashFiles(...) }}) misses, GitHub Actions tries to find a cache using the restore-keys in order.

In our example, npm- tries to find any cache starting with "npm-", which is useful if only minor dependency versions changed.

Caching Python Dependencies

Caching isn't just for Node.js! You can apply the same principle to other languages like Python. Here, we cache Python's virtual environment and pip cache.

The key here uses hashFiles('requirements.txt'), and the path points to the virtual environment and pip's cache directory.

name: Python CI with Cache

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'
    - name: Cache Python dependencies
      uses: actions/cache@v3
      with:
        path: |
          ~/.cache/pip
          .venv
        key: pip-${{ hashFiles('requirements.txt') }}
        restore-keys: |
          pip-
    - name: Install dependencies
      run: |
        python -m venv .venv
        .venv/bin/pip install -r requirements.txt
    - name: Run tests
      run: .venv/bin/python -m unittest discover

When to Invalidate Cache

A cache is only useful if it's fresh enough. If your dependencies change, your cache should too!

The cache automatically invalidates if your primary key (e.g., npm-${{ hashFiles('package-lock.json') }}) changes. If package-lock.json is updated, the hash changes, and a new cache is created.

You can also force a cache rebuild by manually changing the key (e.g., adding a version suffix like npm-v2-${{ hashFiles(...) }}).

Cache Scope and Limits

It's good to know how caching works behind the scenes:

  • Scope: Caches are scoped per repository and per branch. A cache on main branch won't be used on a feature branch, unless you explicitly configure lookup-path.
  • Size Limit: Each cache can be up to 10GB.
  • Retention: Caches are retained for 7 days after the last access. Old, unused caches are automatically removed.

Smart Caching Practices

To get the most out of caching:

  • Use hashFiles: Always link your cache key to dependency manifest files (e.g., package-lock.json, requirements.txt).
  • Be Specific with path: Only cache what's necessary (e.g., node_modules, ~/.cache/pip).
  • Use restore-keys: Provide fallbacks for better cache hit rates.
  • Avoid Caching Volatile Data: Don't cache files that change frequently or are very large and unique per run (e.g., build artifacts meant for deployment).

Cache Key Challenge

You have a Java project that uses Maven. Its dependencies are defined in pom.xml, and Maven typically stores downloaded JARs in ~/.m2/repository.

Which cache configuration best ensures that Maven dependencies are cached and correctly invalidated when pom.xml changes?

Recap: Cache for Speed!

Great job! You've learned how to implement caching in GitHub Actions to significantly speed up your CI/CD workflows.

  • The actions/cache action is your tool.
  • Use key and path to define what and how to cache.
  • hashFiles creates dynamic keys for smart invalidation.
  • restore-keys provide valuable fallbacks.

By intelligently caching dependencies, you save build time and computational resources, making your pipelines more efficient!

Frequently asked questions

Is the “Caching Dependencies for Speed” lesson free?

Yes — the full text of “Caching Dependencies for Speed” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Caching Dependencies for Speed”?

Implement caching to significantly speed up your workflow execution by reusing generated dependencies and build outputs. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Caching Dependencies for Speed” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Matrix Builds for Multiple Environments
  2. Caching Dependencies for Speed
  3. Reusable Workflows and Actions
  4. Conditional Execution and Job Dependencies
← Back to DevOps Bootcamp