Save Cache only on master



I have not found an easy way to save and restore and save cache only under certain conditions.

I’d like to achieve the following:

  • When build runs on master: don’t restore the cache but save the compiled files to cache
  • When build runs on branch: restore cache but don’t save compiled files

This guarantees quick builds on branches but also cleans cache for master builds.

In order for this to work there must be some kind of condition for steps:

  - restore_cache:
      key: deps
      condition: $CIRCLE_BRANCH != "master"

Or is there a better way?


Presently, you will need to leverage multiple jobs.

You can have an if statement to trigger a master build or a branch build, and only do the necessary steps in those builds.

It will definitely speed up your build time.


I ended up doing the following:

  • Always restore cache
  • When on master, delete the directory (therefore triggering a rebuild of the directory)
if [ "${CIRCLE_BRANCH}" == "master" ]; then
  echo resetting cache on master
  rm -rf _build


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.