Gitlab Cache example

Joost Döbken
1 min readMar 4, 2020

--

How caching works and why you would like to use it is very well explained by Gitlab themselves and I certainly don’t have anything to add to their story. This article includes nothing more than a super simple demonstration on how to apply caching to speed up the simple Gitlab CI pipeline for a Python Django project as a short follow up on my earlier article.

storing project dependencies

The only dependencies that we will focus on are the artifacts created by the installation of pip packages of both the test and the production setup.

We can add the following lines somewhere on the top of our .gitlab-ci file to add two cache directories for both setups and share these dependencies within one branch using a key:

cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- ~/.cache/pip-test/
- ~/.cache/pip-production/

Now in the specific Dockerfiles replace the pip install ... line with:

RUN pip install --cache-dir=~/.cache/pip-test/ -r /requirements/test.txt

for the test setup (compose/test/django/Dockerfile) and

RUN pip install --cache-dir=~/.cache/pip-production/ -r    /requirements/production.txt \
&& rm -rf /requirements

for the production setup (compose/production/django/Dockerfile).

--

--