Sub topic 7
Passing connection settings to your tests
This chapter assumes you can read a GitHub Actions workflow, know what a job and a step are, and have run integration tests against a database or cache. It also assumes you know the earlier chapters' basics: how the services key declares a container, how host and port differ between a runner job and a container job, and how health checks work.
Why connection settings need care
Your tests need a host, a port, and credentials to reach the service. Hard-coding them in test code makes the same tests fail on a laptop or in a different workflow. The usual fix is to read one environment variable, such as DATABASE_URL, and set it in the workflow. Locally you set it in your shell. In CI you set it in YAML.
The values depend on where the job runs.
| Job runs | Host | Port |
|---|---|---|
| Directly on the runner | `localhost` | The mapped host port |
| Inside a container | The service label, such as `postgres` | The container's own port |
Setting variables at the right level
You can set env at three levels: workflow, job, or step. Use the narrowest level that works. A job-level env is a good fit for a connection string that every step in the job shares.
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:integrationThe job runs on the runner, so the host is localhost. The fixed mapping 5432:5432 makes the port predictable. Your test code only reads process.env.DATABASE_URL.
Using the label inside a container job
When the job itself runs in a container, localhost points at the job container, not at the service. Use the service label as the hostname and the port the image listens on. No ports mapping is needed.
jobs:
test:
runs-on: ubuntu-latest
container: node:20
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integrationHere the label postgres appears twice in the URL. The first is the user name and password, and the second, after @, is the hostname.
Reading a dynamic port
If you list only the container port, Docker picks a free host port. The workflow then has to look it up. The job.services context holds the chosen port, and you read it with an expression.
jobs:
test:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379/tcp
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
env:
REDIS_URL: redis://localhost:${{ job.services.redis.ports['6379'] }}Two details matter here. The job context is not available at job-level env, so this value is set on the step. The key inside ports[...] is the container port as a string.
Common errors
- Connection refused on
localhostin a container job. The service is not on the job container's loopback. Use the label. - Unknown host
postgresin a runner job. Label names only resolve inside the Docker network. Uselocalhostand a mapped port. - Empty port in the URL. The expression key does not match the container port you listed, or the service has no
portsentry. - Authentication failed. The password in
DATABASE_URLdiffers fromPOSTGRES_PASSWORDon the service. - Tests start before the database accepts connections. Add the health options so the job waits for the service.
Exercise
Exercise
A job runs on ubuntu-latest with no container: key. The Redis service has ports: [6379/tcp]. Which value should REDIS_URL have, and at which level can you set it?
Show answer
redis://localhost:${{ job.services.redis.ports['6379'] }}. Set it at step level, because the job context is not available in job-level env.
Exercise
Your job runs in container: node:20 and the service label is cache. The service listens on its default port 6379. What is the Redis URL?
Show answer
redis://cache:6379. The label is the hostname, and the container port is used directly, so no ports mapping is needed.