• Stars
    star
    4,511
  • Rank 9,459 (Top 0.2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cache dependencies and build outputs in GitHub Actions

Cache action

This action allows caching dependencies and build outputs to improve workflow execution time.

Two other actions are available in addition to the primary cache action:

Tests

Documentation

See "Caching dependencies to speed up workflows".

What's New

v3

  • Added support for caching in GHES 3.5+.
  • Fixed download issue for files > 2GB during restore.
  • Updated the minimum runner version support from node 12 -> node 16.
  • Fixed avoiding empty cache save when no files are available for caching.
  • Fixed tar creation error while trying to create tar with path as ~/ home folder on ubuntu-latest.
  • Fixed zstd failing on amazon linux 2.0 runners.
  • Fixed cache not working with github workspace directory or current directory.
  • Fixed the download stuck problem by introducing a timeout of 1 hour for cache downloads.
  • Fix zstd not working for windows on gnu tar in issues.
  • Allowing users to provide a custom timeout as input for aborting download of a cache segment using an environment variable SEGMENT_DOWNLOAD_TIMEOUT_MINS. Default is 10 minutes.
  • New actions are available for granular control over caches - restore and save.
  • Support cross-os caching as an opt-in feature. See Cross OS caching for more info.
  • Added option to fail job on cache miss. See Exit workflow on cache miss for more info.
  • Fix zstd not being used after zstd version upgrade to 1.5.4 on hosted runners
  • Added option to lookup cache without downloading it.
  • Reduced segment size to 128MB and segment timeout to 10 minutes to fail fast in case the cache download is stuck.

See the v2 README.md for older updates.

Usage

Pre-requisites

Create a workflow .yml file in your repository's .github/workflows directory. An example workflow is available below. For more information, see the GitHub Help Documentation for Creating a workflow file.

If you are using this inside a container, a POSIX-compliant tar needs to be included and accessible from the execution path.

If you are using a self-hosted Windows runner, GNU tar and zstd are required for Cross-OS caching to work. They are also recommended to be installed in general so the performance is on par with hosted Windows runners.

Inputs

  • key - An explicit key for a cache entry. See creating a cache key.
  • path - A list of files, directories, and wildcard patterns to cache and restore. See @actions/glob for supported patterns.
  • restore-keys - An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key.
  • enableCrossOsArchive - An optional boolean when enabled, allows Windows runners to save or restore caches that can be restored or saved respectively on other platforms. Default: false
  • fail-on-cache-miss - Fail the workflow if cache entry is not found. Default: false
  • lookup-only - If true, only checks if cache entry exists and skips download. Does not change save cache behavior. Default: false

Environment Variables

  • SEGMENT_DOWNLOAD_TIMEOUT_MINS - Segment download timeout (in minutes, default 10) to abort download of the segment if not completed in the defined number of minutes. Read more

Outputs

  • cache-hit - A boolean value to indicate an exact match was found for the key.

    Note cache-hit will only be set to true when a cache hit occurs for the exact key match. For a partial key match via restore-keys or a cache miss, it will be set to false.

See Skipping steps based on cache-hit for info on using this output

Cache scopes

The cache is scoped to the key, version, and branch. The default branch cache is available to other branches.

See Matching a cache key for more info.

Example cache workflow

Restoring and saving cache using a single action

name: Caching Primes

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Cache Primes
      id: cache-primes
      uses: actions/cache@v3
      with:
        path: prime-numbers
        key: ${{ runner.os }}-primes

    - name: Generate Prime Numbers
      if: steps.cache-primes.outputs.cache-hit != 'true'
      run: /generate-primes.sh -d prime-numbers

    - name: Use Prime Numbers
      run: /primes.sh -d prime-numbers

The cache action provides a cache-hit output which is set to true when the cache is restored using the primary key and false when the cache is restored using restore-keys or no cache is restored.

Using a combination of restore and save actions

name: Caching Primes

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Restore cached Primes
      id: cache-primes-restore
      uses: actions/cache/restore@v3
      with:
        path: |
          path/to/dependencies
          some/other/dependencies
        key: ${{ runner.os }}-primes
    .
    . //intermediate workflow steps
    .
    - name: Save Primes
      id: cache-primes-save
      uses: actions/cache/save@v3
      with:
        path: |
          path/to/dependencies
          some/other/dependencies
        key: ${{ steps.cache-primes-restore.outputs.cache-primary-key }}

Note You must use the cache or restore action in your workflow before you need to use the files that might be restored from the cache. If the provided key matches an existing cache, a new cache is not created and if the provided key doesn't match an existing cache, a new cache is automatically created provided the job completes successfully.

Caching Strategies

With the introduction of the restore and save actions, a lot of caching use cases can now be achieved. Please see the caching strategies document for understanding how you can use the actions strategically to achieve the desired goal.

Implementation Examples

Every programming language and framework has its own way of caching.

See Examples for a list of actions/cache implementations for use with:

Creating a cache key

A cache key can include any of the contexts, functions, literals, and operators supported by GitHub Actions.

For example, using the hashFiles function allows you to create a new cache when dependencies change.

  - uses: actions/cache@v3
    with:
      path: |
        path/to/dependencies
        some/other/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

Additionally, you can use arbitrary command output in a cache key, such as a date or software version:

  # http://man7.org/linux/man-pages/man1/date.1.html
  - name: Get Date
    id: get-date
    run: |
      echo "date=$(/bin/date -u "+%Y%m%d")" >> $GITHUB_OUTPUT
    shell: bash

  - uses: actions/cache@v3
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }}

See Using contexts to create cache keys

Cache Limits

A repository can have up to 10GB of caches. Once the 10GB limit is reached, older caches will be evicted based on when the cache was last accessed. Caches that are not accessed within the last week will also be evicted.

Skipping steps based on cache-hit

Using the cache-hit output, subsequent steps (such as install or build) can be skipped when a cache hit occurs on the key. It is recommended to install missing/updated dependencies in case of a partial key match when the key is dependent on the hash of the package file.

Example:

steps:
  - uses: actions/checkout@v3

  - uses: actions/cache@v3
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

  - name: Install Dependencies
    if: steps.cache.outputs.cache-hit != 'true'
    run: /install.sh

Note The id defined in actions/cache must match the id in the if statement (i.e. steps.[ID].outputs.cache-hit)

Cache Version

Cache version is a hash generated for a combination of compression tool used (Gzip, Zstd, etc. based on the runner OS) and the path of directories being cached. If two caches have different versions, they are identified as unique caches while matching. This, for example, means that a cache created on a windows-latest runner can't be restored on ubuntu-latest as cache Versions are different.

Pro tip: The list caches API can be used to get the version of a cache. This can be helpful to troubleshoot cache miss due to version.

Example The workflow will create 3 unique caches with same keys. Ubuntu and windows runners will use different compression technique and hence create two different caches. And `build-linux` will create two different caches as the `paths` are different.
jobs:
  build-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Cache Primes
        id: cache-primes
        uses: actions/cache@v3
        with:
          path: prime-numbers
          key: primes

      - name: Generate Prime Numbers
        if: steps.cache-primes.outputs.cache-hit != 'true'
        run: ./generate-primes.sh -d prime-numbers

      - name: Cache Numbers
        id: cache-numbers
        uses: actions/cache@v3
        with:
          path: numbers
          key: primes

      - name: Generate Numbers
        if: steps.cache-numbers.outputs.cache-hit != 'true'
        run: ./generate-primes.sh -d numbers

  build-windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v3

      - name: Cache Primes
        id: cache-primes
        uses: actions/cache@v3
        with:
          path: prime-numbers
          key: primes

      - name: Generate Prime Numbers
        if: steps.cache-primes.outputs.cache-hit != 'true'
        run: ./generate-primes -d prime-numbers

Known practices and workarounds

There are a number of community practices/workarounds to fulfill specific requirements. You may choose to use them if they suit your use case. Note these are not necessarily the only solution or even a recommended solution.

Windows environment variables

Please note that Windows environment variables (like %LocalAppData%) will NOT be expanded by this action. Instead, prefer using ~ in your paths which will expand to the HOME directory. For example, instead of %LocalAppData%, use ~\AppData\Local. For a list of supported default environment variables, see the Learn GitHub Actions: Variables page.

Contributing

We would love for you to contribute to actions/cache. Pull requests are welcome! Please see the CONTRIBUTING.md for more information.

License

The scripts and documentation in this project are released under the MIT License

More Repositories

1

runner-images

GitHub Actions runner images
PowerShell
9,683
star
2

starter-workflows

Accelerating new GitHub Actions workflows
TypeScript
8,687
star
3

toolkit

The GitHub ToolKit for developing GitHub Actions.
TypeScript
4,974
star
4

runner

The Runner for GitHub Actions 🚀
C#
4,825
star
5

checkout

Action for checking out a repo
TypeScript
4,634
star
6

actions-runner-controller

Kubernetes controller for GitHub Actions self-hosted runners
Go
4,595
star
7

github-script

Write workflows scripting the GitHub API in JavaScript
TypeScript
4,184
star
8

setup-node

Set up your GitHub Actions workflow with a specific version of node.js
TypeScript
3,895
star
9

upload-artifact

TypeScript
3,162
star
10

typescript-action

Create a TypeScript Action with tests, linting, workflow, publishing, and versioning
TypeScript
2,001
star
11

labeler

An action for automatically labelling pull requests
TypeScript
1,941
star
12

setup-python

Set up your GitHub Actions workflow with a specific version of Python
TypeScript
1,702
star
13

setup-java

Set up your GitHub Actions workflow with a specific version of Java
TypeScript
1,517
star
14

setup-go

Set up your GitHub Actions workflow with a specific version of Go
TypeScript
1,391
star
15

stale

Marks issues and pull requests that have not had recent interaction
TypeScript
1,345
star
16

download-artifact

TypeScript
1,338
star
17

create-release

An Action to create releases via the GitHub Release API
JavaScript
1,333
star
18

javascript-action

Create a JavaScript Action with tests, linting, workflow, publishing, and versioning
JavaScript
966
star
19

setup-dotnet

Set up your GitHub Actions workflow with a specific version of the .NET core sdk
TypeScript
932
star
20

upload-release-asset

An Action to upload a release asset via the GitHub Release API
JavaScript
660
star
21

first-interaction

An action for filtering pull requests and issues from first-time contributors
JavaScript
648
star
22

dependency-review-action

A GitHub Action for detecting vulnerable dependencies and invalid licenses in your PRs
TypeScript
598
star
23

deploy-pages

GitHub Action to publish artifacts to GitHub Pages for deployments
JavaScript
555
star
24

add-to-project

Automate adding issues and pull requests to GitHub projects
TypeScript
455
star
25

delete-package-versions

TypeScript
345
star
26

gh-actions-cache

A GitHub (gh) CLI extension to manage the GitHub Actions caches being used in a GitHub repository.
Go
257
star
27

example-services

Example workflows using service containers
JavaScript
248
star
28

hello-world-javascript-action

A template to demonstrate how to build a JavaScript action.
JavaScript
230
star
29

container-action

Shell
185
star
30

heroku

GitHub Action for interacting with Heroku
HCL
179
star
31

setup-ruby

Set up your GitHub Actions workflow with a specific version of Ruby
TypeScript
173
star
32

upload-pages-artifact

A composite action for packaging and uploading an artifact that can be deployed to GitHub Pages.
Shell
171
star
33

hello-world-docker-action

A template to demonstrate how to build a Docker action.
Shell
162
star
34

setup-elixir

Set up your GitHub Actions workflow with OTP and Elixir
JavaScript
154
star
35

python-versions

Python builds for Actions Runner Images
PowerShell
148
star
36

container-toolkit-action

Template repo for creating container actions using https://github.com/actions/toolkit/
TypeScript
115
star
37

github

Wraps actions-toolkit into an Action for common GitHub automations.
JavaScript
103
star
38

actions-sync

This tool allows GHES administrators to sync Actions to their instances
Go
93
star
39

configure-pages

An action to enable Pages and extract various metadata about a site. It can also be used to configure various static site generators we support as starter workflows.
JavaScript
91
star
40

create-github-app-token

GitHub Action for creating a GitHub App Installation Access Token
JavaScript
86
star
41

http-client

A lightweight HTTP client optimized for use with actions, TypeScript with generics and async await.
TypeScript
72
star
42

setup-haskell

Set up your GitHub Actions workflow with a specific version of Haskell (GHC and Cabal)
TypeScript
71
star
43

node-versions

Node builds for Actions Runner Images
PowerShell
69
star
44

languageservices

Language services for GitHub Actions workflows and expressions.
TypeScript
67
star
45

jekyll-build-pages

A simple GitHub Action for producing Jekyll build artifacts compatible with GitHub Pages.
HTML
66
star
46

importer-labs

GitHub Actions Importer helps you plan and automate the migration of Azure DevOps, Bamboo, CircleCI, GitLab, Jenkins, and Travis CI pipelines to GitHub Actions.
Ruby
65
star
47

runner-container-hooks

Runner Container Hooks for GitHub Actions
TypeScript
58
star
48

partner-runner-images

About GitHub Actions runner images provided by 3rd parties
54
star
49

go-dependency-submission

Calculates dependencies for a Go build-target and submits the list to the Dependency Submission API
TypeScript
53
star
50

importer-issue-ops

GitHub Actions Importer helps you plan and automate the migration of Azure DevOps, Bamboo, CircleCI, GitLab, Jenkins, and Travis CI pipelines to GitHub Actions.
Ruby
49
star
51

publish-action

TypeScript
39
star
52

go-versions

Go releases for Actions Runner Images
PowerShell
39
star
53

reusable-workflows

Reusable workflows for developing actions
JavaScript
38
star
54

.github

30
star
55

humans.txt

An Action to list out the humans who help feed and tend the robots of GitHub Actions.
JavaScript
29
star
56

versions-package-tools

Libs and tools used to build all *-version tools for GitHub Actions
PowerShell
20
star
57

publish-immutable-action

A GitHub Action used for publishing an Action to ghcr.io as an OCI container.
TypeScript
20
star
58

virtual-environments-packages

Code and scripts used to automate delivery of tool packages used in virtual-environments.
17
star
59

action-versions

Shell
17
star
60

boost-versions

Boost builds for Actions Virtual Environments
PowerShell
5
star
61

alpine_nodejs

Workflow for redistribution of Node.JS for actions/runner
Dockerfile
5
star
62

anno-test

1
star