Skip to content
Latchkey

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-python with cache: 'pip' and a Python matrix.
  • Install with pip install -r requirements.txt.
  • Lint with ruff check . and test with pytest.

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: pytest

Gotchas

  • cache: 'pip' keys on requirements*.txt by default; set cache-dependency-path if yours live elsewhere.
  • Quote version numbers like '3.10' so YAML does not read them as the float 3.1.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →