Files
cache/save/README.md
T

117 lines
3.8 KiB
Markdown
Raw Normal View History

2022-12-21 19:38:44 +05:30
# Save action
2023-01-31 05:31:15 -05:00
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.
2022-12-21 19:38:44 +05:30
2023-01-31 05:31:15 -05:00
## Documentation
2022-12-21 19:38:44 +05:30
2023-01-31 05:31:15 -05:00
### Inputs
2022-12-21 19:38:44 +05:30
2023-01-31 05:31:15 -05:00
* `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 cache. See [`@actions/glob`](https://github.com/actions/toolkit/tree/main/packages/glob) for supported patterns.
* `upload-chunk-size` - The chunk size used to split up large files during upload, in bytes
### Outputs
2022-12-21 19:38:44 +05:30
This action has no outputs.
## Use cases
### Only save cache
2023-01-31 05:31:15 -05:00
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.
2022-12-21 19:38:44 +05:30
```yaml
steps:
2024-01-26 14:08:22 -08:00
- uses: actions/checkout@v4
2022-12-21 19:38:44 +05:30
- name: Install Dependencies
run: /install.sh
2023-02-03 09:53:09 +05:30
- name: Build artifacts
2022-12-21 19:38:44 +05:30
run: /build.sh
2024-01-26 14:08:22 -08:00
- uses: actions/cache/save@v4
2022-12-21 19:38:44 +05:30
id: cache
with:
path: path/to/dependencies
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
```
### Re-evaluate cache key while saving
2023-01-31 05:31:15 -05:00
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.
2022-12-21 19:38:44 +05:30
2023-01-31 05:31:15 -05:00
Let's say we have a restore step that computes a key at runtime.
#### Restore a cache
2022-12-21 19:38:44 +05:30
```yaml
2024-01-26 14:08:22 -08:00
uses: actions/cache/restore@v4
2022-12-21 19:38:44 +05:30
id: restore-cache
with:
key: cache-${{ hashFiles('**/lockfiles') }}
```
2023-01-31 05:31:15 -05:00
#### Case 1 - Where a user would want to reuse the key as it is
2022-12-21 19:38:44 +05:30
```yaml
2024-01-26 14:08:22 -08:00
uses: actions/cache/save@v4
2022-12-21 19:38:44 +05:30
with:
key: ${{ steps.restore-cache.outputs.cache-primary-key }}
2022-12-21 19:38:44 +05:30
```
2023-01-31 05:31:15 -05:00
#### Case 2 - Where the user would want to re-evaluate the key
2022-12-21 19:38:44 +05:30
```yaml
2024-01-26 14:08:22 -08:00
uses: actions/cache/save@v4
2022-12-21 19:38:44 +05:30
with:
key: npm-cache-${{hashfiles(package-lock.json)}}
```
### Always save cache
2024-10-03 14:37:55 -04:00
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 `actions/cache/save` action to save the cache by using an [`always()`](https://docs.github.com/actions/writing-workflows/choosing-what-your-workflow-does/expressions#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 `actions/cache/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 `cache-hit` output from a restore step should be checked.
The `cache-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.
2022-12-21 19:38:44 +05:30
Here's an example where we imagine we're calculating a lot of prime numbers and want to cache them:
2022-12-21 19:38:44 +05:30
```yaml
name: Always Caching Prime Numbers
2024-10-03 14:37:55 -04:00
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Restore cached Prime Numbers
id: cache-prime-numbers-restore
2024-10-03 14:37:55 -04:00
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-prime-numbers
2024-10-03 14:37:55 -04:00
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'
2024-10-03 14:37:55 -04:00
uses: actions/cache/save@v4
with:
key: ${{ steps.cache-prime-numbers-restore.outputs.cache-primary-key }}
2024-10-03 14:37:55 -04:00
path: |
path/to/dependencies
some/other/dependencies
2022-12-21 19:38:44 +05:30
```