Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 606
northtree Avatar asked Jul 19 '20 08:07

northtree


People also ask

How does caching work in GitHub actions?

The cache key uses contexts and expressions to generate a key that includes the runner's operating system and a SHA-256 hash of the package-lock. json file. When key matches an existing cache, it's called a cache hit, and the action restores the cached files to the path directory.

How long does GitHub actions cache last?

Get GitHub Actions cache usage for an organization Gets the total GitHub Actions cache usage for an organization. The data fetched using this API is refreshed approximately every 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated.


2 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/[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 :)

like image 145
Sondre Avatar answered Sep 19 '22 07:09

Sondre


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.

like image 43
Matti John Avatar answered Sep 19 '22 07:09

Matti John