Search code examples
dockercachingtravis-ci

How to remove caches on Travis CI?


I cached a docker image on travis-ci. The docker image is created from a dockerfile. Now my dockerfile changed, and I need to remove caches and rebuild the docker image. How can I remove the caches on travis-ci?

My current .travis.yml looks like this:

language: C
services:
- docker
cache:
  directories:
  - docker_cache
before_script:
- |
  echo Now loading...
  filename=docker_cache/saved_images.tar
  if [[ -f "$filename" ]]; then
    echo "Got one from cache."
    docker load < "$filename"
  else
    echo "Got one from scratch";
    docker build -t $IMAGE .
    docker save -o "$filename" $IMAGE 
  fi
script:
- docker run -it ${IMAGE} /bin/bash -c "pwd"
env:
- IMAGE=test04

Solution

  • Per the docs there are three methods:

    • Using the UI: "More options" -> "Caches" on the repo's page
    • Using the CLI: travis cache --delete
    • Using the API: DELETE /repos/{repository.id}/caches

    That said, Docker images are one of the examples explicitly called out as a thing not to cache:

    Large files that are quick to install but slow to download do not benefit from caching, as they take as long to download from the cache as from the original source

    In your example it's not clear what's involved in the pipeline beyond that Dockerfile - even if the file itself hasn't changed, any of the things that go into it (base image, source code, etc.) might have. Caching the image means you may get false positives, builds that pass even though docker build would have failed.