/action-setup-cache-python-poetry

GitHub Action for Python Poetry setup and also the caching of dependencies and the Poetry binary.

MIT LicenseMIT

GitHub Action - Setup and Cache Python Poetry

This action simplifies the setup and caching of Poetry, and provides the following functionality for GitHub Actions users:

Basic Usage

Note:

  • We assume you already have pyproject.toml, poetry.lock and a test module created for pytest to run this workflow example.
  • For your first push to main, the workflow will download Poetry and the required project dependencies, then save it to the cache.
  • For your second run (whether it's on a different job, re-run the job or a different workflow based on your first cached commit) this Action will use the cache.
  • You can see list of cache entries by going to: Your Repo -> Actions tab -> Caches under Managements (left navbar, at the bottom).

Don't forget the limitation of cache.

name: ci

on:
  # Triggers the workflow on push but only for the main branch
  push:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      #-------------------------------------#
      #  Check out repo and set up Python   #
      #-------------------------------------#
      - name: Check out the repository
        uses: actions/checkout@v3
      - name: "Setup Python, Poetry and Dependencies"
        uses: packetcoders/action-setup-cache-python-poetry@main
        with:
          python-version: 3.8
          poetry-version: 1.2.2

      #------------------------#
      #  Run your actual job   #
      #------------------------#
      - name: Run tests
        run: |
          poetry run pytest

With a matrix strategy, several "workflows" are generated based on your matrix inputs. In this case, multiple caches for each of the matrix workflows will be generated.

name: ci

on:
  # Triggers the workflow on push but only for the main branch
  push:
    branches: [ main ]

jobs:
  test:
    # Using matrix strategy
    strategy:
      matrix:
        python-version: [3.8, 3.9, 3.10]
        poetry-version: [1.2.2]
    runs-on: ubuntu-latest
    steps:
      #------------------------------------#
      #  Check out repo and set up Python  #
      #------------------------------------#
      - name: Check out the repository
        uses: actions/checkout@v3
      - name: "Setup Python, Poetry and Dependencies"
        uses: packetcoders/action-setup-cache-python-poetry@main
        with:
          python-version: ${{matrix.python-version}}
          poetry-version: ${{matrix.poetry-version}}

      #-----------------------#
      #  Run your actual job  #
      #-----------------------#
      - name: Run tests
        run: |
          poetry run pytest

Advanced usage

Passing extra arguments to poetry install

By default the action will install your dendencies with poetry install --no-interaction --no-root You can specify extra arguments with install-args, e.g.

      - name: "Setup Python, Poetry and Dependencies"
        uses: packetcoders/action-setup-cache-python-poetry@main
        with:
          python-version: "3.12"
          poetry-version: "1.6.1"
          install-args: --all-extras

to install any optional dependencies alongside the required ones.

License

The scripts and documentation in this project are released under the MIT License.