# ===========================================
# CI Pipeline — Generated by SDD DevFlow
# ===========================================
# Runs on pushes and PRs to main branch.
# Customize steps as your project grows.

name: CI

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

jobs:
  ci:
    runs-on: ubuntu-latest

    # -- Database service (remove if not needed) --
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: testdb
        ports:
          - 5432:5432
        options: >-
          --health-cmd="pg_isready"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5

    env:
      NODE_ENV: test
      DATABASE_URL: postgresql://test:test@localhost:5432/testdb

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci

      - name: Lint
        run: npm run lint --if-present

      - name: Test
        run: npm test --if-present

      - name: Build
        run: npm run build --if-present

# ===========================================
# Scaling tips
# ===========================================
# When this project grows and you split CI into multiple jobs with path
# filters (e.g., test-api, test-frontend triggered only when relevant files
# change), enabling branch protection that requires those individual checks
# will create a deadlock for docs-only PRs: no code changed → no jobs run →
# required checks never report → merge blocked.
#
# Solution: add a rollup job that always runs and aggregates the others.
# Then configure branch protection to require ONLY the rollup, not the
# individual jobs:
#
#   ci-success:
#     runs-on: ubuntu-latest
#     needs: [test-api, test-frontend]   # list all jobs to aggregate
#     if: always()
#     steps:
#       - name: All required jobs passed or were skipped
#         run: |
#           if [[ "${{ contains(needs.*.result, 'failure') }}" == "true" ]]; then
#             echo "One or more required jobs failed"
#             exit 1
#           fi
#
# The rollup passes when all dependencies pass OR were skipped due to path
# filters. This is the standard pattern for path-filtered CI + required
# checks. Avoid `|| true` and `continue-on-error` — they silence real failures.
