Interview Prep/CI/CD/GitHub Actions

How do you run a job in a container or with service containers?

IntermediateConceptGitHub Actions
Answer

container: runs all steps of a job inside an image; services: starts sidecar containers (databases, Redis) reachable by their service name when the job is in a container, or via localhost and the mapped port otherwise. Use options for health checks so tests wait for readiness.

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env: { POSTGRES_PASSWORD: pw }
        ports: ['5432:5432']
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 5s --health-retries 10
    steps:
      - uses: actions/checkout@v4
      - run: npm test
        env: { DATABASE_URL: postgres://postgres:pw@localhost:5432/postgres }
All CI/CD questions

More on GitHub Actions