How to Spin Up a Redis Service for Tests in GitHub Actions
A Redis service container gives tests a real key-value store on localhost:6379; a redis-cli ping health check confirms readiness.
Declare redis under services: with the official redis image, map port 6379, and use redis-cli ping as the health command.
Steps
- Add
services.rediswithimage: redis:7. - Map
ports: [6379:6379]. - Add a
redis-cli pinghealth check expectingPONG. - Point your client at
redis://localhost:6379.
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
REDIS_URL: redis://localhost:6379
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testGotchas
- If you set a password with
--requirepass, the health check needsredis-cli -a <pass> pingor it reports NOAUTH. - Redis is in-memory, so each job starts empty; do not assume data from a previous run survives.
Related guides
How to Spin Up MongoDB for Tests in GitHub ActionsRun MongoDB as a GitHub Actions service container with a mongosh ping health check so integration tests hit a…
How to Spin Up a Postgres Service for Tests in GitHub ActionsRun a real Postgres alongside your GitHub Actions tests using a service container with a pg_isready health ch…
How to Set Up a Postgres + Redis Service for Tests in GitHub ActionsRun Postgres and Redis as service containers in a GitHub Actions job with health checks, so integration tests…