continuous-integrationgithub-actionspython-poetry

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

  • @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/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 :)