Problem to solve
I use the following .gitlab-ci.yml
to lint a Python poetry project:
image: python
stages:
- lint
cache:
paths:
- .poetry
- .venv
key: "${CI_COMMIT_REF_SLUG}"
before_script:
- export POETRY_HOME=.poetry
- python3 -m venv $POETRY_HOME
- $POETRY_HOME/bin/pip install poetry
- $POETRY_HOME/bin/poetry config virtualenvs.in-project true
- $POETRY_HOME/bin/poetry install
- source $($POETRY_HOME/bin/poetry env info --path)/bin/activate
pre-commit:
stage: lint
script:
- pre-commit install
- pre-commit run --all-files
Note that I use pip to install poetry into ./.poetry/
and poetry to install the project dependencies into ./.venv/
. This is a time consuming process so I cache both these directories. Using key: "${CI_COMMIT_REF_SLUG}"
, poetry and the dependencies are installed the first time I push a new branch.
When I push additional commits, pip doesn’t install anything and just displays:
$ $POETRY_HOME/bin/pip install poetry
Requirement already satisfied: poetry in ./.poetry/lib/python3.12/site-packages (1.8.3)
Requirement already satisfied: build<2.0.0,>=1.0.3 in ./.poetry/lib/python3.12/site-packages (from poetry) (1.2.1)
...
Also poetry doesn’t install anything and displays:
$ $POETRY_HOME/bin/poetry install
Installing dependencies from lock file
No dependencies to install or update
Installing the current project: myproject (0.0.1)
This saves time, which is good. However, after the CI job completes successfully, it recreates the cache:
Saving cache for successful job 02:41
Creating cache ci-cd-non_protected...
.poetry: found 4165 matching artifact files and directories
.venv: found 40214 matching artifact files and directories
No URL provided, cache will not be uploaded to shared cache server. Cache will be stored only locally.
Created cache
This step takes a lot of time and nullifies the time saving caching is supposed to provide in the first place. I know that recaching ./.poetry
is completely unnecessary because pip changes nothing about its conents, and recaching ./.venv
is also unnecessary because if anything the editable installation of myproject
changed, but that will get overwritten in the next CI run anyways.
Steps to reproduce
Use the above .gitlab-ci.yml
in any Python poetry project that depends on pre-commit.
Versions
Please select whether options apply, and add the version information.
- Self-managed
- GitLab.com SaaS
- Self-hosted Runners
Versions
-
GitLab (Web:
/help
or self-managed system information): GitLab Enterprise Edition v16.11.2-ee -
GitLab Runner, if self-hosted (Web
/admin/runners
or CLIgitlab-runner --version
):
Version: 17.0.0
Git revision: 44feccdf
Git branch: 17-0-stable
GO version: go1.21.9
Built: 2024-05-16T13:46:14+0000
OS/Arch: linux/amd64