Run Poetry in a Github action

posted on 2022-02-10

Using Poetry in a Github action is simple. However, because it's nice to copy-paste a good start, here is a quick snippet of how you can use it.

Save the file as .github/workflows/main.yml:

name: main

on:
  push:
    branches: [main]
  pull_request:

jobs:
  ci:
    runs-on: ubuntu-20.04
    env:
      POETRY_VIRTUALENVS_IN_PROJECT: true
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: 3.8
      - run: pip install poetry==1.1.12
      - name: cache venv
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
      - run: poetry install
      - run: poetry run pytest tests