https://github.com/packetcoders/action-setup-cache-python-poetry
GitHub Action for Python Poetry setup and also the caching of dependencies and the Poetry binary.
https://github.com/packetcoders/action-setup-cache-python-poetry
github github-actions poetry python
Last synced: 3 months ago
JSON representation
GitHub Action for Python Poetry setup and also the caching of dependencies and the Poetry binary.
- Host: GitHub
- URL: https://github.com/packetcoders/action-setup-cache-python-poetry
- Owner: packetcoders
- License: mit
- Created: 2022-10-21T13:41:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-29T17:38:44.000Z (almost 2 years ago)
- Last Synced: 2025-06-14T16:53:25.950Z (7 months ago)
- Topics: github, github-actions, poetry, python
- Homepage:
- Size: 16.6 KB
- Stars: 15
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
* Python setup via [`actions/setup-python`](https://github.com/actions/setup-python).
* Poetry install via [`snok/install-poetry`](https://github.com/snok/install-poetry).
* Poetry binary caching via [`actions/cache`](https://github.com/actions/cache).
* Poetry dependency caching via [`actions/cache`](https://github.com/actions/cache).
## 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](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache)) 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](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy).
```yml
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](https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs) 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.
```yml
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.
```yml
- 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](LICENSE).