Sub topic 13
Integration tests with service containers
This chapter assumes you can already write a workflow that checks out code, installs Node.js, and runs npm test. It also assumes you know what a Docker image is and have a project whose integration tests need a database.
Why service containers
Unit tests run in isolation. Integration tests need real dependencies, such as a database or a cache. A service container is a Docker container that GitHub Actions starts next to your job and removes when the job ends. Each run gets a fresh, empty instance, so tests cannot be affected by leftover data from earlier runs.
Service containers work only on Linux runners, such as ubuntu-latest.
A PostgreSQL service
The services key sits at the job level. Each entry has a name and an image.
jobs:
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe parts that matter:
imageselects the container image. Pin a version such aspostgres:16so a new major release cannot break your runs.envunder the service sets the container's environment variables. The official PostgreSQL image refuses to start withoutPOSTGRES_PASSWORD.portsmaps a container port to the runner.5432:5432means host port 5432 forwards to container port 5432.optionspasses extra flags todocker create. Here it defines a health check.- The job-level
envgives the tests a connection string. Your test code readsDATABASE_URL.
Health checks
A container counts as started as soon as its process launches, but the database may need several seconds before it accepts connections. Without a health check, your tests can start too early and fail with "connection refused".
With --health-cmd, the runner waits until the container reports healthy before it runs your steps. The command pg_isready ships in the PostgreSQL image. With the settings above, the check runs every 10 seconds, and the job fails if five checks in a row fail.
How the tests reach the service
The address depends on where the job's steps run.
| Job runs in | Hostname to use | Ports mapping needed |
|---|---|---|
| The runner machine (default) | `localhost` | Yes |
| A container (`container:` key) | The service name, such as `postgres` | No |
In this chapter's example the steps run directly on the runner, so you use localhost and must publish the port. If you later add container: to the job, the steps join the same Docker network as the service, and the host becomes postgres.
If you map the port as just 5432 with no host side, Docker picks a random free host port. Read it from ${{ job.services.postgres.ports['5432'] }}.
Common errors
- Connection refused: the health check is missing or the port is not published.
- Container fails to start: a required variable, such as
POSTGRES_PASSWORD, is missing. - Works on Linux, fails elsewhere: service containers are not supported on Windows or macOS runners. Do not combine them with a matrix that includes those systems.
- Wrong hostname:
localhostinside a job container points to the job container itself, not the service.
Exercise
Your integration tests fail intermittently with "connection refused" on the first few runs of each job, though the Postgres service is defined correctly and the port is published. What is the most likely cause, and how do you fix it?
Show answer
The tests start before PostgreSQL is ready to accept connections. Add a health check to the service's options, for example --health-cmd pg_isready with an interval, timeout, and retries. The runner then waits for the container to be healthy before running the steps.