Sub topic 9
Running tests against several service versions
This chapter assumes you can write a basic workflow, declare a services block, and run integration tests against PostgreSQL or Redis. It builds on health checks and connection settings from earlier chapters.
Why test several versions
Production databases rarely match the version on your laptop. You may run PostgreSQL 14 in production while the team develops on 16. An upgrade can change query planner behavior, default settings, or the SQL features available. Running the same tests against each version you support catches these problems before they reach production.
A job matrix does this without copying the job. GitHub Actions creates one job per matrix value, and each job gets its own service container.
Using a matrix value as the image tag
A matrix is declared under strategy. You can use its values in the services block through the matrix context.
name: integration-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
postgres: ['14', '15', '16']
services:
postgres:
image: postgres:${{ matrix.postgres }}
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgresThis workflow creates three jobs. Each starts its own PostgreSQL container with a different tag, waits for the health check, and runs the same test command.
Quote the version numbers. Unquoted, YAML reads 14 as a number, which is usually fine. But a value such as 9.60 becomes 9.6, and you then pull the wrong image.
Why the port mapping does not collide
Each matrix job runs on its own runner. Every job can therefore map 5432:5432 without a conflict. The jobs do not share a network or a host port.
The port conflict you might expect from three PostgreSQL containers does not occur. It would occur only if you started several containers inside a single job.
Combining several dimensions
You can add more matrix keys, such as the Node.js version. The matrix then produces every combination. Three PostgreSQL versions and two Node.js versions give six jobs.
strategy:
fail-fast: false
matrix:
postgres: ['14', '15', '16']
node: ['18', '20']Reference the second value with ${{ matrix.node }} in the setup-node step.
The fail-fast: false setting matters here. By default, GitHub cancels the remaining matrix jobs as soon as one fails. When you compare versions, you usually want to see all results. A failure on PostgreSQL 14 only tells you more if you also know that 15 and 16 passed.
Common errors
- Unavailable tag. A typo such as
postgres:17.xmakes the image pull fail, and the job stops before any step runs. Check the tag on the registry. - Missing quotes on versions. As described above, a value like
9.60is changed by YAML. - Hard-coded version elsewhere. If a migration step or client tool assumes one version, the matrix will not test what you think. Search the workflow for stray version numbers.
- Matrix too large. Every combination uses runner minutes. Keep the matrix to the versions you actually support.
- Unclear job names. Matrix jobs are named after their values. A check labeled
test (14)is clear enough, but with several dimensions, setname: test (pg ${{ matrix.postgres }}, node ${{ matrix.node }})so failures are easy to read.
Adding a single extra case
To test one more version without multiplying every combination, use include.
strategy:
matrix:
postgres: ['15', '16']
include:
- postgres: '17'This runs three jobs. The entry under include adds a case that is not part of the main list. Use it for a release candidate you want to watch.
Exercise
A workflow has matrix.postgres: ['14', '15', '16'] and matrix.node: ['18', '20']. How many jobs run, and what setting lets all of them finish when one fails?
Show answer
Six jobs run, one for each combination of three PostgreSQL versions and two Node.js versions. Setting fail-fast: false under strategy stops GitHub from cancelling the remaining jobs after the first failure.