Skip to content

Commit

Permalink
Merge pull request #21 from Leafwing-Studios/20-save-if
Browse files Browse the repository at this point in the history
Add `save-if` option for conditional cache saving
  • Loading branch information
alice-i-cecile authored Jul 20, 2024
2 parents 80f9ec4 + b46a3eb commit 6c2a159
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ jobs:
## Inputs
| Name | Description | Type | Default |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------------------- |
| Name | Description | Type | Default |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------ |
| `cache-group` | The group of the cache, defaults to a unique identifier for the workflow job. If you want two jobs to share the same cache, give them the same group name. | `string` | `${{ hashFiles(env.workflow_path)-${{ github.job }}-${{ strategy.job-index }}` |
| `cargo-home` | The location of the Cargo cache files. If you specify the `CARGO_HOME` env variable for your commands, you need to set it here too. This must NOT end with the trailing slash of the directory. | `string` | `~/.cargo` |
| `cargo-target-dir` | Location of where to place all generated artifacts, relative to the current working directory. If you specify the `CARGO_TARGET_DIR` env variable for your commands, you need to set it here too. This must NOT end with the trailing slash of the directory. | `string` | `target` |
| `save-if` | A condition which determines whether the cache should be saved. Otherwise, it's only restored | `boolean` | `true` |
| `save-always` | Run the post step to save the cache even if another step before fails. | `boolean` | `true` |

## Outputs
Expand Down
39 changes: 29 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ inputs:
Run the post step to save the cache even if another step before fails.
Defaults to `true`.
required: false
default: true
default: "true"
save-if:
description: |
A condition to determine whether the new cache should be saved.
If it evaluates to `false`, the cache is only loaded.
required: false
default: "true"
outputs:
cache-hit:
description: |
Expand Down Expand Up @@ -103,10 +109,12 @@ runs:
echo 'cache-group=${{ hashFiles(env.workflow_path) }}-${{ github.job }}-${{ strategy.job-index }}' >> "${GITHUB_OUTPUT}"
fi
# If the cache should also be saved, we use the cache action
# See <https://github.com/actions/cache>.
- name: Create cache
- name: Restore and save cache
id: cache
uses: actions/cache@v4
if: ${{ inputs.save-if == 'true' }}
with:
save-always: ${{ inputs.save-always }}
path: |
Expand All @@ -115,15 +123,26 @@ runs:
${{ inputs.cargo-home }}/registry/cache/
${{ inputs.cargo-home }}/git/db/
${{ inputs.cargo-target-dir }}/
# Never share a cache between different values of the following:
# - `runner.os` (build platform)
# - `cargo --version` (build toolchain)
# Restore from a fallback cache whenever one of the following changes:
# - `cache-group` (workflow job, or user-defined)
# - `Cargo.toml` file (build configs)
# - `Cargo.lock` file (build dependencies)
key: ${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
# Restore caches from the same group.
restore-keys: |
${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-
${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-
${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-
# Otherwise, we only restore the cache
# See <https://github.com/actions/cache/tree/main/restore>.
- name: Restore cache
id: cache-restore
uses: actions/cache/restore@v4
if: ${{ inputs.save-if != 'true' }}
with:
path: |
${{ inputs.cargo-home }}/bin/
${{ inputs.cargo-home }}/registry/index/
${{ inputs.cargo-home }}/registry/cache/
${{ inputs.cargo-home }}/git/db/
${{ inputs.cargo-target-dir }}/
key: ${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-${{ hashFiles('**/Cargo.toml') }}-
${{ runner.os }}-${{ steps.cargo-version.outputs.cargo-version }}-${{ steps.cache-group.outputs.cache-group }}-
Expand Down

0 comments on commit 6c2a159

Please sign in to comment.