Skip to content

Latest commit

 

History

History
139 lines (102 loc) · 18.2 KB

README.md

File metadata and controls

139 lines (102 loc) · 18.2 KB

Save action

The save action saves a cache. It works similarly to the cache action except that it doesn't first do a restore. This action provides granular ability to save a cache without having to restore it, or to do a save at any stage of the workflow job -- not only in post phase.

Configuration

Inputs

name description required default
primary-key
  • When a non-empty string, the action uses this key for saving a cache.
  • Otherwise, the action fails.
true ""
nix
  • Can have an effect only when the action runs on a Linux or a macOS runner.
  • When true, the action can do Nix-specific things.
  • Otherwise, the action doesn't do them.
false true
save
  • When true, the action can save a cache with the primary-key.
  • Otherwise, the action can't save a cache.
false true
paths
  • When nix: true, the action uses ["/nix", "~/.cache/nix", "~root/.cache/nix"] as default paths, as suggested here.
  • Otherwise, the action uses an empty list as default paths.
  • When a newline-separated non-empty list of non-empty path patterns (see @actions/glob for supported patterns), the action appends it to default paths and uses the resulting list for saving caches.
  • Otherwise, the action uses default paths for saving caches.
false ""
paths-macos
  • Overrides paths.
  • Can have an effect only when the action runs on a macOS runner.
false ""
paths-linux
  • Overrides paths.
  • Can have an effect only when the action runs on a Linux runner.
false ""
backend

Choose an implementation of the cache package.

false actions
gc-max-store-size
  • Can have an effect only when nix: true, save: true.
  • The input has no effect if "primary-key" hit occurs when starting to save the new cache.
  • When a number, the action collects garbage (via nix store gc --max ...) until the Nix store size (in bytes) is at most this number just before the action tries to save a new cache.
  • Otherwise, this input has no effect.
false ""
gc-max-store-size-macos
  • Overrides gc-max-store-size.
  • Can have an effect only when the action runs on a macOS runner.
false ""
gc-max-store-size-linux
  • Overrides gc-max-store-size.
  • Can have an effect only when the action runs on a Linux runner.
false ""
purge
  • When true, the action purges (possibly zero) caches.
  • Otherwise, this input has no effect.
false false
purge-primary-key
  • Can have an effect only when purge: true.
  • When always, the action always purges cache with the primary-key.
  • When never, the action never purges cache with the primary-key.
  • Otherwise, this input has no effect.
false ""
purge-prefixes
  • Can have an effect only when purge: true.
  • When a newline-separated non-empty list of non-empty cache key prefixes, the action selects for purging all caches whose keys match some of these prefixes and that are scoped to the current GITHUB_REF.
  • Otherwise, this input has no effect.
false ""
purge-last-accessed
  • Can have an effect only when purge: true.
  • When a non-negative number, the action purges selected caches that were last accessed more than this number of seconds ago relative to the start of the Post Restore phase.
  • Otherwise, this input has no effect.
false ""
purge-created
  • Can have an effect only when purge: true.
  • When a non-negative number, the action purges selected caches that were created more than this number of seconds ago relative to the start of the Post Restore phase.
  • Otherwise, this input has no effect.
false ""
upload-chunk-size
  • When a non-negative number, the action uses it as the chunk size (in bytes) to split up large files during upload.
  • Otherwise, the action uses the default value 33554432 (32MB).
false ""
token
  • The action uses it to communicate with GitHub API.
  • If you use a personal access token, it must have the repo scope (link).
false ${{ github.token }}

Outputs

This action has no outputs.

Use cases

Only save cache

If you are using separate jobs for generating common artifacts and sharing them across jobs, this action will take care of your cache saving needs.

steps:
  - uses: actions/checkout@v4

  - name: Install Dependencies
    run: /install.sh

  - name: Build artifacts
    run: /build.sh

  - uses: nix-community/cache-nix-action@v6
    id: cache
    with:
      primary-key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      paths: path/to/dependencies

Re-evaluate cache key while saving

With this save action, the key can now be re-evaluated while executing the action. This helps in cases where lockfiles are generated during the build.

Let's say we have a restore step that computes a key at runtime.

Restore a cache

uses: nix-community/cache-nix-action@v6
id: restore-cache
with:
  primary-key: cache-${{ hashFiles('**/lockfiles') }}

Case 1 - Where a user would want to reuse the key as it is

uses: nix-community/cache-nix-action@v6
with:
  primary-key: ${{ steps.restore-cache.outputs.primary-key }}

Case 2 - Where the user would want to re-evaluate the key

uses: nix-community/cache-nix-action@v6
with:
  primary-key: npm-cache-${{hashfiles(package-lock.json)}}

Always save cache

There are instances where some flaky test cases would fail the entire workflow and users would get frustrated because the builds would run for hours and the cache couldn't be saved as the workflow failed in between. For such use-cases, users now have the ability to use the nix-community/cache-nix-action/save action to save the cache by using an always() condition. This way the cache will always be saved if generated, or a warning will be generated that nothing is found on the cache path. Users can also use the if condition to only execute the nix-community/cache-nix-action/save action depending on the output of previous steps. This way they get more control of when to save the cache.

To avoid saving a cache that already exists, the hit-primary-key output from a restore step should be checked.

The primary-key output from the restore step should also be used to ensure the cache key does not change during the build if it's calculated based on file contents.

Here's an example where we imagine we're calculating a lot of prime numbers and want to cache them:

Note

The paths input in the cache-nix-action/restore and cache-nix-action/save must be the same.

name: Always Caching Prime Numbers

on: push

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Restore cached Prime Numbers
        id: cache-prime-numbers-restore
        uses: nix-community/cache-nix-action/restore@v6
        with:
          primary-key: ${{ runner.os }}-prime-numbers
          paths: |
            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.hit-primary-key != 'true'
        uses: nix-community/cache-nix-action/save@v6
        with:
          primary-key: ${{ steps.cache-prime-numbers-restore.outputs.primary-key }}
          paths: |
            path/to/dependencies
            some/other/dependencies