Skip job if cache exists

I want to skip a job if a particular cache already exists.

In my case, the first job that runs builds the cache. The cache rarely changes. So each run of the workflow runs the job “cache_rebuild”. The job “cache_rebuild” tries to save that cache. Most often the cache already exists (Circle CI reports “Skipping cache generation, cache already exists”). The job “cache_rebuild” isn’t necessary most of the time.

How do I skip the job “cache_rebuild” if a specific cache already exists?

This is similar to this post regarding skipping a build.

There is no way to skip a build conditionally like this, but what you could do is wrap the steps up in a script that first checks if the cache has changed, and if it has not return an exit code of 0 rather than continuing to regenerate the cache.

@levlaz how would I check if the cache has changed from within a script?

That depends on your cache key. If you are are using a checksum you would check that to see if it has changed from what it was when you restored it.

Can you share your config.yml file?

1 Like

Can you share your config.yml file?

Hi there, I would like to do the same.
Did you manage to do something @jtmoon79 ?

Nope. :frowning:

It looks like you are using cache-{{ .Environment.CACHE_VERSION }}-{{ .Branch }}-{{ arch }}-{{ checksum "Pipfile.lock" }}. I assume that if you do a cache restore and the files do not appear, does that not mean that the hash has changed? In other words, the key lookup failed and your dependency fetch has to do an operation from scratch?