Search code examples
pythongitlabcontinuous-integrationpython-poetry

How to cache "poetry install" for Gitlab CI?


Is there a way to cache poetry install command in Gitlab CI (.gitlab-ci.yml)?

For example, in node yarn there is a way to cache yarn install (https://classic.yarnpkg.com/lang/en/docs/install-ci/ Gitlab section) this makes stages a lot faster.


Solution

  • GitLab can only cache things in the working directory and Poetry stores packages elsewhere by default:

    Directory where virtual environments will be created. Defaults to {cache-dir}/virtualenvs ({cache-dir}\virtualenvs on Windows).

    On my machine, cache-dir is /home/chris/.cache/pypoetry.

    You can use the virtualenvs.in-project option to change this behaviour:

    If set to true, the virtualenv wil be created and expected in a folder named .venv within the root directory of the project.

    So, something like this should work in your gitlab-ci.yml:

    before_script:
      - poetry config virtualenvs.in-project true
    
    cache:
      paths:
        - .venv