Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving cache on job failure in GitHub Actions

I am using the GitHub cache action, but I noticed that the no cache will be created if the job fails. From the docs:

If the job completes successfully, the action creates a new cache with the contents of the path directory.

A stripped down version of my workflow YAML file:

name: Build

on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Setup Node.js
        uses: actions/setup-node@master
        with:
          node-version: '10.x'

      - name: Get yarn cache path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Restore yarn cache
        uses: actions/cache@v1
        id: yarn-cache
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install yarn dependencies
        run: yarn install

      - name: Build
        run: yarn build

I noticed that if my Build step fails the cache post-step will be skipped unnecessarily, which causes the installed dependencies to not be cached. This requires subsequent runs to download dependencies again, slowing down the job.

Is there a way to always cache the dependencies, even when the build step fails?

like image 470
Duncan Luk Avatar asked Feb 04 '23 15:02

Duncan Luk


1 Answers

In the official version of the action, no it's not possible to cache dependencies if the build fails. See this line in the cache action's manifest:

runs:
  using: 'node12'
  main: 'dist/restore/index.js'
  post: 'dist/save/index.js'
  post-if: 'success()'

It will only run the post-step if the job succeeds. I don't know the original reasoning for this, but there are a few issues opened around this idea. In this issue a user forked the action to change the post-if to always(), which may be what you're after, assuming you're willing to run an unofficial action.

like image 59
Patrick Quirk Avatar answered Feb 07 '23 09:02

Patrick Quirk