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:
1 - name: Set up Python 3.112 uses: actions/setup-python@v43 with:4 python-version: '3.11'5 cache: pip6 cache-dependency-path: setup.py7 - name: Install dependencies8 run: |9 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:
1name: Test2
3on: [push, pull_request]4
5permissions:6 contents: read7
8jobs:9 test:10 runs-on: ubuntu-latest11 strategy:12 matrix:13 python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]14 steps:15 - uses: actions/checkout@v316 - name: Set up Python ${{ matrix.python-version }}17 uses: actions/setup-python@v418 with:19 python-version: ${{ matrix.python-version }}20 cache: pip21 cache-dependency-path: setup.py22 - name: Install dependencies23 run: |24 pip install '.[test]'25 - name: Run tests26 run: |27 pytest
I updated my various cookiecutter templates to use this new pattern in this issue.