cache: Configurable save cache on failure

Currently this cache action will only save caches if all tests succeed. In many cases this is desirable behavior. However I have some projects with long build times and flakey test suites. It would be very helpful if I could configure the cache to be saved regardless of the test suite success or failure.

I have created a fork of this project action to set the post-if to always().

Is it possible to make the cache policy configurable? Or to pass post-if as an argument from the cache configuration?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 172
  • Comments: 40 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Seems like it would make a lot of sense in some circumstances.

Say I have following steps:

  • install dependencies (success)
  • check code style (fail)
  • post cache (skip)

It would make sense if post cache could happen conditionally on success of dependencies install, code style step is irrelevant to it.

I made a fork of this repository that gives you full control over when the cache is saved. This allows you to both set your own if: always() (or success or failure or what you want) as well as run the save action where/when you want. Docs here: https://github.com/MartijnHols/actions-cache

For the OP it sounds like you run install and tests in a single job. If you place the save action after the step that generates the things you want to cache, you can achieve this without always. For example:

name: Build app

on: push

jobs:
  install:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - name: Restore "node_modules" from cache
      id: cache
      uses: martijnhols/actions-cache/restore@v3
      with:
        path: node_modules
        key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock', 'patches') }}
        restore-keys: ${{ runner.os }}-node_modules

    - name: Install dependencies
      if: steps.cache.outputs.cache-hit != 'true'
      run: yarn install

    - name: Save "node_modules" to cache
      if: steps.cache.outputs.cache-hit != 'true'
      uses: martijnhols/actions-cache/save@v3
      with:
        path: node_modules
        key: ${{ steps.cache.outputs.primary-key }}

    - name: Run flaky tests
      run: yarn test

If you want your cache to be saved regardless of a previous failure you can change the if: steps.cache.outputs.cache-hit != 'true' line into if: always() && steps.cache.outputs.cache-hit != 'true'.

Next fork with latest upstream changes (including the new 5GB cache limit): https://github.com/marketplace/actions/always-upload-cache

Hey everyone 👋🏽

Two new actions actions/cache/restore and actions/cache/save are now available with tag v3 to everyone for use. These can now be used to achieve granular control on the restore and save steps in the cache action.

Do try them and give your feedback. We hope these new actions will take care of your use cases. 🙇🏽

Closing this issue now as we believe the new actions will take care of the same, feel free to reopen it if need be. 😄

I also desperately need this. For my use cases (grading of student assignments), it is expected that at least some of the tests will fail most of the time, so having a completely successful run is rare. This means caching is basically useless, as the cache will never actually be persisted.

@gerbal I’ve tried your fork, but it also doesn’t seem to work. The post-action simply never runs (it stays a grey square in the log UI), same as with the original cache action.

I made https://github.com/mxxk/gh-actions-cache-always into a reusable action, which patches the original actions/cache to change the post-if: 'success()' predicate to post-if: 'success() || failure()'.

.github/actions/cache-always/action.yml:

Show file contents

name: 'Cache Always'
description: 'Cache artifacts like dependencies and build outputs to improve workflow execution time'

inputs:
  path:
    description: 'A list of files, directories, and wildcard patterns to cache and restore'
    required: true
  key:
    description: 'An explicit key for restoring and saving the cache'
    required: true
  restore-keys:
    description: 'An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note `cache-hit` returns false in this case.'
    required: false
  upload-chunk-size:
    description: 'The chunk size used to split up large files during upload, in bytes'
    required: false

outputs:
  cache-hit:
    description: 'A boolean value to indicate an exact match was found for the primary key'
    value: ${{ steps.cache.outputs.cache-hit }}

runs:
  using: 'composite'
  steps:
    # Instead of running `actions/cache@v3` directly, check it out locally.
    - name: Checkout actions/cache@v3
      uses: actions/checkout@v3
      with:
        repository: actions/cache
        ref: v3
        path: ./.github/.tmp/cache-always/actions/cache

    - name: Patch actions/cache@v3 to make it cache data also when the job fails
      run: |
        sed -i -e 's/post-if:.*$/post-if: "success() || failure()"/' ./.github/.tmp/cache-always/actions/cache/action.yml
      shell: bash

    - name: Cache
      id: cache
      uses: ./.github/.tmp/cache-always/actions/cache
      with:
        path: ${{ inputs.path }}
        key: ${{ inputs.key }}
        restore-keys: ${{ inputs.restore-keys }}
        upload-chunk-size: ${{ inputs.upload-chunk-size }}

