Published on

Setting up the cache on Github Actions and Poetry

Authors

Introduction

Github actions was released in 2019 aiming at providing a new tool to build your CI/CD pipeline. You can leverage the cache to accelerate your pipelines and decrease your bill. Let’s see how to do this when using python and poetry.

Before

So far I was installing poetry with abatilo/actions-poetry and then setting up the cache manually using actions/cache. That was not straightforward to set up, especially when you have to test in the CI directly. Using https://github.com/nektos/act to run Github actions locally helped marginally. But I just noticed that an official action was released to do the same thing simpler.

Updated Version

By using actions/setup-python@v4. It implements its caching logic and is compatible with poetry. But when I tried it first, it didn’t work. I’m using the following poetry.toml to standardize poetry's behavior among the team:

[cache]
cache-dir = "./.cache/"
[virtualenvs]
create = true
in-project = true

This caused https://github.com/actions/setup-python not to be able to find the .venv. I could solve it by forcing the config before running the poetry install command:

- name: Install dependencies
  uses: actions/setup-python@v4
  with:
    python-version: 3.9
    cache: 'poetry'
- run: poetry config virtualenvs.in-project false && poetry install

The whole workflow is available here.