From 178359c42adbc961a7531d956f55e12b360f38af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=BF=20corey?= Date: Tue, 23 Jan 2024 11:18:24 -0800 Subject: [PATCH] Add `inputs.cache-save-always` (#34) * 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 dd40c852997979683503b1c648321aab6b528e6e. * Revert "Use `-partial` key to no-op after full cache-hit" This reverts commit 629a6a9212809cfd89aa68a9ac4aef7d91934998. * 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 --- README.md | 4 ++++ action.yml | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c18fc73..129f11c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index 76dbccf..eafe7a5 100644 --- a/action.yml +++ b/action.yml @@ -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" @@ -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: | @@ -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: | @@ -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: | @@ -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 }}