How to cache poetry install for GitHub Actions

Viewed 6813

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.

4 Answers

@northtree's answer is correct, but for anyone browsing, you should know that hte referenced action is no longer maintained.

For Poetry installs using versions >= 1.1.0 I'd recommend using this snippet to cache your Poetry dependencies:

...
- name: Install poetry
  uses: snok/install-poetry@v1.0.0
  with:
    virtualenvs-create: true
    virtualenvs-in-project: true
- name: Load cached venv
  id: cached-poetry-dependencies
  uses: actions/cache@v2
  with:
    path: .venv
    key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
  run: poetry install
  if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
...

More complete examples are documented here :)

In your YAML file you are using dschep/install-poetry-action@v1.3 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/install-poetry-action@v1.3

    - 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.

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

Related