Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 152CI/CD · GitHub

How to test on many versions with a CI matrix

Your library must work on Python 3.11, 3.12 and 3.13, or your app on Ubuntu and Windows, or against PostgreSQL 16 and 17. Writing one job per combination is copy-paste. A matrix runs the same job for every combination in parallel. This how-to builds a matrix, adds and removes single combinations, keeps all results visible, and makes required checks work with it.

  • 5 Lessen
  • 2 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

152.1

The quick answer

.github/workflows/test.yml
name: test
on: [push, pull_request]
permissions:
  contents: read
jobs:
  test:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-24.04, windows-2022]
        python: ["3.11", "3.12", "3.13"]
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python }}
          cache: pip
      - run: pip install -r requirements-dev.txt
      - run: pytest -q

2 OS × 3 Python versions = 6 jobs in parallel.

152.2

Step by step

  1. Define the axes under strategy.matrix and use them as ${{ matrix.NAME }} (quick answer).
  2. **fail-fast: false** — otherwise the first failure cancels the other jobs and you do not see the full picture.
  3. Add or remove single combinations:
.github/workflows/test.yml (part)
      matrix:
        os: [ubuntu-24.04, windows-2022]
        python: ["3.11", "3.12", "3.13"]
        exclude:
          - os: windows-2022
            python: "3.11"
        include:
          - os: ubuntu-24.04
            python: "3.14"
            experimental: true

With continue-on-error: ${{ matrix.experimental == true }} on the job, a new version may fail without blocking.

  1. A database version matrix with a service container:
.github/workflows/test.yml (part)
    strategy:
      matrix:
        pg: ["16", "17"]
    services:
      postgres:
        image: postgres:${{ matrix.pg }}
        env: { POSTGRES_PASSWORD: test }
        ports: ["5432:5432"]
        options: --health-cmd "pg_isready" --health-interval 5s --health-retries 10
  1. Required checks: each matrix job has its own name (test (ubuntu-24.04, 3.12)). Simplest: add one small job that needs: test and mark that job as required:
.github/workflows/test.yml (part)
  all-tests:
    needs: test
    runs-on: ubuntu-24.04
    steps:
      - run: echo "all matrix jobs passed"
  1. Limit cost: max-parallel: 4; run the full matrix on main and nightly, a smaller one on pull requests.

Commando's in deze les

152.3

Check that it worked

The Actions run shows one job per combination with its name, a failure in one combination does not cancel the others, and the all-tests job is green only when all are green.

152.4

Common problems

ProblemCauseFix
3.10 becomes 3.1YAML reads it as a number.Quote versions: "3.10".
One failure cancels everythingfail-fast is true by default.fail-fast: false.
Required check never matchesMatrix job names contain the values.Require a summary job with needs:.
Too many jobs / minutesLarge matrix on every push.Smaller matrix on PRs; full matrix nightly; max-parallel.
Windows job fails on shell commandsDefault shell is PowerShell.shell: bash on the step, or OS-specific steps with if: runner.os == 'Windows'.
include adds a whole new job unexpectedlyinclude keys that do not match an existing combination add one.Match existing values to extend; otherwise it is a new job (as intended).

152.5

Go further

1What does fail-fast: false change?

A failing matrix job no longer cancels the other jobs, so you see all results.

2How do you make a matrix a single required status check?

Add a job that needs: the matrix job and mark that job as required.

Commando's in deze les

Alle commando's in dit hoofdstuk