### Install Dependencies Source: https://github.com/actions/cache/blob/main/CONTRIBUTING.md Run this command to install project dependencies. ```bash npm install ``` -------------------------------- ### Basic Cache Key Configuration Source: https://github.com/actions/cache/blob/main/caching-strategies.md A fundamental example of configuring a cache key. Use this as a starting point for more complex key strategies. ```yaml jobs: build: runs-on: ubuntu-latest - uses: actions/cache@v5 with: key: ${{ some-metadata }}-cache ``` -------------------------------- ### Restore Cache Action Example Source: https://github.com/actions/cache/blob/main/restore/README.md Use this snippet to restore a cache for dependencies. It sets up the cache action with a path and a key. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache/restore@v5 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 - name: Build run: /build.sh - name: Publish package to public run: /publish.sh ``` -------------------------------- ### Create Cache Key with Date, Runner OS, and HashFiles Source: https://github.com/actions/cache/blob/main/README.md This example demonstrates creating a cache key that includes the current date, runner OS, and a hash of lock files. This provides a more granular cache that is invalidated daily. ```yaml # 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@v5 with: path: path/to/dependencies key: ${{ runner.os }}-${{ steps.get-date.outputs.date }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Skip Install Dependencies if Cache Hit Source: https://github.com/actions/cache/blob/main/README.md This snippet shows how to conditionally skip the dependency installation step if a cache hit occurs. It uses the `cache-hit` output from the actions/cache step to determine whether to run the installation script. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache@v5 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 ``` -------------------------------- ### Cache Pip Dependencies (Simple Example) Source: https://github.com/actions/cache/blob/main/examples.md Caches pip dependencies for Ubuntu. Uses '~/.cache/pip' as the path and the runner OS, 'pip', and requirements.txt hash for the cache key. Includes restore-keys for partial matches. ```yaml - uses: actions/cache@v5 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ``` -------------------------------- ### Restore and Save Cache Separately Source: https://github.com/actions/cache/blob/main/README.md This example demonstrates a caching strategy using separate restore and save actions. It first restores a cache using a primary key and then saves it using the primary key obtained from the restore step, allowing for more granular control over caching. ```yaml name: Caching Primes on: push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Restore cached Primes id: cache-primes-restore uses: actions/cache/restore@v5 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@v5 with: path: | path/to/dependencies some/other/dependencies key: ${{ steps.cache-primes-restore.outputs.cache-primary-key }} ``` -------------------------------- ### Cleanup Caches by Branch Source: https://github.com/actions/cache/blob/main/tips-and-workarounds.md This example demonstrates how to delete caches associated with a specific branch, which can be useful for managing storage quota, especially with frequent pull requests. It requires `actions:write` permission. ```yaml name: cleanup caches by a branch on: pull_request: types: - closed workflow_dispatch: jobs: cleanup: runs-on: ubuntu-latest permissions: # `actions:write` permission is required to delete caches # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id actions: write contents: read steps: - name: Cleanup run: | echo "Fetching list of cache key" cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id') ## Setting this to not fail the workflow while deleting cache keys. set +e echo "Deleting caches..." for cacheKey in $cacheKeysForPR do gh cache delete $cacheKey done echo "Done" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_REPO: ${{ github.repository }} BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge ``` -------------------------------- ### Update Dependency Cache Source: https://github.com/actions/cache/blob/main/CONTRIBUTING.md Locally install and run 'licensed cache' to update the dependency cache if you install or update production dependencies. This tool helps verify third-party dependencies. ```bash licensed cache ``` -------------------------------- ### Cache R renv package dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache renv package dependencies. This example sets the RENV_PATHS_ROOT environment variable and uses Rscript to get R and OS versions for the cache key. ```yaml - name: Set RENV_PATHS_ROOT shell: bash run: | echo "RENV_PATHS_ROOT=${{ runner.temp }}/renv" >> $GITHUB_ENV - name: Install and activate renv run: | install.packages("renv") renv::activate() shell: Rscript {0} - name: Get R and OS version id: get-version run: | cat("##[set-output name=os-version;]", sessionInfo()$running, "\n", sep = "") cat("##[set-output name=r-version;]", R.Version()$version.string, sep = "") shell: Rscript {0} - name: Restore Renv package cache uses: actions/cache@v5 with: path: ${{ env.RENV_PATHS_ROOT }} key: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{ inputs.cache-version }}-${{ hashFiles('renv.lock') }} restore-keys: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{inputs.cache-version }}- ``` -------------------------------- ### Always Save Cache with Conditional Logic Source: https://github.com/actions/cache/blob/main/save/README.md This example demonstrates how to always save a cache even if intermediate steps fail, using the `always()` expression. It also checks `cache-hit` to avoid saving an existing cache and uses the `cache-primary-key` to maintain key consistency. ```yaml name: Always Caching Prime Numbers on: push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Restore cached Prime Numbers id: cache-prime-numbers-restore uses: actions/cache/restore@v5 with: key: ${{ runner.os }}-prime-numbers path: | path/to/dependencies some/other/dependencies # Intermediate workflow steps - name: Always Save Prime Numbers id: cache-prime-numbers-save if: always() && steps.cache-prime-numbers-restore.outputs.cache-hit != 'true' uses: actions/cache/save@v5 with: key: ${{ steps.cache-prime-numbers-restore.outputs.cache-primary-key }} path: | path/to/dependencies some/other/dependencies ``` -------------------------------- ### Cache Swift Mint dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache Mint dependencies and binaries. This speeds up Swift projects using Mint by reusing previously installed tools. ```yaml env: MINT_PATH: .mint/lib MINT_LINK_PATH: .mint/bin steps: - uses: actions/cache@v5 with: path: .mint key: ${{ runner.os }}-mint-${{ hashFiles('**/Mintfile') }} restore-keys: | ${{ runner.os }}-mint- ``` -------------------------------- ### Cache Deno dependencies on Windows Source: https://github.com/actions/cache/blob/main/examples.md Cache Deno dependencies on Windows, including the Deno installation and local app data cache directories. The cache key is based on the OS and the hash of the deps.ts file. ```yaml - uses: actions/cache@v5 with: path: | ~\.deno ~\AppData\Local\deno key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }} ``` -------------------------------- ### Get npm cache directory using PWSH shell Source: https://github.com/actions/cache/blob/main/examples.md Retrieves the npm cache directory path using the PowerShell shell and sets it as an output. ```yaml - name: Get npm cache directory id: npm-cache-dir shell: pwsh run: echo "dir=$(npm config get cache)" >> ${env:GITHUB_OUTPUT} ``` -------------------------------- ### Cache Deno dependencies on Linux Source: https://github.com/actions/cache/blob/main/examples.md Cache Deno dependencies on Linux, including both the Deno installation and cache directories. The cache key is based on the OS and the hash of the deps.ts file. ```yaml - uses: actions/cache@v5 with: path: | ~/.deno ~/.cache/deno key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }} ``` -------------------------------- ### Cache Yarn 2 Dependencies Source: https://github.com/actions/cache/blob/main/examples.md Caches Yarn 2 dependencies. It retrieves the cache directory using 'yarn config get cacheFolder' and uses the runner OS, 'yarn', and yarn.lock hash for the cache key. Includes restore-keys for partial matches. ```yaml - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT - uses: actions/cache@v5 id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} restore-keys: | ${{ runner.os }}-yarn- ``` -------------------------------- ### Cache Esy Dependencies Source: https://github.com/actions/cache/blob/main/examples.md Caches esy dependencies using the '_export' directory. The cache key is based on the runner OS and a hash of 'esy.lock/index.json'. It includes steps to restore, install, and re-export dependencies, conditionally running the export if the cache was not hit. ```yaml - name: Restore Cache id: restore-cache uses: actions/cache@v5 with: path: _export key: ${{ runner.os }}-esy-${{ hashFiles('esy.lock/index.json') }} restore-keys: | ${{ runner.os }}-esy- - name: Esy install run: 'esy install' - name: Import Cache run: | esy import-dependencies _export rm -rf _export ...(Build job)... # Re-export dependencies if anything has changed or if it is the first time - name: Setting dependency cache run: | esy export-dependencies if: steps.restore-cache.outputs.cache-hit != 'true' ``` -------------------------------- ### Get npm cache directory using Bash shell Source: https://github.com/actions/cache/blob/main/examples.md Retrieves the npm cache directory path using the bash shell and sets it as an output. ```yaml - name: Get npm cache directory id: npm-cache-dir shell: bash run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} ``` -------------------------------- ### Cache Deno dependencies on macOS Source: https://github.com/actions/cache/blob/main/examples.md Cache Deno dependencies on macOS, including the Deno installation and cache directories in the user's Library folder. The cache key is based on the OS and the hash of the deps.ts file. ```yaml - uses: actions/cache@v5 with: path: | ~/.deno ~/Library/Caches/deno key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }} ``` -------------------------------- ### Cache npm dependencies using retrieved cache directory Source: https://github.com/actions/cache/blob/main/examples.md Caches npm dependencies using the path obtained from a previous step. This snippet demonstrates how to use the output of 'Get npm cache directory'. ```yaml - uses: actions/cache@v5 id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true' with: path: ${{ steps.npm-cache-dir.outputs.dir }} key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- ``` -------------------------------- ### Workflow demonstrating cache version differences Source: https://github.com/actions/cache/blob/main/README.md This YAML workflow illustrates how the same cache key can result in different cache versions due to the runner's operating system (Ubuntu vs. Windows) and different path configurations. Use this to understand cache versioning implications. ```yaml jobs: build-linux: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Cache Primes id: cache-primes uses: actions/cache@v5 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@v5 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@v6 - name: Cache Primes id: cache-primes uses: actions/cache@v5 with: path: prime-numbers key: primes - name: Generate Prime Numbers if: steps.cache-primes.outputs.cache-hit != 'true' run: ./generate-primes -d prime-numbers ``` -------------------------------- ### Cache Dependencies with Single Action Source: https://github.com/actions/cache/blob/main/README.md This snippet shows how to cache and restore dependencies using a single actions/cache action. It demonstrates setting up the cache path and key, and conditionally running subsequent steps based on whether the cache was hit. ```yaml name: Caching Primes on: push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Cache Primes id: cache-primes uses: actions/cache@v5 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 ``` -------------------------------- ### Cache Ruby Bundler dependencies with setup-ruby Source: https://github.com/actions/cache/blob/main/examples.md Recommended approach for caching Bundler gems using the ruby/setup-ruby action with the 'bundler-cache: true' option. ```yaml - uses: ruby/setup-ruby@v1 with: ruby-version: ... bundler-cache: true ``` -------------------------------- ### Create Cache Key with Runner OS and HashFiles Source: https://github.com/actions/cache/blob/main/README.md Use this snippet to create a cache key based on the runner's operating system and a hash of lock files. This ensures that the cache is specific to the OS and changes when dependencies are updated. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache with Restore Keys for Closest Match Source: https://github.com/actions/cache/blob/main/caching-strategies.md Utilize restore keys to download the most recently created cache that closely matches the primary key. This can save build time by fetching only newer dependencies. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: cache-npm-${{ hashFiles('**/lockfiles') }} restore-keys: | cache-npm- ``` -------------------------------- ### Run Tests Source: https://github.com/actions/cache/blob/main/CONTRIBUTING.md Execute this command to ensure all tests pass on your local machine before submitting changes. ```bash npm run test ``` -------------------------------- ### Save Intermediate Build Artifacts - Step 1 Source: https://github.com/actions/cache/blob/main/restore/README.md This step demonstrates building a parent module and saving its artifact to the cache using the save action. ```yaml steps: - uses: actions/checkout@v6 - name: Build run: /build-parent-module.sh - uses: actions/cache/save@v5 id: cache with: path: path/to/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache Go build and module dependencies on Windows Source: https://github.com/actions/cache/blob/main/examples.md Caches Go build artifacts and module dependencies for Windows runners. ```yaml - uses: actions/cache@v5 with: path: | ~\AppData\Local\go-build ~\go\pkg\mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- ``` -------------------------------- ### Cache Bun dependencies on Windows Source: https://github.com/actions/cache/blob/main/examples.md Cache Bun dependencies on Windows, using a different path for the cache. ```yaml - uses: actions/cache@v5 with: path: | ~\.bun key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }} ``` -------------------------------- ### Cache Go build and module dependencies on Linux Source: https://github.com/actions/cache/blob/main/examples.md Caches Go build artifacts and module dependencies for Linux runners. ```yaml - uses: actions/cache@v5 with: path: | ~/.cache/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- ``` -------------------------------- ### Fail Workflow on Cache Miss Source: https://github.com/actions/cache/blob/main/caching-strategies.md Configure your workflow to exit if a cache with an exact key is not found. This ensures builds only proceed when a cache hit occurs. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache/restore@v5 id: cache with: path: path/to/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} - name: Check cache hit if: steps.cache.outputs.cache-hit != 'true' run: exit 1 - name: Build run: /build.sh ``` -------------------------------- ### Cache Pip Dependencies (Matrix Strategy) Source: https://github.com/actions/cache/blob/main/examples.md Caches pip dependencies using a matrix strategy across different operating systems. The 'path' is defined within the matrix 'include' based on the OS, and the cache key uses the runner OS, 'pip', and requirements.txt hash. Includes restore-keys for partial matches. ```yaml jobs: build: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] include: - os: ubuntu-latest path: ~/.cache/pip - os: macos-latest path: ~/Library/Caches/pip - os: windows-latest path: ~\AppData\Local\pip\Cache steps: - uses: actions/cache@v5 with: path: ${{ matrix.path }} key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ``` -------------------------------- ### Exit Workflow on Cache Miss Source: https://github.com/actions/cache/blob/main/restore/README.md Configure the restore action to fail the workflow if a cache miss occurs by setting 'fail-on-cache-miss: true'. Ensure 'restore-keys' is empty to only rely on the primary key. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache/restore@v5 id: cache with: path: path/to/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} fail-on-cache-miss: true - name: Build run: /build.sh ``` -------------------------------- ### Cache Bun dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache Bun dependencies based on the operating system and the bun.lock file. ```yaml - uses: actions/cache@v5 with: path: | ~/.bun/install/cache key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }} ``` -------------------------------- ### Cache Go build and module dependencies on macOS Source: https://github.com/actions/cache/blob/main/examples.md Caches Go build artifacts and module dependencies for macOS runners. ```yaml - uses: actions/cache@v5 with: path: | ~/Library/Caches/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- ``` -------------------------------- ### Cache DUB dependencies on Windows Source: https://github.com/actions/cache/blob/main/examples.md Cache DUB dependencies on Windows, using the AppData\Local\dub directory. The cache key includes the OS and the hash of the dub.selections.json file. ```yaml - uses: actions/cache@v5 with: path: ~\AppData\Local\dub key: ${{ runner.os }}-dub-${{ hashFiles('**/dub.selections.json') }} restore-keys: | ${{ runner.os }}-dub- ``` -------------------------------- ### Create New Branch Source: https://github.com/actions/cache/blob/main/CONTRIBUTING.md Use this command to create a new branch for your feature or bug fix. ```bash git switch -c my-branch-name ``` -------------------------------- ### Cache Haskell Stack global directory on Windows Source: https://github.com/actions/cache/blob/main/examples.md Caches the global Stack configuration and binaries for Windows. ```yaml - uses: actions/cache@v5 name: Cache %APPDATA%\stack %LOCALAPPDATA%\Programs\stack with: path: | ~\AppData\Roaming\stack ~\AppData\Local\Programs\stack key: ${{ runner.os }}-stack-global-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }} restore-keys: | ${{ runner.os }}-stack-global- ``` -------------------------------- ### Restore Cache for Key Re-evaluation Source: https://github.com/actions/cache/blob/main/save/README.md This snippet demonstrates restoring a cache using a key that might be re-evaluated later. It's useful when lockfiles are generated during the build process. ```yaml uses: actions/cache/restore@v5 id: restore-cache with: key: cache-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache Pip Dependencies (Using pip cache dir) Source: https://github.com/actions/cache/blob/main/examples.md Caches pip dependencies by dynamically retrieving the cache directory using 'pip cache dir' (requires pip 20.1+). The cache key is based on the runner OS, 'pip', and requirements.txt hash. Includes restore-keys for partial matches. ```yaml - name: Get pip cache dir id: pip-cache shell: bash run: | echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT - name: pip cache uses: actions/cache@v5 with: path: ${{ steps.pip-cache.outputs.dir }} key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ``` -------------------------------- ### Build Parent Module and Save Cache Source: https://github.com/actions/cache/blob/main/caching-strategies.md Build a parent module and save its artifact as a cache. This prevents redundant builds of the parent module when it's needed for subsequent child module builds. ```yaml steps: - uses: actions/checkout@v6 - name: Build run: ./build-parent-module.sh - uses: actions/cache/save@v5 id: cache with: path: path/to/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache Gradle caches and wrapper Source: https://github.com/actions/cache/blob/main/examples.md Caches Gradle's dependency caches and wrapper files. Ensure Gradle daemons are not running when the workflow completes. ```yaml - uses: actions/cache@v5 with: path: | ~/.gradle/caches ~/.gradle/wrapper key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} restore-keys: | ${{ runner.os }}-gradle- ``` -------------------------------- ### Cache DUB dependencies on POSIX Source: https://github.com/actions/cache/blob/main/examples.md Cache DUB dependencies on POSIX systems, using the ~/.dub directory. The cache key includes the OS and the hash of the dub.selections.json file. ```yaml - uses: actions/cache@v5 with: path: ~/.dub key: ${{ runner.os }}-dub-${{ hashFiles('**/dub.selections.json') }} restore-keys: | ${{ runner.os }}-dub- ``` -------------------------------- ### Cache Pip Dependencies (Multiple OS) Source: https://github.com/actions/cache/blob/main/examples.md Caches pip dependencies across multiple operating systems (Linux, macOS, Windows) by defining separate cache steps for each OS. Each step uses the appropriate cache directory path for that OS and a cache key based on the runner OS, 'pip', and requirements.txt hash. Includes restore-keys for partial matches. ```yaml - uses: actions/cache@v5 if: startsWith(runner.os, 'Linux') with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- - uses: actions/cache@v5 if: startsWith(runner.os, 'macOS') with: path: ~/Library/Caches/pip key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- - uses: actions/cache@v5 if: startsWith(runner.os, 'Windows') with: path: ~\AppData\Local\pip\Cache key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ``` -------------------------------- ### Save Intermediate Build Artifacts - Step 2 Source: https://github.com/actions/cache/blob/main/restore/README.md This step restores a previously saved build artifact using the restore action, enabling its use in child module builds. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache/restore@v5 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 - name: Build run: /build-child-module.sh - name: Publish package to public run: /publish.sh ``` -------------------------------- ### Short-Lived Cache Using Run ID and Attempt Source: https://github.com/actions/cache/blob/main/caching-strategies.md Create a short-lived cache scoped to a specific workflow run ID and attempt. This is effective for temporary caching needs. ```yaml key: cache-${{ github.run_id }}-${{ github.run_attempt }} ``` -------------------------------- ### Cache Key Combining OS and Lockfile Hash Source: https://github.com/actions/cache/blob/main/caching-strategies.md Form a cache key by combining multiple factors, such as the operating system and the hash of lockfiles, to create a more specific cache scope. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache Python pipenv dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache virtual environment dependencies for pipenv. This uses the setup-python action to determine the Python version for the cache key. ```yaml - name: Set up Python # The actions/cache step below uses this id to get the exact python version id: setup-python uses: actions/setup-python@v6 ⋮ - uses: actions/cache@v5 with: path: ~/.local/share/virtualenvs key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-pipenv-${{ hashFiles('Pipfile.lock') }} ``` -------------------------------- ### Reuse Primary Key for Save Action Source: https://github.com/actions/cache/blob/main/caching-strategies.md Avoid re-computing the cache key in the save action by using the outputs from the restore action. This is useful for multi-module projects or when dependencies are restored before saving. ```yaml - uses: actions/cache/restore@v5 id: restore-cache with: path: | path/to/dependencies some/other/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} . . . - uses: actions/cache/save@v5 with: path: | path/to/dependencies some/other/dependencies key: ${{ steps.restore-cache.outputs.cache-primary-key }} ``` -------------------------------- ### Cache Bazel build artifacts on Linux Source: https://github.com/actions/cache/blob/main/examples.md Cache Bazel build artifacts for Linux. This speeds up Bazel builds by reusing previously downloaded and compiled components. ```yaml - name: Cache Bazel uses: actions/cache@v5 with: path: | ~/.cache/bazel key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel') }} restore-keys: | ${{ runner.os }}-bazel- - run: bazelisk test //... ``` -------------------------------- ### Cache NuGet packages by moving cache folder Source: https://github.com/actions/cache/blob/main/examples.md Cache NuGet packages by setting the NUGET_PACKAGES environment variable to a custom path within the workspace. This method is suitable when the default user profile cache location is not ideal. ```yaml env: NUGET_PACKAGES: ${{ github.workspace }} /.nuget/packages steps: - uses: actions/cache@v5 with: path: ${{ github.workspace }} /.nuget/packages key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget- ``` -------------------------------- ### Update Cache with Unique Key Source: https://github.com/actions/cache/blob/main/tips-and-workarounds.md Use a unique key for each run and `restore-keys` to update a cache. This creates a new cache on every run, consuming cache quota. ```yaml - name: update cache on every commit uses: actions/cache@v5 with: path: prime-numbers key: primes-${{ runner.os }}-${{ github.run_id }} # Can use time based key as well restore-keys: | primes-${{ runner.os }} ``` -------------------------------- ### Cache Erlang Rebar3 dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache Erlang Rebar3 project dependencies, including the ~/.cache/rebar3 and _build directories. The cache key includes the OS, OTP version, and the hash of rebar lock files. ```yaml - uses: actions/cache@v5 with: path: | ~/.cache/rebar3 _build key: ${{ runner.os }}-erlang-${{ env.OTP_VERSION }}-${{ hashFiles('**/*rebar.lock') }} restore-keys: | ${{ runner.os }}-erlang-${{ env.OTP_VERSION }}- ``` -------------------------------- ### Cache NuGet packages Source: https://github.com/actions/cache/blob/main/examples.md Cache NuGet packages using the packages.lock.json file for dependency locking. Restore keys are used to improve cache hit rate. ```yaml - uses: actions/cache@v5 with: path: ~/.nuget/packages key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget- ``` -------------------------------- ### Cache NuGet packages with exclusion Source: https://github.com/actions/cache/blob/main/examples.md Cache NuGet packages while excluding specific unwanted packages using an exclude pattern. This is useful for managing cache size. ```yaml - uses: actions/cache@v5 with: path: | ~/.nuget/packages !~/.nuget/packages/unwanted key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget- ``` -------------------------------- ### Short-Lived Cache Using Commit SHA Source: https://github.com/actions/cache/blob/main/caching-strategies.md Create a very specialized and short-lived cache using the commit SHA. This is useful for highly specific caching scenarios. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: cache-${{ github.sha }} ``` -------------------------------- ### Cache Composer Dependencies Source: https://github.com/actions/cache/blob/main/examples.md Caches Composer dependencies. It first retrieves the cache directory using 'composer config cache-files-dir' and then uses the runner OS, 'composer', and composer.lock hash for the cache key. Includes restore-keys for partial matches. ```yaml - name: Get Composer Cache Directory id: composer-cache run: | echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - uses: actions/cache@v5 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} restore-keys: | ${{ runner.os }}-composer- ``` -------------------------------- ### Cache Haskell Stack global directory on Linux/macOS Source: https://github.com/actions/cache/blob/main/examples.md Caches the global Stack configuration and binaries for Linux or macOS. ```yaml - uses: actions/cache@v5 name: Cache ~/.stack with: path: ~/.stack key: ${{ runner.os }}-stack-global-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }} restore-keys: | ${{ runner.os }}-stack-global- ``` -------------------------------- ### Cache Scoped by Operating System Source: https://github.com/actions/cache/blob/main/caching-strategies.md Separate caches for each operating system when running workflows with a matrix. This is useful when combined with hashfiles for OS-specific caches. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: ${{ runner.os }}-cache ``` -------------------------------- ### Cache Key Based on Lockfile Changes Source: https://github.com/actions/cache/blob/main/caching-strategies.md Use a hash of your lockfile to ensure the cache is updated only when dependencies change. This is a common strategy for dependency caching. ```yaml - uses: actions/cache@v5 with: path: | path/to/dependencies some/other/dependencies key: cache-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Only Save Cache Source: https://github.com/actions/cache/blob/main/save/README.md Use this snippet to save a cache of dependencies without performing a restore operation first. Ensure the 'path' and 'key' inputs are correctly configured. ```yaml steps: - uses: actions/checkout@v6 - name: Install Dependencies run: /install.sh - name: Build artifacts run: /build.sh - uses: actions/cache/save@v5 id: cache with: path: path/to/dependencies key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} ``` -------------------------------- ### Cache Clojure project dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache lein project dependencies by caching the ~/.m2/repository directory. The cache key is based on the OS and the hash of the project.clj file. ```yaml - name: Cache lein project dependencies uses: actions/cache@v5 with: path: ~/.m2/repository key: ${{ runner.os }}-clojure-${{ hashFiles('**/project.clj') }} restore-keys: | ${{ runner.os }}-clojure ``` -------------------------------- ### Cache Yarn Dependencies Source: https://github.com/actions/cache/blob/main/examples.md Caches Yarn dependencies. It first retrieves the cache directory path using 'yarn cache dir' and then uses the runner OS, 'yarn', and yarn.lock hash for the cache key. Includes restore-keys for partial matches. ```yaml - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT - uses: actions/cache@v5 id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} restore-keys: | ${{ runner.os }}-yarn- ``` -------------------------------- ### Save Cache with Re-evaluated Key (Case 1) Source: https://github.com/actions/cache/blob/main/save/README.md Reuses the primary cache key obtained from a previous restore step. This ensures consistency when the key is calculated based on file contents. ```yaml uses: actions/cache/save@v5 with: key: ${{ steps.restore-cache.outputs.cache-primary-key }} ``` -------------------------------- ### Cache Swift Carthage dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache Carthage build artifacts. This speeds up Swift projects using Carthage by reusing previously built frameworks. ```yaml - uses: actions/cache@v5 with: path: Carthage key: ${{ runner.os }}-carthage-${{ hashFiles('**/Cartfile.resolved') }} restore-keys: | ${{ runner.os }}-carthage- ``` -------------------------------- ### Cache Bazel build artifacts on macOS Source: https://github.com/actions/cache/blob/main/examples.md Cache Bazel build artifacts for macOS. This speeds up Bazel builds by reusing previously downloaded and compiled components. ```yaml - name: Cache Bazel uses: actions/cache@v5 with: path: | /private/var/tmp/_bazel_runner/ key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel') }} restore-keys: | ${{ runner.os }}-bazel- - run: bazelisk test //... ``` -------------------------------- ### Cache Elixir Mix dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache Elixir Mix project dependencies, including the 'deps' and '_build' directories. The cache key is based on the OS and the hash of the mix.lock file. ```yaml - uses: actions/cache@v5 with: path: | deps _build key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} restore-keys: | ${{ runner.os }}-mix- ``` -------------------------------- ### Save Cache with Re-evaluated Key (Case 2) Source: https://github.com/actions/cache/blob/main/save/README.md Saves a cache using a newly re-evaluated key, which is useful when the cache key needs to be dynamically generated based on specific files like 'package-lock.json'. ```yaml uses: actions/cache/save@v5 with: key: npm-cache-${{hashfiles(package-lock.json)}} ``` -------------------------------- ### Restore Built Artifact from Cache Source: https://github.com/actions/cache/blob/main/caching-strategies.md Restore a previously built artifact from the cache using the same key and path. This is the second step in reusing a parent module's artifact for child module builds. ```yaml steps: - uses: actions/checkout@v6 - uses: actions/cache/restore@v5 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 - name: Build run: ./build-child-module.sh - name: Publish package to public run: ./publish.sh ``` -------------------------------- ### Restore Lerna Dependencies Source: https://github.com/actions/cache/blob/main/examples.md Caches node_modules for Lerna projects. Uses runner OS and yarn.lock hash for the cache key. ```yaml - name: restore lerna uses: actions/cache@v5 with: path: '**/node_modules' key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }} ``` -------------------------------- ### Cache Haskell Stack work directory on Linux/macOS Source: https://github.com/actions/cache/blob/main/examples.md Caches the Stack build artifacts for Linux or macOS. ```yaml - uses: actions/cache@v5 name: Cache .stack-work with: path: .stack-work key: ${{ runner.os }}-stack-work-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }}-${{ hashFiles('**/*.hs') }} restore-keys: | ${{ runner.os }}-stack-work- ``` -------------------------------- ### Cache Haskell Cabal packages and store Source: https://github.com/actions/cache/blob/main/examples.md Caches Haskell Cabal package metadata and the store directory. This is recommended to avoid large cache sizes. ```yaml - name: Cache ~/.cabal/packages, ~/.cabal/store and dist-newstyle uses: actions/cache@v5 with: path: | ~/.cabal/packages ~/.cabal/store dist-newstyle key: ${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('**/*.cabal', '**/cabal.project', '**/cabal.project.freeze') }} restore-keys: ${{ runner.os }}-${{ matrix.ghc }}- ``` -------------------------------- ### Cache Rust Cargo build artifacts Source: https://github.com/actions/cache/blob/main/examples.md Cache Cargo dependencies, registry indexes, git databases, and build targets. This ensures faster Rust builds by reusing previously downloaded and compiled artifacts. ```yaml - uses: actions/cache@v5 with: path: | ~/.cargo/bin/ ~/.cargo/registry/index/ ~/.cargo/registry/cache/ ~/.cargo/git/db/ target/ key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} ``` -------------------------------- ### Cache Swift Package Manager build artifacts Source: https://github.com/actions/cache/blob/main/examples.md Cache Swift Package Manager build artifacts. This speeds up Swift projects using SPM by reusing previously built packages. ```yaml - uses: actions/cache@v5 with: path: .build key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }} restore-keys: | ${{ runner.os }}-spm- ``` -------------------------------- ### Cache local Maven repository Source: https://github.com/actions/cache/blob/main/examples.md Caches the local Maven repository to speed up subsequent builds. ```yaml - name: Cache local Maven repository uses: actions/cache@v5 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven- ``` -------------------------------- ### Cache Scala SBT dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache SBT dependencies, including Ivy and SBT's own cache directories. This speeds up Scala builds by reusing downloaded libraries and build configurations. ```yaml - name: Cache SBT uses: actions/cache@v5 with: path: | ~/.ivy2/cache ~/.sbt key: ${{ runner.os }}-sbt-${{ hashFiles('**/build.sbt') }} ``` -------------------------------- ### Cache Swift CocoaPods dependencies Source: https://github.com/actions/cache/blob/main/examples.md Cache CocoaPods dependencies. This speeds up Swift projects using CocoaPods by reusing previously downloaded pods. ```yaml - uses: actions/cache@v5 with: path: Pods key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }} restore-keys: | ${{ runner.os }}-pods- ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.