Skip to content

Commit

Permalink
Add inputs.cache-save-always (#34)
Browse files Browse the repository at this point in the history
* Add `inputs.cache-save-always`

Adds an input that borrows the `save-always` input from [v4 of `actions/cache`][0], which will run the post step to save the cache even if a failure occurs. Using the added `cache-save-always` input saves the cache when a build fails, which will speed up subsequent runs which restore the partial build. This comes at the cost of requiring a build when there is an exact `key` match, since the cache from a failed build will have the same `key` as a completed build.

[0]: https://github.com/actions/cache/tree/main#v4

* Documentation

* Evaluate input as string

* Use `-partial` key to no-op after full cache-hit

Saves the set of build artifacts after a build failure with a `-partial` suffix on the cache key. A full cache-hit will therefore only occur when restoring the cache saved after a successful build, allowing for the subsequent `stack build` and cache-save steps to be skipped.

* Set suffix using conditionally run step

Status check functions can only be used in `if` conditionals, so the use of a cache key suffix needs to be determined in a conditionally-run step.

* Revert "Set suffix using conditionally run step"

This reverts commit dd40c85.

* Revert "Use `-partial` key to no-op after full cache-hit"

This reverts commit 629a6a9.

* Extend conditional in build/save-cache steps

Checks that the workflow has not been cancelled (i.e., is succeeding or failed) and that `cache-save-always: true` when building and saving the cache. This extends the existing conditional on the `cache-hit` outputs, making the previously-implied `success()` check explicit.

When `cache-save-always: false`, the default behavior is unchanged for the action: steps either no-op if a cache-hit occurred, or build and save otherwise.

When `cache-save-always: true`, a building and saving the cache occurs on success or failure.

* Update input documentation
  • Loading branch information
stackptr authored Jan 23, 2024
1 parent ac0dd8d commit 178359c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ need to use a separate `stack-cache-action` step any more.
like, but teams often use `v{N}` and bump it to `v{N+1}` when/if they need to
explicitly bust caches. The default is empty.

- `cache-save-always`: save artifacts to the cache even if the build fails.
This may speed up builds in subsequent runs at the expense of slightly-longer
builds when a full cache-hit occurs. (Since `@v4.2.0`)

## Outputs

`compiler` (e.g. `ghc-9.2.5`) and `compiler-version` (e.g. `9.2.5`) are set from
Expand Down
19 changes: 15 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ inputs:
cache-prefix:
required: true
default: ""
cache-save-always:
description: "Save the dependencies and build cache even if a build fails"
default: false
outputs:
compiler:
description: "compiler.actual value from stack query"
Expand Down Expand Up @@ -207,7 +210,9 @@ runs:
${{ inputs.cache-prefix }}${{ runner.os }}-stack-deps-${{ steps.stack-query.outputs.compiler }}-
- name: Dependencies
if: steps.restore-deps.outputs.cache-hit != 'true'
if: |
(success() && steps.restore-deps.outputs.cache-hit != 'true') ||
(!cancelled() && inputs.cache-save-always == 'true')
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
Expand All @@ -220,7 +225,9 @@ runs:
${{ inputs.stack-arguments }}
- name: Save dependencies cache
if: steps.restore-deps.outputs.cache-hit != 'true'
if: |
(success() && steps.restore-deps.outputs.cache-hit != 'true') ||
(!cancelled() && inputs.cache-save-always == 'true')
uses: actions/cache/save@v3
with:
path: |
Expand All @@ -240,7 +247,9 @@ runs:
${{ inputs.cache-prefix }}${{ runner.os }}-stack-build-${{ steps.setup.outputs.snapshot-hash }}-
- name: Build
if: steps.restore-build.outputs.cache-hit != 'true'
if: |
(success() && steps.restore-build.outputs.cache-hit != 'true') ||
(!cancelled() && inputs.cache-save-always == 'true')
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
Expand All @@ -251,7 +260,9 @@ runs:
${{ inputs.stack-arguments }}
- name: Save build cache
if: steps.restore-build.outputs.cache-hit != 'true'
if: |
(success() && steps.restore-build.outputs.cache-hit != 'true') ||
(!cancelled() && inputs.cache-save-always == 'true')
uses: actions/cache/save@v3
with:
path: ${{ steps.setup.outputs.stack-works }}
Expand Down

0 comments on commit 178359c

Please sign in to comment.