Date Modified Tags gitlab / python

Using a pip cache on GitLab CI can speed up your builds considerably (my build time went down from 8 minutes to 3 minutes). Here is how to do it:

# .gitlab-ci.yml
image: python:2

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/pip-cache"

cache:
  paths:
    - "$CI_PROJECT_DIR/pip-cache"
  key: "$CI_PROJECT_ID"

test:
  script:
    # Make sure you get modern pip and setuptools. Old pip
    # versions may ignore the cache. Old setuptools versions
    # may be unable to install some packages.
    # You may want to pin these to exact versions instead, in order
    # to keep your pipeline deterministic.
    - pip install -U pip setuptools
    - pip install -r requirements_testing.txt
    - tox

I encountered two gotchas. One is that apparently, the path must be inside the build directory. You cannot tell GitLab to cache ~/.cache/pip or such.

The second is that you will probably see this confusing warning on your first build:

The directory 'pip-cache' or its parent directory is not owned by the
current user and caching wheels has been disabled. check the permissions
and owner of that directory. If executing pip with sudo, you may want
sudo's -H flag.

This is just pip`s way of telling you that the cache directory does not exist yet. It will create it. Ignore the warning on the first build. On the second build, you should see that pip is using the cache.