Skip to content

Latest commit

 

History

History
154 lines (107 loc) · 17.1 KB

README.md

File metadata and controls

154 lines (107 loc) · 17.1 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.

Documentation

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 with a matching key 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, the action tries to restore all caches whose keys match 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 ""
skip-restore-on-hit-primary-key
  • Can have an effect only when restore-prefixes-first-match has no effect.
  • When true, when there's a hit on the primary-key, the action doesn't restore caches.
  • Otherwise, this input has no effect.
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", "~/.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 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.

false ${{ github.token }}

Outputs

name description
primary-key
  • A string.
  • The primary-key.
hit
  • A boolean value.
  • true when hit-primary-key is true or hit-first-match is true.
  • false otherwise.
hit-primary-key
  • A boolean value.
  • true when there was a hit on the primary-key.
  • false otherwise.
hit-first-match
  • A boolean value.
  • 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@v4

  - uses: actions/cache/restore@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

  - name: Build
    run: /build.sh

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

Once the cache is restored, unlike actions/cache, 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 actions/cache or actions/cache/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@v4

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

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

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

steps:
  - uses: actions/checkout@v4

  - uses: actions/cache/restore@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

  - 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-cache-miss: true to exit a workflow on a cache miss. This way you can restrict your workflow to only build when there is a cache-hit.

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

steps:
  - uses: actions/checkout@v4

  - uses: actions/cache/restore@v3
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      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 key with both actions/cache/restore and actions/cache/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 cache-primary-key and cache-restored-key can be used to check if the restored cache is same as the given primary key. Alternatively, the cache-hit 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 key and path that were used by either actions/cache or actions/cache/save while saving the cache. Learn more about cache key naming and versioning here.