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 }
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate