CI and CD with GitHub Actions

Test, Build, Deploy

.github/workflows/api.yml

name: api
on:
  push: { branches: [main] }
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env: { POSTGRES_USER: shopease, POSTGRES_PASSWORD: secret, POSTGRES_DB: shopease_test }
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 5s --health-retries 10
    env:
      DATABASE_URL: postgresql://shopease:secret@localhost:5432/shopease_test
      JWT_SECRET: test-secret-that-is-at-least-32-chars
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm }
      - run: npm ci
      - run: npm run lint
      - run: npx prisma migrate deploy
      - run: npm test -- --ci
      - run: npm run test:e2e -- --ci
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: docker build -t asia-south1-docker.pkg.dev/shopease/api/api:${{ github.sha }} .
      # push the image, then roll it out (Cloud Run, Kubernetes, or ssh + pm2 reload on a VM)