northtree
northtree

Reputation: 9255

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.

Upvotes: 29

Views: 16640

Answers (4)

Sondre
Sondre

Reputation: 584

@northtree's answer is correct, but for anyone browsing, you should know that the 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/[email protected]
  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 :)

Upvotes: 40

Sylvain Lesage
Sylvain Lesage

Reputation: 961

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

Upvotes: 31

jjmerelo
jjmerelo

Reputation: 23477

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

Upvotes: 3

Matti John
Matti John

Reputation: 20467

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.

Upvotes: 6

Related Questions