CI with GitHub Actions

Pipeline

.github/workflows/ci.yml

.github/workflows/ci.yml

name: CI
on:
  pull_request:
  push:
    branches: [main]

jobs:
  check:
    runs-on: ubuntu-latest
    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 tsc --noEmit
      - run: npm test
      - uses: actions/cache@v4
        with:
          path: .next/cache
          key: nextjs-${{ hashFiles('package-lock.json') }}-${{ hashFiles('app/**', 'components/**', 'lib/**') }}
      - run: npm run build
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL_CI }}

  deploy:
    needs: check
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Deploy over SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.VM_HOST }}
          username: deploy
          key: ${{ secrets.VM_SSH_KEY }}
          script: cd /srv/shopease && git pull && npm ci && npm run build && pm2 reload shopease-web

Tips

  • Cache .next/cache between runs: builds get much faster.
  • Run the build in CI: it catches type errors and pages that fail to pre-render.
  • Use GitHub environments with required reviewers for production deploys.