Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions not finding cache

I have a workflow which caches the output of:

  • composer install using a hash of composer.lock
  • npm ci using a hash of package-lock.json
  • npm run prod using a hash of public/**/*

so they can be reused on subsequent runs.

The problem is, only the composer cache is ever hit - the two npm caches always result in a message in the CI output saying Cache not found for input keys.

Taking the npm ci code as an example, the cache setup and the subsequent usage are done by these two steps:

- name: Cache node packages
  id: cache-npm-packages
  uses: actions/cache@v2
  env:
    cache-name: cache-node-packages
  with:
    # npm cache files are stored in `~/.npm` on Linux/macOS
    path: ~/.npm
    key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }}

- if: ${{ steps.cache-npm-packages.outputs.cache-hit != 'true' }}
  run: npm ci

But the CI output says:

Cache not found for input keys: Linux-build-cache-node-packages-4370b0e1b9396b576e8db83e048ce2efe36557c8c4bae8492253ef706fc8a4bc

Despite being in the list of cache entries:

Image showing list of cache entries

The screenshot also shows that the cache of npm run prod is repeatedly being generated and never reused.

What am I doing wrong?

like image 787
Adam Hopkinson Avatar asked Apr 25 '26 03:04

Adam Hopkinson


1 Answers

I had this same issue, the problem is that a branch 'A' can only retrieve the cache of the 'main' branch or if it is a child branch.

enter image description here

All your cache is being created by pull requests, as you can see ref/pull/foo/merge

Caches stored by pull request can only be accessed on re-runs on the same branch.

enter image description here

To solve your problem, you should run the workflow once in the main branch, so any other action will be able to access it.

like image 105
JvSchulz Avatar answered Apr 29 '26 00:04

JvSchulz