You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
GitHub Action
Setup and Cache Python Poetry
v1.1.0
This action simplifies the setup and caching of Poetry, and provides the following functionality for GitHub Actions users:
- Python setup via
actions/setup-python
. - Poetry install via
snok/install-poetry
. - Poetry binary caching via
actions/cache
. - Poetry dependency caching via
actions/cache
.
Note:
- We assume you already have
pyproject.toml
,poetry.lock
and a test module created forpytest
to run this workflow example. - For your first
push
tomain
, 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
underManagements
(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
Matrix Strategy Usage
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
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.
The scripts and documentation in this project are released under the MIT License.