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.
name | description | required | default |
---|---|---|---|
primary-key |
|
true |
"" |
nix |
|
false |
true |
save |
|
false |
true |
paths |
|
false |
"" |
paths-macos |
|
false |
"" |
paths-linux |
|
false |
"" |
backend |
Choose an implementation of the
|
false |
actions |
gc-max-store-size |
|
false |
"" |
gc-max-store-size-macos |
|
false |
"" |
gc-max-store-size-linux |
|
false |
"" |
purge |
|
false |
false |
purge-primary-key |
|
false |
"" |
purge-prefixes |
|
false |
"" |
purge-last-accessed |
|
false |
"" |
purge-created |
|
false |
"" |
upload-chunk-size |
|
false |
"" |
token |
|
false |
${{ github.token }} |
This action has no outputs.
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
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.
uses: nix-community/cache-nix-action@v6
id: restore-cache
with:
primary-key: cache-${{ hashFiles('**/lockfiles') }}
uses: nix-community/cache-nix-action@v6
with:
primary-key: ${{ steps.restore-cache.outputs.primary-key }}
uses: nix-community/cache-nix-action@v6
with:
primary-key: npm-cache-${{hashfiles(package-lock.json)}}
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