How to run commands only if there is no cache



For our CD process we need to install the Google Cloud Engine CLI and Kubernetes CLI. Installing these are where most of our time is spent. Is there a way to cache them, and only run the commands if there is no cache?

Here are the commands

      - sudo /opt/google-cloud-sdk/bin/gcloud --quiet components update --version 131.0.0
      - sudo /opt/google-cloud-sdk/bin/gcloud --quiet components update --version 131.0.0 kubectl
      - echo $GCLOUD_SERVICE_KEY | base64 --decode -i > ${HOME}/gcloud-service-key.json
      - sudo /opt/google-cloud-sdk/bin/gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json
      - sudo /opt/google-cloud-sdk/bin/gcloud config set project $PROJECT_NAME
      - sudo /opt/google-cloud-sdk/bin/gcloud --quiet config set container/cluster $CLUSTER_NAME
      - sudo /opt/google-cloud-sdk/bin/gcloud config set compute/zone ${CLOUDSDK_COMPUTE_ZONE}
      - sudo /opt/google-cloud-sdk/bin/gcloud --quiet container clusters get-credentials $CLUSTER_NAME
      - docker build -t${PROJECT_NAME}/${APP_NAME}:$CIRCLE_SHA1 .
      - docker tag${PROJECT_NAME}/${APP_NAME}:$CIRCLE_SHA1${PROJECT_NAME}/${APP_NAME}:latest
      - sudo /opt/google-cloud-sdk/bin/gcloud --quiet container clusters get-credentials ${CLUSTER_NAME}
      - sudo /opt/google-cloud-sdk/bin/gcloud docker push${PROJECT_NAME}/${APP_NAME}
      - sudo chown -R ubuntu:ubuntu /home/ubuntu/.kube
      - /opt/google-cloud-sdk/bin/kubectl patch deployment ${APP_NAME} -p '{"spec":{"template":{"spec":{"containers":[{"name":"'"${APP_NAME}"'","image":"'"${PROJECT_NAME}"'/'"${APP_NAME}"':'"$CIRCLE_SHA1"'"}]}}}}'


You can check for the existence of binaries you installed into a predetermined cache directory. Then, if they don’t exists, run something.

For example, a statically generated website called is built on CircleCI using Hugo. Hugo doesn’t come pre-installed in CircleCI so we run a script to check for it first before installing it. You can view the project here:

Basically three things are done:

  • set ~/bin as a cached directory
  • check if the hugo binary exists in ~/bin
  • if not, download and unpack Hugo into ~/bin and then continue the build


Can we have an environment variable to say if we doing a rerun with or without cache?


I don’t believe that to current be a thing but you can open a feature request.

For without cache specifically, if there’s a known file that would be in cache, you can simply check for that file to determine if cache has been loaded or not.

For re-run builds, you can check the commit SHA1. For a build that’s run again, the build number would be incremented but the SHA1 would be the same as the build it originated from.