Speed up checkout on CircleCI with caching

Standard

If you’ve got a long Git history or large files changing frequently in your repository, checking out the repository can take a good amount of time in your CircleCI jobs. It’s annoying when most of the time is spent on non useful tasks like checking out your code or downloading dependencies. In these situations, caching is what you need. The idea is that checking out your code takes time if you start from a blank state. If you already have a recent previous commit locally, checking out new changes will be far faster.

You can cache your Git folder with the following configuration.

    steps:
      - restore_cache:
          keys:
            - source-v1-{{ .Branch }}-{{ .Revision }}
            - source-v1-{{ .Branch }}-
            - source-v1-

      - checkout

      - save_cache:
          key: source-v1-{{ .Branch }}-{{ .Revision }}
          paths:
            - ".git"

When CircleCI encounters a list of keys, the cache will be restored from the first match. In this example, restore_cache looks for a cache hit from the current Git revision, then for a hit from the current branch, and finally for any cache hit, regardless of branch or revision. If there are multiple matches, the most recently generated cache will be used.

I was able to take down a 30-35s checkout task down to the expected 2-3s. A substantial amount of time saved for your continuous integration!