mirror of https://github.com/actions/cache.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
5.4 KiB
Groff
136 lines
5.4 KiB
Groff
2 years ago
|
# Restore action
|
||
|
|
||
2 years ago
|
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.
|
||
2 years ago
|
|
||
2 years ago
|
## Documentation
|
||
2 years ago
|
|
||
2 years ago
|
### Inputs
|
||
|
|
||
|
* `key` - An explicit key for a cache entry. See [creating a cache key](../README.md#creating-a-cache-key).
|
||
|
* `path` - A list of files, directories, and wildcard patterns to restore. See [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob) for supported patterns.
|
||
2 years ago
|
* `restore-keys` - An ordered list of prefix-matched keys to use for restoring stale cache if no cache hit occurred for key.
|
||
2 years ago
|
* `fail-on-cache-miss` - Fail the workflow if cache entry is not found. Default: `false`
|
||
2 years ago
|
* `lookup-only` - If true, only checks if cache entry exists and skips download. Default: `false`
|
||
2 years ago
|
|
||
2 years ago
|
### Outputs
|
||
2 years ago
|
|
||
2 years ago
|
* `cache-hit` - A boolean value to indicate an exact match was found for the key.
|
||
2 years ago
|
* `cache-primary-key` - Cache primary key passed in the input to use in subsequent steps of the workflow.
|
||
2 years ago
|
* `cache-matched-key` - Key of the cache that was restored, it could either be the primary key on cache-hit or a partial/complete match of one of the restore keys.
|
||
2 years ago
|
|
||
|
> **Note**
|
||
|
`cache-hit` will be set to `true` only when 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`.
|
||
|
|
||
|
### Environment Variables
|
||
2 years ago
|
|
||
2 years ago
|
* `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](https://github.com/actions/cache/blob/main/tips-and-workarounds.md#cache-segment-restore-timeout)
|
||
2 years ago
|
|
||
|
## 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
|
||
|
|
||
2 years ago
|
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.
|
||
2 years ago
|
|
||
|
```yaml
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- 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
|
||
|
```
|
||
|
|
||
2 years ago
|
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.
|
||
2 years ago
|
|
||
|
### Save intermediate private build artifacts
|
||
|
|
||
2 years ago
|
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.
|
||
2 years ago
|
|
||
|
#### Step 1 - Build the parent module and save it
|
||
2 years ago
|
|
||
2 years ago
|
```yaml
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- 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
|
||
2 years ago
|
|
||
2 years ago
|
```yaml
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- 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
|
||
|
|
||
2 years ago
|
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!
|
||
2 years ago
|
|
||
|
```yaml
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- uses: actions/cache/restore@v3
|
||
|
id: cache
|
||
|
with:
|
||
|
path: path/to/dependencies
|
||
|
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
||
2 years ago
|
fail-on-cache-miss: true
|
||
2 years ago
|
|
||
|
- name: Build
|
||
|
run: /build.sh
|
||
|
```
|
||
|
|
||
|
## Tips
|
||
|
|
||
2 years ago
|
### Reusing primary key and restored key in the save action
|
||
2 years ago
|
|
||
2 years ago
|
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).
|
||
2 years ago
|
|
||
2 years ago
|
### Using restore action outputs to make save action behave just like the cache action
|
||
2 years ago
|
|
||
|
The outputs `cache-primary-key` and `cache-matched-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.
|
||
|
|
||
2 years ago
|
### Ensuring proper restores and save happen across the actions
|
||
2 years ago
|
|
||
|
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](https://github.com/actions/cache#creating-a-cache-key) and [versioning](https://github.com/actions/cache#cache-version) here.
|