CARVIEW |
actions/setup-python caching for setup.py projects
I used to use a combination of actions/setup-python
and actions/cache
in all of my Python GitHub Actions projects in order to install Python dependencies via a cache, rather than hitting PyPI to download copies every time.
actions/setup-python added built-in caching a while ago, but it wasn't obvious to me from the documentation how I could use that with setup.py
based projects (as opposed to projects that install dependencies via Pipfile
or requirements.txt
).
The trick is to use cache: pip
and cache-dependency-path: setup.py
as arguments to the actions/setup-python
action.
Here's the pattern I found that works:
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: pip
cache-dependency-path: setup.py
- name: Install dependencies
run: |
pip install '.[test]'
And here's a full .github/workflows/test.yml
configuration that uses a matrix to run tests against five currently supported Python versions:
name: Test
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: setup.py
- name: Install dependencies
run: |
pip install '.[test]'
- name: Run tests
run: |
pytest
I updated my various cookiecutter templates to use this new pattern in this issue.
Related
- github-actions Running tests against multiple versions of a Python dependency in GitHub Actions - 2023-09-15
- github-actions Testing against Python 3.11 preview using GitHub Actions - 2022-02-02
- github-actions Using the GitHub Actions cache with npx and no package.json - 2022-03-22
- googlecloud Workaround for google-github-actions/setup-gcloud errors - 2022-12-01
- github Configuring Dependabot for a Python project - 2022-01-14
- github-actions Deploying a live Datasette demo when the tests pass - 2022-03-27
- github-actions Using Prettier to check JavaScript code style in GitHub Actions - 2020-12-31
- readthedocs Running pip install '.[docs]' on ReadTheDocs - 2023-11-24
- github-actions Running tests against PostgreSQL in a service container - 2021-02-23
- github-actions Installing different PostgreSQL server versions in GitHub Actions - 2021-07-05
Created 2022-11-28T14:26:50-08:00, updated 2022-11-28T22:06:53-08:00 · History · Edit