The Problem
I run a small team working on a growing monorepo. Every commit, some CI checks run on the entire codebase, from GitHub actio...
For further actions, you may consider blocking this person and/or reporting abuse
I think we can just use the
setup-node
action:The docs say that it uses the cache action underneath and caches the yarn dependencies. See github.com/actions/setup-node/blob...
it doesn't cache node_modules, only global cache data
I just want to add a little note to this. According to the cache action docs, caching
node_modules
is not recommended.I'm saying this because I'm trying to speed up our workflow too and I haven't solved this problem yet other than caching
npm
.I've seen people add the node version as part of the key to mitigate the node version not matching. If you do that and don't use npm ci you are probably safe.
On a second run I got a message:
Cache restored from key: node_modules-7895c53553e57f00e1fd69b577ee8f44d76199b1c231a85c936c5886d7416416
but the next step (yarn install) still takes the same time (almost 2 minutes) and looks like it install everything from scratch.
Why could this happen?
in the post is missing the if condition, like this
Yes in the docs it is also mention to do like this.
@mpocock1 Could you please update your post with this important step?
Great idea.
After implementing your second solution, I noticed the dependency installation was not being shortened.
I ran the CI a few times with no changes. The cache step was taking 0-1s and the proceeding yarn install step always took the same amount of time it used to.
Maybe I'm reaching the limits of cache space? Unlikely as my dependency installation time is much smaller than your 4 minutes.
However your first solution has worked for me, but as you mentioned the savings are minor.
Is there any special you also needed to do alongside solution #2 to make it work?
Thank you,
You all should refer to this example:
github.com/actions/cache/blob/main...
you're still getting a yarn install because you're never checking whether the cache hits. Honestly I don't know how the OP ever got this to work in the first place.
The whole point here is you need to create the cache, then see if it hits, and use whether or not it hits to determine whether or not you should take a given action.
Yes, it's possible you're reaching the end of your cached space. There is a final step at the end of each GH action where it saves the cache. Is this step passing?
Also, is your repo a monorepo or a single project repo?
Thank you.
Just to add that -- unlike the other responses -- caching the node_modules folder as illustrated seems sufficient for me.
I find that i) the cache is restored ii)
yarn install
completes rapidly.Example: github.com/bfdes/bfdes.in/runs/131...
This item may help people the most:
stackoverflow.com/a/62244232/2602771
Recommend taking a look at github.com/bahmutov/npm-install :) tried to make this effortless!
I tried bahmutov/npm-install and it took much longer to retrieve cache compared to that of actions/cache.
So, please keep in mind node_modules size when choose one. :)
As far as I know
bahmutov/npm-install
uses the@actions/cache
under the hood so the cache retrieval itself should be the same as github actions cache.But the other steps might still take longer as it doesn't cache the
node_modules
, like this post suggest as an improvement. So if that's what you compared to I'd expect different results!As also mentioned in the post the "official" recommendation is to not cache the
node_modules
folder itself, as that can potentially cause trouble in some scenarios. You can probably avoid those by putting for example the node version in your cache key or so.github.com/actions/cache/blob/main...
That said I also wish it was a bit faster 😅