'How to cache poetry install for GitHub Actions
I tried to use this actions/cache@v2 to cache poetry venv. There are only two libraries pylint and pytest installed. It seems that installation was cached (cache size ~ 26MB). However, they couldn't be retrieved after cache hit.
The cache installed libraries are not found while run
poetry run pip list
Package Version
---------- -------
pip 20.1.1
setuptools 41.2.0
https://github.com/northtree/poetry-github-actions/runs/875926237?check_suite_focus=true#step:9:1
The YAML is here.
Could I know how to use actions/cache@v2 to cache poetry installation / virturalenv to avoid reinstalling dependencies.
Solution 1:[1]
In your YAML file you are using dschep/[email protected] to install Poetry, which sets poetry config virtualenvs.create false, which means that the current python interpreter/virtualenv is used. Because you're not activating a virtualenv anywhere poetry is just using the system python and there isn't a virtualenv inside the ~/.poetry directory.
It should work if you set poetry config virtualenvs.create true, e.g.:
- name: Install poetry
uses: dschep/[email protected]
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project false
poetry config cache-dir ~/.poetry
poetry config virtualenvs.path ~/.poetry/venv
NOTE: According to the docs for dschep/install-poetry-action there is an option to set poetry config virtualenvs.create true during install but it seems to be broken at the moment (see https://github.com/dschep/install-poetry-action/issues/11). In any case I personally prefer doing it in the same config block as everything else.
Solution 2:[2]
You don't need to use an external action for installing Poetry, or set up virtual environments. poetry installs modules to ~/.cache/pypoetry, so:
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: |
~/.cache/pypoetry
.venv
key: poetry-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install poetry no matter what
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Install requirements
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install
It should theoretically be possible to cache installation of poetry too, which is installed to ~/.poetry. I couldn't find the way, though.
However, by the time you read this, this PR might have already landed so all you're going to need to cache Poetry deps is actions/setup-python
Solution 3:[3]
Caching is integrated natively to actions/setup-python (https://github.com/actions/setup-python#caching-packages-dependencies):
steps:
- uses: actions/checkout@v3
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v3
with:
python-version: '3.9'
cache: 'poetry'
- run: poetry install
- run: poetry run pytest
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matti John |
| Solution 2 | |
| Solution 3 | Sylvain Lesage |
