Build-image: Cache isn鈥檛 available to deploy previews

Created on 4 May 2020  路  12Comments  路  Source: netlify/build-image

I am creating and caching a file and getting confirmation from the utils that it was cached, but it's not there on subsequent pushes to that PR

this log shows a cache miss, despite confirmation that the cache was created in the previous deploy (you can see the confirmation again, as well as a quick read of the file contents to make sure they're actually there, in the bottom of this log)
https://app.netlify.com/sites/modest-murdock-9912c4/deploys/5eae49da1063950007a54613

source code: https://github.com/jlengstorf/netlify-plugin-applitools-demo/tree/feat/netlify-plugin-high-five/netlify-plugin-en-corg-agement

Most helpful comment

Netlify Support here 馃憢馃徎 Exciting update: this has been fixed 馃帀

Check out the community post for more details: https://community.netlify.com/t/build-cache-is-now-updated-in-deploy-preview-builds/26344

All 12 comments

@ehmicky any thoughts on this one?

~@jlengstorf does this have something to do with deploy previews in particular? Or is it that in general utils.cache is failing to cache and subsequently load the file?~

Nevermind, I looked at https://github.com/jlengstorf/netlify-plugin-gatsby-cache/issues/22 and I see that it's particular to deploy preview builds.

I remember some discussions about caching in deploy previews having non-intuitive behavior at times (outside of Build plugins or the cache utility). If I remember, this discussion was with @fool, but I am not completely sure. @fool Are you aware of anything specific about caching in deploy previews?

the issue I keep raising is this one:

https://github.com/netlify/buildbot/issues/575

...which is only about the FIRST build of any PR. Subsequent builds seem to me to have (correct) cache, and I think Support would have heard about it if the cache was never available to subsequent PR builds.

Thanks for pointing this out @fool.
I will investigate this bug.

I did some testing on this, and it's only impacting caching within Netlify Build - additional node modules, for instance, are cached successfully in deploy preview builds. Also verified that it's not an issue for other non-master builds (ie branch builds).

Once the file is cached successfully (for example during a branch build), it's can be loaded successfully in a deploy preview build. So, the problem seems to be with saving.

I'm not really sure how the context might be interacting with caching for Netlify Build alone.

According to the Debug Cache Plugin, my cache is not being shared between builds on a deploy branch. See https://github.com/spotlightpa/poor-richard/pull/104. (Parcel, like Gatsby, use the .cache directory.)

I believe this is happening on the API side - @erezrokah pointed me in the right direction. We track a list of branches we've got a cache for. If some build of a branch (either triggered by a branch deploy or a deploy preview) has an existing cache, we use it. Otherwise we fall back to the cache for the master branch. However, we only update the list of "branches with an existing cache" on branch deploys.

So, when I saw the node_modules correctly cached during a deploy preview in my report above, it was happening because there had previously (several commits ago) been a branch deploy of that branch.

The node_modules weren't actually cached during the earlier branch deploy, but it caused us to add the branch to our list of branches that we should store a dedicated cache for.

In the deploy previews here you can see that node_modules is not successfully cached because there's never been a deploy preview of that branch.

If the cache doesn't currently work in the deploy preview, can we document that somewhere in the site or in the READMEs?

It's totally fine if that's the way it works, but I just spent my whole day trying to debug why the build cache wasn't working just to find out it was because the cache doesn't work in the deploy preview 馃槄

What I'm currently not understanding is why we're able to set a digest for saving but not restoring. How does netlify even determine which cache to use? In all CI providers with caching I'm familiar with you have a single "cache"-task with a digest that can contain strings and files. The CI provider then uses this for saving and restoring. I should be able to do the same (Azure cache, CircleCI cache, GitHub cache)

The reason why you can usually hard code strings is if you have cache "evolutions" where you simply want to avoid cache sharing for one reason or another. The you go from cache-v1 to cache-v2 and are done.

Right now it's not apparent how netlify is determining if it should re-use a cache or not. By making this behavior configurable or at least transparent we'd probably be able to figure out why netlify is sometimes not caching deploy previews.

On a personal note: Caching for deploy previews is way more important for OSS packages than it is for branch deploys. The number of deploys we get from deploy previews is easily a magnitude bigger than that of production deploys. It's also the situation where you want the lowest response time between push and ready deploy.

Netlify Support here 馃憢馃徎 Exciting update: this has been fixed 馃帀

Check out the community post for more details: https://community.netlify.com/t/build-cache-is-now-updated-in-deploy-preview-builds/26344

Was this page helpful?
0 / 5 - 0 ratings