ci.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. name: CI
  2. on:
  3. push:
  4. branches: [main]
  5. pull_request:
  6. branches: [main]
  7. # Run on PRs targeting main, but skip for repo owner (runs local tests)
  8. # Skip CI for PRs authored by repo owner (they run tests locally)
  9. # Uses PR author instead of triggering actor so rebasing by owner doesn't skip CI
  10. env:
  11. PYTHON_VERSION: '3.11'
  12. NODE_VERSION: '22'
  13. # Cancel in-progress runs for the same branch
  14. concurrency:
  15. group: ${{ github.workflow }}-${{ github.ref }}
  16. cancel-in-progress: true
  17. # Minimum permissions for all jobs
  18. permissions:
  19. contents: read
  20. jobs:
  21. # ============================================================================
  22. # Backend Checks
  23. # ============================================================================
  24. backend-lint:
  25. name: Backend Lint
  26. runs-on: ubuntu-latest
  27. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  28. steps:
  29. - uses: actions/checkout@v4
  30. - name: Set up Python
  31. uses: actions/setup-python@v5
  32. with:
  33. python-version: ${{ env.PYTHON_VERSION }}
  34. - name: Install ruff
  35. run: pip install ruff
  36. - name: Run ruff check
  37. run: ruff check backend/
  38. - name: Run ruff format check
  39. run: ruff format --check backend/
  40. backend-security:
  41. name: Backend Security
  42. runs-on: ubuntu-latest
  43. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  44. continue-on-error: true
  45. steps:
  46. - uses: actions/checkout@v4
  47. - name: Set up Python
  48. uses: actions/setup-python@v5
  49. with:
  50. python-version: ${{ env.PYTHON_VERSION }}
  51. - name: Install dependencies
  52. run: |
  53. python -m pip install --upgrade pip
  54. pip install -r requirements.txt
  55. pip install pip-audit
  56. - name: Run pip-audit
  57. run: pip-audit --desc on
  58. backend-tests:
  59. name: Backend Tests
  60. runs-on: ubuntu-latest
  61. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  62. needs: backend-lint
  63. steps:
  64. - uses: actions/checkout@v4
  65. - name: Set up Python
  66. uses: actions/setup-python@v5
  67. with:
  68. python-version: ${{ env.PYTHON_VERSION }}
  69. - name: Cache pip
  70. uses: actions/cache@v4
  71. with:
  72. path: ~/.cache/pip
  73. key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
  74. restore-keys: |
  75. ${{ runner.os }}-pip-
  76. - name: Install dependencies
  77. run: |
  78. python -m pip install --upgrade pip
  79. pip install -r requirements.txt
  80. pip install -r requirements-dev.txt
  81. - name: Run tests
  82. timeout-minutes: 10
  83. run: |
  84. cd backend
  85. python -m pytest tests/ -v --tb=short --timeout=60 --timeout-method=thread -n auto
  86. # ============================================================================
  87. # Frontend Checks
  88. # ============================================================================
  89. frontend-lint:
  90. name: Frontend Lint
  91. runs-on: ubuntu-latest
  92. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  93. steps:
  94. - uses: actions/checkout@v4
  95. - name: Set up Node.js
  96. uses: actions/setup-node@v4
  97. with:
  98. node-version: ${{ env.NODE_VERSION }}
  99. cache: 'npm'
  100. cache-dependency-path: frontend/package-lock.json
  101. - name: Install dependencies
  102. working-directory: frontend
  103. run: npm ci
  104. - name: Run ESLint
  105. working-directory: frontend
  106. run: npm run lint
  107. frontend-security:
  108. name: Frontend Security
  109. runs-on: ubuntu-latest
  110. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  111. continue-on-error: true
  112. steps:
  113. - uses: actions/checkout@v4
  114. - name: Set up Node.js
  115. uses: actions/setup-node@v4
  116. with:
  117. node-version: ${{ env.NODE_VERSION }}
  118. cache: 'npm'
  119. cache-dependency-path: frontend/package-lock.json
  120. - name: Install dependencies
  121. working-directory: frontend
  122. run: npm ci
  123. - name: Run npm audit
  124. working-directory: frontend
  125. run: |
  126. # Only audit production dependencies and filter out npm-internal packages.
  127. # npm 10.x audit/ls reports vulns in its own bundled deps (npm, tar, minimatch)
  128. # so we parse package-lock.json directly to get the real prod dep list.
  129. npm audit --omit=dev --json > /tmp/audit.json 2>/dev/null || true
  130. python3 -c "
  131. import json, sys
  132. data = json.load(open('/tmp/audit.json'))
  133. lock = json.load(open('package-lock.json'))
  134. prod = set()
  135. for path, info in lock.get('packages', {}).items():
  136. if path and not info.get('dev') and not info.get('devOptional'):
  137. prod.add(path.split('node_modules/')[-1])
  138. vulns = data.get('vulnerabilities', {})
  139. fixable = {n: v for n, v in vulns.items()
  140. if n in prod and v.get('severity') in ('high', 'critical') and v.get('fixAvailable')}
  141. skipped = len(vulns) - len({n: v for n, v in vulns.items() if n in prod})
  142. if fixable:
  143. for name, v in fixable.items():
  144. print(f'FIXABLE {v[\"severity\"].upper()}: {name}')
  145. sys.exit(1)
  146. total = sum(1 for n, v in vulns.items() if n in prod and v.get('severity') in ('high', 'critical'))
  147. print(f'npm audit: {total} high/critical (0 fixable), {len(vulns)} total ({skipped} npm-internal filtered)')
  148. "
  149. frontend-typecheck:
  150. name: Frontend Type Check
  151. runs-on: ubuntu-latest
  152. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  153. steps:
  154. - uses: actions/checkout@v4
  155. - name: Set up Node.js
  156. uses: actions/setup-node@v4
  157. with:
  158. node-version: ${{ env.NODE_VERSION }}
  159. cache: 'npm'
  160. cache-dependency-path: frontend/package-lock.json
  161. - name: Install dependencies
  162. working-directory: frontend
  163. run: npm ci
  164. - name: Run TypeScript check
  165. working-directory: frontend
  166. run: npx tsc --noEmit
  167. frontend-tests:
  168. name: Frontend Tests
  169. runs-on: ubuntu-latest
  170. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  171. needs: [frontend-lint, frontend-typecheck]
  172. steps:
  173. - uses: actions/checkout@v4
  174. - name: Set up Node.js
  175. uses: actions/setup-node@v4
  176. with:
  177. node-version: ${{ env.NODE_VERSION }}
  178. cache: 'npm'
  179. cache-dependency-path: frontend/package-lock.json
  180. - name: Install dependencies
  181. working-directory: frontend
  182. run: npm ci
  183. - name: Run tests
  184. timeout-minutes: 10
  185. working-directory: frontend
  186. run: npm run test:run
  187. frontend-build:
  188. name: Frontend Build
  189. runs-on: ubuntu-latest
  190. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  191. needs: [frontend-tests]
  192. steps:
  193. - uses: actions/checkout@v4
  194. - name: Set up Node.js
  195. uses: actions/setup-node@v4
  196. with:
  197. node-version: ${{ env.NODE_VERSION }}
  198. cache: 'npm'
  199. cache-dependency-path: frontend/package-lock.json
  200. - name: Install dependencies
  201. working-directory: frontend
  202. run: npm ci
  203. - name: Build
  204. working-directory: frontend
  205. run: npm run build
  206. # ============================================================================
  207. # Docker Tests (matches test_docker.sh)
  208. # ============================================================================
  209. docker-test:
  210. name: Docker Build
  211. runs-on: ubuntu-latest
  212. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  213. timeout-minutes: 20
  214. needs: [backend-tests, frontend-build]
  215. steps:
  216. - uses: actions/checkout@v4
  217. # Test 1: Docker Build
  218. - name: Build production image
  219. run: docker build -t bambuddy:test .
  220. - name: Verify backend imports
  221. run: docker run --rm bambuddy:test python -c "import backend.app.main; print('Backend imports OK')"
  222. - name: Verify static files exist
  223. run: docker run --rm bambuddy:test test -d /app/static
  224. # Test 2: Backend Unit Tests in Docker
  225. - name: Build backend test image
  226. run: docker compose -f docker-compose.test.yml build backend-test
  227. - name: Run backend tests in Docker
  228. run: docker compose -f docker-compose.test.yml run --rm backend-test
  229. # Test 3: Frontend Unit Tests in Docker
  230. - name: Build frontend test image
  231. run: docker compose -f docker-compose.test.yml build frontend-test
  232. - name: Run frontend tests in Docker
  233. run: docker compose -f docker-compose.test.yml run --rm frontend-test
  234. # Test 4: Integration Tests
  235. - name: Build integration container
  236. run: docker compose -f docker-compose.test.yml build integration
  237. - name: Start integration container
  238. run: |
  239. docker compose -f docker-compose.test.yml up -d integration
  240. echo "Waiting for container to be healthy..."
  241. for i in {1..30}; do
  242. if docker compose -f docker-compose.test.yml ps integration | grep -q "healthy"; then
  243. echo "Container is healthy"
  244. break
  245. fi
  246. sleep 2
  247. done
  248. - name: Test health endpoint
  249. run: |
  250. HEALTH=$(docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/health)
  251. echo "$HEALTH"
  252. echo "$HEALTH" | grep -q "healthy"
  253. - name: Test API endpoint
  254. run: |
  255. docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/api/v1/settings
  256. - name: Test static files served
  257. run: |
  258. STATUS=$(docker compose -f docker-compose.test.yml exec -T integration curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/)
  259. echo "Static files HTTP status: $STATUS"
  260. [ "$STATUS" = "200" ]
  261. # Test 5: Integration Test Suite (pytest)
  262. - name: Build integration test runner
  263. run: docker compose -f docker-compose.test.yml build integration-test-runner
  264. - name: Run integration test suite
  265. run: docker compose -f docker-compose.test.yml run --rm integration-test-runner
  266. - name: Show logs on failure
  267. if: failure()
  268. run: docker compose -f docker-compose.test.yml logs
  269. - name: Cleanup
  270. if: always()
  271. run: docker compose -f docker-compose.test.yml down -v --remove-orphans