AI Hints
Home›CI/CD with GitHub Actions Tutorial›Artifacts, Services and Timeouts
CI/CD with GitHub Actions · Lesson 53 of 138

Service Containers

A database for tests.

⏱ 2 min read▶ Video at 59:59🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

Many apps need a database, or a cache like Redis, to be tested properly. In GitHub Actions, a job can start service containers alongside it. Real databases, running in Docker, just for that job.

Service Containers
From the video: Service Containers
  • Apps need databases
  • Services start alongside
  • Real, and temporary

A Postgres service

Under services, we name a container, postgres, and pick its image. Env sets the password. Ports makes it reachable on port five four three two. And health checks make the job wait until the database is actually ready, before the steps start.

services.ymlyaml
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: test
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 5s
    steps:
      - uses: actions/checkout@v4
      - run: pytest -q
        env:
          DATABASE_URL: postgres://postgres:test@localhost:5432/postgres

Why it matters

Tests against a fake database can miss real problems, like a query that only fails in Postgres. Service containers give each run a fresh, real database, that disappears when the job ends. Nothing to install, and nothing to clean up.

keyvalue
services:containers for the job
image:any Docker image
Health checkswait until ready
Fresh each runnothing to clean

Summary

Service containers give a job real databases and caches, fresh for every run. Next, protecting your pipeline with timeouts and continue on error.

Key takeaways
  • services:
  • postgres:16
  • health checks
Next up: Timeouts and continue-on-error — keeping runs under control