How to Set Up CI for Python (pip) With GitHub Actions
setup-python with cache: pip keys the wheel cache on your requirements files so installs stay fast.
Run actions/setup-python with cache: 'pip', install from requirements.txt, lint with ruff, and run pytest. A matrix across Python minor versions catches version-specific breakage.
Steps
- Check out the code.
- Run
actions/setup-pythonwithcache: 'pip'and a Python matrix. - Install with
pip install -r requirements.txt. - Lint with
ruff check .and test withpytest.
Workflow
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- run: pip install -r requirements.txt
- run: ruff check .
- run: pytestGotchas
cache: 'pip'keys onrequirements*.txtby default; setcache-dependency-pathif yours live elsewhere.- Quote version numbers like
'3.10'so YAML does not read them as the float 3.1.
Related guides
How to Set Up CI for Python (Poetry) With GitHub ActionsSet up GitHub Actions CI for a Poetry project: install Poetry, let setup-python cache the Poetry virtualenv,…
How to Set Up CI for Python (uv) With GitHub ActionsSet up GitHub Actions CI for a uv project with astral-sh/setup-uv, enable-cache for the uv cache, uv sync for…