Problem
Right now I am writing a .gitlab-ci.yml
for a Python project hosted on Gitlab.
I do not really know where to put the setup of a virtual environment and the installation of requirements from the requirements.txt
.
My primary resource is the Gitlab Python.gitlab-ci.yml
template.
In there, virtualenv is installed before every job, using the before_script
keyword and a virtualenv named 'venv' is created:
before_script:
- python -V # Print out python version for debugging
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
Additionally, caching for 'venv' gets set up:
cache:
paths:
- .cache/pip
- venv/
I want to do the same in my script and additionally install from the requirements.txt
in my repo. Afterwards, I want to execute code analysis (pylint, flake8) and tests in different stages. If I append pip install -r requirements.txt
to before_script
, it gets executed with every job, which produces unnecessary logs.
Question
Wouldn't it make more sense to
- Define cached sub-directories. (as seen above)
- Only call
source venv/bin/activate
inbefore_script
. - Add a stage or job for setup that calls
virtualenv venv
to generate an environment once. This job also callspip install -r requirements.txt
once.
Where would you create your venv and install your requirements?