Sub topic 3
Declaring a service with the services key
This chapter assumes you can read a GitHub Actions workflow (events, jobs, steps), write YAML, and run basic Docker commands. You should also know what an image, a container, a port, and an environment variable are. Here you learn how to declare a service. Networking and readiness checks come in later chapters.
Where services live
The services key sits inside a job, at the same level as runs-on and steps. Each child key is a label you choose. The runner starts one container per label before the first step runs and removes it after the job ends.
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16Here postgres is the label. It is not the image name. Later you will use the label as a hostname when the job runs in a container, so choose a short, clear name.
The fields you will use
A service accepts these fields.
| Field | Purpose |
|---|---|
| `image` | The Docker image to run. Required. |
| `env` | Environment variables set inside the service container. |
| `ports` | Ports to publish from the container to the runner. |
| `options` | Extra flags passed to `docker create`, such as health checks. |
| `volumes` | Volumes or bind mounts, in Docker's `source:destination` form. |
| `credentials` | Registry username and password for private images. |
Services are scoped to a single job. Another job in the same workflow does not see them, and each job that needs a database must declare its own.
A complete example
This job starts PostgreSQL, then runs the tests against it.
name: integration-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app_test
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run test:integrationEach part does a specific job.
image: postgres:16pins the major version. Avoidlatest, because a new release can break your pipeline without any change in your repository.envis read by the PostgreSQL image on first start.POSTGRES_PASSWORDis mandatory for this image, andPOSTGRES_DBcreates the database your tests use.portspublishes container port 5432 on runner port 5432, so steps running on the runner can reach it atlocalhost:5432.optionsis a folded YAML string (>-) that joins the lines into one. The runner passes it todocker create. The health flags make Docker track whether the database accepts connections.- The job-level
envprovidesDATABASE_URLto every step.
What the runner does
The runner creates a Docker network for the job, starts every service on it, and waits for any service that defines a health check to report healthy. Only then does the first step run. If a service never becomes healthy, the job fails during setup.
Common mistakes
- Missing
POSTGRES_PASSWORD. The container exits immediately, and the job fails during setup. - Putting
servicesundersteps. It belongs to the job. - Writing
optionsas a list. It is a single string of Docker flags. - Using secrets in
envwithout care. Test databases do not need real credentials. A throwaway password in the workflow file is fine. Use${{ secrets.NAME }}only for real secrets.
Check your understanding
Exercise
A job declares services: with a child key cache and image: redis:7. What does cache stand for, and why does the choice of this name matter?
Show answer
cache is the label for the service. It is not the image name. The runner uses it to name the container on the job's network, so when the job itself runs in a container, tests reach Redis at the hostname cache.
Exercise
Your job starts a postgres:16 service with ports and options set, but the job fails during setup and the container exits at once. No env is configured. What is the most likely cause, and what is the smallest fix?
Show answer
The PostgreSQL image refuses to start without a password. Add env with POSTGRES_PASSWORD (for example POSTGRES_PASSWORD: postgres) to the service.