You can use it by saving the above action into its recommended location and writing:

uses: ./.github/actions/cache-always

instead of

uses: actions/cache@v3

e.g.

- name: Setup pnpm cache
  uses: ./.github/actions/cache-always
  with:
    path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
    key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
    restore-keys: |
      ${{ runner.os }}-pnpm-store-

Edits: 2022-11-11: run on success() || failure(), to prevent caching on cancelled()

An alternative workaround, which does not require maintaining a forked version of actions/cache@v3, is to check out the action repo manually, modify action.yml, and run the modified action from its local path:

- name: Checkout actions/cache@v3
  uses: actions/checkout@v3
  with:
    repository: actions/cache
    ref: v3
    path: .tmp/actions/cache
- name: Make actions/cache@v3 run always, not only when job succeeds
  # Tweak `action.yml` of `actions/cache@v3` to remove its `post-if`
  # condition, making it default to `post-if: always()`.
  run: |
    sed -i -e '/ post-if: /d' .tmp/actions/cache/action.yml
- name: Cache data
  id: cache
  uses: ./.tmp/actions/cache
  with:
    ...

You can see a minimal example of a full workflow in https://github.com/mxxk/gh-actions-cache-always. The modified actions/cache@v3 command still saved the cache even though the job https://github.com/mxxk/gh-actions-cache-always/runs/5847143256 failed.

(Of course, this is only a hack, since the sed command which finds and deletes the post-if: line from action.yml is quite janky.)

Hey all, 👋🏽

We have created a discussion with a proposal that we feel will solve this problem, do let us know your feedback, thanks 😊

@pat-s, could you please fix deprecation warnings in your fork:

The `save-state` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands
The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

Thanks.

Yeah. If you want, you could use my fork and just change the “success()” to “always()”.

https://github.com/actions/cache/pull/498#issuecomment-753804797

It lets you control the behavior of the post action with an environment variable. So you could “always” run the post action and then have an environment variable that is set to true or false to control whether or not to update the cache. There are likely dozens of solutions similar to mine.

The authors of actions/cache ought to look at what the common forks of this repo are trying to do and incorporate the features.

Any progress on this one? I have the same problem as mentioned in https://github.com/actions/cache/issues/92#issuecomment-562204290.

Would this feature be officially supported?

I’ve just updated my fork to v3.0.1: https://github.com/marketplace/actions/always-upload-cache

FYI: such fork already exists: https://github.com/pat-s/always-upload-cache

For anyone interested, you are welcome to try out my advanced cache action built on top of this repo. Simply specify your cache targets in a config file, and finishes restoration, build, and save as many caches as you want in one step:

steps:
- uses: actions/checkout@v2
- uses: ktmud/cached-dependencies@v1
  with:
    run: |
      cache-restore npm
      npm install
      cache-save npm

      cache-restore pip
      pip install -r requirements.txt
      cache-save pip

https://github.com/ktmud/cached-dependencies#speficy-when-to-restore-and-save

We have the same use case @trent-boyd for this: failing / flaky end-to-end tests are really hard to debug due to having to re-run build processes: by allowing to cache builds even on failure would speed up this considerably.

This would be extremely useful for my team. We use Next.js. The process goes something like this.

  1. Restore the Next build cache
  2. Run the Next build (hopefully with cache)
  3. Run Cypress tests (which can be flakey)
  4. Save the Next build cache.

It would be great to save the Next cache even if the Cypress tests fail. There’s a high likelihood that the Cypress tests fail due to flakiness, but sometimes we just need to update the Cypress tests. That change doesn’t invalidate the build cache, so it would be nice to have that speedier build on the second run.

Hmm, I understood the requirement. Thanks @LorenzoBettini. I’ll take a look at this.

+1

We’re running a test suite with Hypothesis, a property based testing tool. The database should be saved (cached) on failed runs, so failed tests are replayed on subsequent runs.