CI/CD for a Hapi API with GitHub Actions
Lint, test, and containerize your Hapi API on every push.
A Hapi API is configuration-centric and commonly tested with the lab and code toolkit. This recipe lints, runs lab tests against a Postgres service, builds, and ships a container.
What the pipeline does
- install deps with npm ci
- lint with eslint
- run lab tests against Postgres
- build the app
- build and push a container image on main
The workflow
A Postgres service container backs integration tests; lab runs the suite with coverage and threshold enforcement.
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
options: >-
--health-cmd pg_isready --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
cache: npm
- run: npm ci
- run: npm run lint --if-present
- run: npx lab -t 85 -L
- run: npm run build --if-presentCaching and speed
cache: npm restores installs. The Postgres service plus the lab suite dominate CI time; cheaper managed runners such as Latchkey keep frequent runs inexpensive and auto-retry transient service-startup races.
Deploying
Build a container from a node:slim base with production deps, push to GHCR or ECR, and deploy to ECS, Kubernetes, or a PaaS. Expose the configured port and add a /health route for the readiness probe.
Key takeaways
- lab enforces a coverage threshold (-t) and lint (-L) together.
- Back integration tests with a Postgres service container.
- Add a /health route for orchestrator readiness probes.