Skip to content

Latest commit

 

History

History
154 lines (107 loc) · 15.7 KB

File metadata and controls

154 lines (107 loc) · 15.7 KB

Restore action

The restore action restores a cache. It works similarly to the cache action except that it doesn't have a post step to save the cache. This action provides granular ability to restore a cache without having to save it. It accepts the same set of inputs as the cache action.

Configuration

Inputs

name description required default
primary-key
  • When a non-empty string, the action uses this key for restoring a cache.
  • Otherwise, the action fails.
true ""
restore-prefixes-first-match
  • When a newline-separated non-empty list of non-empty key prefixes, when there's a miss on the primary-key, the action searches in this list for the first prefix for which there exists a cache whose key has this prefix, and the action tries to restore that cache.
  • Otherwise, this input has no effect.
false ""
restore-prefixes-all-matches
  • When a newline-separated non-empty list of non-empty key prefixes, when there's a miss on the primary-key, the action tries to restore all caches whose keys have these prefixes.
  • Tries caches across all refs to make use of caches created on the current, base, and default branches (see docs).
  • Otherwise, this input has no effect.
false ""
lookup-only
  • When true, when there's a hit on the primary-key, the action doesn't restore any cache.
  • Otherwise, the action can restore caches.
false false
fail-on
  • Input form: <key type>.<result>.
  • <key type> options: primary-key, first-match.
  • <result> options: miss, not-restored.
  • When the input satisfies the input form, when the event described in the input happens, the action fails.
  • Example:
    • Input: primary-key.not-restored.
    • Event: a cache could not be restored via the primary-key.
  • Otherwise, this input has no effect.
false ""
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"] as default paths.
  • 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 restoring caches.
  • Otherwise, the action uses default paths for restoring 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
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

name description
primary-key
  • A string.
  • The primary-key.
hit
  • A boolean string.
  • 'true' when hit-primary-key is true or hit-first-match is true.
  • 'false' otherwise.
hit-primary-key
  • A boolean string.
  • 'true' when there was a hit on the primary-key.
  • 'false' otherwise.
hit-first-match
  • A boolean string.
  • 'true' when there was a hit on a key matching restore-prefixes-first-match.
  • 'false' otherwise.
restored-key
  • A string.
  • The key of a cache restored via the primary-key or via the restore-prefixes-first-match.
  • An empty string otherwise.
restored-keys
  • A possibly empty array of strings (JSON).
  • Keys of restored caches.
  • Example: ["key1", "key2"].

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

Use cases

As this is a newly introduced action to give users more control in their workflows, below are some use cases where one can use this action.

Only restore cache

If you are using separate jobs to create and save your cache(s) to be reused by other jobs in a repository, this action will take care of your cache restoring needs.

steps:
  - uses: actions/checkout@v6

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

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

  - name: Build
    run: /build.sh

  - name: Publish package to public
    run: /publish.sh

Once the cache is restored, unlike nix-community/cache-nix-action, this action won't run a post step to do post-processing, and the rest of the workflow will run as usual.

Save intermediate private build artifacts

In case of multi-module projects, where the built artifact of one project needs to be reused in subsequent child modules, the need to rebuild the parent module again and again with every build can be eliminated. The nix-community/cache-nix-action or nix-community/cache-nix-action/save action can be used to build and save the parent module artifact once, and it can be restored multiple times while building the child modules.

Step 1 - Build the parent module and save it

steps:
  - uses: actions/checkout@v6

  - name: Build
    run: /build-parent-module.sh

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

Step 2 - Restore the built artifact from cache using the same key and path

steps:
  - uses: actions/checkout@v6

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

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

  - name: Build
    run: /build-child-module.sh

  - name: Publish package to public
    run: /publish.sh

Exit workflow on cache miss

You can use fail-on: miss to exit a workflow on a cache miss. This way you can restrict your workflow to only build when there is a hit-primary-key.

To fail if there is no cache hit for the primary key, leave restore-prefixes-first-match empty!

steps:
  - uses: actions/checkout@v6

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

  - name: Build
    run: /build.sh

Tips

Reusing primary key and restored key in the save action

Usually you may want to use the same primary-key with both actions/cache/restore and nix-community/cache-nix-action/save actions. To achieve this, use outputs from the restore action to reuse the same primary key (or the key of the cache that was restored).

Using restore action outputs to make save action behave just like the cache action

The outputs primary-key and restored-key can be used to check if the restored cache is same as the given primary key. Alternatively, the hit-primary-key output can also be used to check if the restored was a complete match or a partially restored cache.

Ensuring proper restores and save happen across the actions

It is very important to use the same primary-key and paths that were used by either nix-community/cache-nix-action or nix-community/cache-nix-action/save while saving the cache. Learn more about cache key naming and versioning here.