Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Workflow Caching in GitHub Actions

Introduction

In this lesson, we will explore the concept of workflow caching in GitHub Actions, which helps speed up your workflows by storing and reusing dependencies or build outputs.

What is Caching?

Caching is a mechanism for storing data temporarily so that future requests for that data can be served faster. In the context of GitHub Actions, caching allows you to save the results of previous runs, reducing the time it takes to run workflows.

Note: Proper caching can significantly reduce build times, especially for large projects with many dependencies.

How Caching Works

GitHub Actions caching utilizes a key-value store to save and retrieve cached items. The cache is identified by a unique key, which you can define based on the dependencies or outputs you want to cache.

Cache Key

The cache key is a unique identifier for the cache. You can create a cache key using a combination of static strings and dynamic values, such as a hash of your dependencies file.

Cache Restore

When a workflow job runs, it first attempts to restore the cache using the provided key. If a matching cache is found, it will be used; otherwise, the job will proceed without the cache.

Cache Save

At the end of the job, you can save the cache using the cache save step. This will store the specified files or directories under the defined cache key.

Setting Up Caching

To set up caching in your GitHub Actions workflow, follow these steps:

  • Define a cache key based on your project dependencies.
  • Use the actions/cache action to restore the cache.
  • Run your build steps.
  • Use the actions/cache action again to save the cache.
  • Example Workflow

    Here's an example of a GitHub Actions workflow that sets up caching for Node.js dependencies:

    name: CI
    
    on:
      push:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
    
          - name: Cache Node.js modules
            uses: actions/cache@v2
            with:
              path: ~/.npm
              key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
              restore-keys: |
                ${{ runner.os }}-node-
    
          - name: Install dependencies
            run: npm install
    
          - name: Run tests
            run: npm test
    
          - name: Save Node.js modules cache
            uses: actions/cache@v2
            with:
              path: ~/.npm
              key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
            

    Best Practices

    To maximize the benefits of caching, consider the following best practices:

  • Use specific cache keys to avoid cache conflicts.
  • Regularly clean up old caches to free up storage.
  • Monitor cache hit rates to assess effectiveness.
  • Keep cache sizes manageable to avoid exceeding limits.
  • FAQ

    What is the maximum size for a cache?

    The maximum cache size for GitHub Actions is 5 GB per cache.

    How many caches can I create?

    You can create up to 100 caches per repository.

    Can I cache multiple directories?

    Yes, you can cache multiple directories by specifying them in a comma-separated list under the path parameter.