ci.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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: '20'
  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 — dev dependency vulnerabilities
  127. # don't affect end users. Detailed audits are handled by security.yml.
  128. npm audit --omit=dev --json > /tmp/audit.json 2>/dev/null || true
  129. python3 -c "
  130. import json, sys
  131. data = json.load(open('/tmp/audit.json'))
  132. vulns = data.get('vulnerabilities', {})
  133. fixable = {n: v for n, v in vulns.items()
  134. if v.get('severity') in ('high', 'critical') and v.get('fixAvailable')}
  135. if fixable:
  136. for name, v in fixable.items():
  137. print(f'FIXABLE {v[\"severity\"].upper()}: {name}')
  138. sys.exit(1)
  139. total = sum(1 for v in vulns.values() if v.get('severity') in ('high', 'critical'))
  140. print(f'npm audit: {total} high/critical (0 fixable), {len(vulns)} total known')
  141. "
  142. frontend-typecheck:
  143. name: Frontend Type Check
  144. runs-on: ubuntu-latest
  145. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  146. steps:
  147. - uses: actions/checkout@v4
  148. - name: Set up Node.js
  149. uses: actions/setup-node@v4
  150. with:
  151. node-version: ${{ env.NODE_VERSION }}
  152. cache: 'npm'
  153. cache-dependency-path: frontend/package-lock.json
  154. - name: Install dependencies
  155. working-directory: frontend
  156. run: npm ci
  157. - name: Run TypeScript check
  158. working-directory: frontend
  159. run: npx tsc --noEmit
  160. frontend-tests:
  161. name: Frontend Tests
  162. runs-on: ubuntu-latest
  163. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  164. needs: [frontend-lint, frontend-typecheck]
  165. steps:
  166. - uses: actions/checkout@v4
  167. - name: Set up Node.js
  168. uses: actions/setup-node@v4
  169. with:
  170. node-version: ${{ env.NODE_VERSION }}
  171. cache: 'npm'
  172. cache-dependency-path: frontend/package-lock.json
  173. - name: Install dependencies
  174. working-directory: frontend
  175. run: npm ci
  176. - name: Run tests
  177. timeout-minutes: 10
  178. working-directory: frontend
  179. run: npm run test:run
  180. frontend-build:
  181. name: Frontend Build
  182. runs-on: ubuntu-latest
  183. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  184. needs: [frontend-tests]
  185. steps:
  186. - uses: actions/checkout@v4
  187. - name: Set up Node.js
  188. uses: actions/setup-node@v4
  189. with:
  190. node-version: ${{ env.NODE_VERSION }}
  191. cache: 'npm'
  192. cache-dependency-path: frontend/package-lock.json
  193. - name: Install dependencies
  194. working-directory: frontend
  195. run: npm ci
  196. - name: Build
  197. working-directory: frontend
  198. run: npm run build
  199. # ============================================================================
  200. # Docker Tests (matches test_docker.sh)
  201. # ============================================================================
  202. docker-test:
  203. name: Docker Build
  204. runs-on: ubuntu-latest
  205. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  206. timeout-minutes: 20
  207. needs: [backend-tests, frontend-build]
  208. steps:
  209. - uses: actions/checkout@v4
  210. # Test 1: Docker Build
  211. - name: Build production image
  212. run: docker build -t bambuddy:test .
  213. - name: Verify backend imports
  214. run: docker run --rm bambuddy:test python -c "import backend.app.main; print('Backend imports OK')"
  215. - name: Verify static files exist
  216. run: docker run --rm bambuddy:test test -d /app/static
  217. # Test 2: Backend Unit Tests in Docker
  218. - name: Build backend test image
  219. run: docker compose -f docker-compose.test.yml build backend-test
  220. - name: Run backend tests in Docker
  221. run: docker compose -f docker-compose.test.yml run --rm backend-test
  222. # Test 3: Frontend Unit Tests in Docker
  223. - name: Build frontend test image
  224. run: docker compose -f docker-compose.test.yml build frontend-test
  225. - name: Run frontend tests in Docker
  226. run: docker compose -f docker-compose.test.yml run --rm frontend-test
  227. # Test 4: Integration Tests
  228. - name: Build integration container
  229. run: docker compose -f docker-compose.test.yml build integration
  230. - name: Start integration container
  231. run: |
  232. docker compose -f docker-compose.test.yml up -d integration
  233. echo "Waiting for container to be healthy..."
  234. for i in {1..30}; do
  235. if docker compose -f docker-compose.test.yml ps integration | grep -q "healthy"; then
  236. echo "Container is healthy"
  237. break
  238. fi
  239. sleep 2
  240. done
  241. - name: Test health endpoint
  242. run: |
  243. HEALTH=$(docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/health)
  244. echo "$HEALTH"
  245. echo "$HEALTH" | grep -q "healthy"
  246. - name: Test API endpoint
  247. run: |
  248. docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/api/v1/settings
  249. - name: Test static files served
  250. run: |
  251. STATUS=$(docker compose -f docker-compose.test.yml exec -T integration curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/)
  252. echo "Static files HTTP status: $STATUS"
  253. [ "$STATUS" = "200" ]
  254. # Test 5: Integration Test Suite (pytest)
  255. - name: Build integration test runner
  256. run: docker compose -f docker-compose.test.yml build integration-test-runner
  257. - name: Run integration test suite
  258. run: docker compose -f docker-compose.test.yml run --rm integration-test-runner
  259. - name: Show logs on failure
  260. if: failure()
  261. run: docker compose -f docker-compose.test.yml logs
  262. - name: Cleanup
  263. if: always()
  264. run: docker compose -f docker-compose.test.yml down -v --remove-orphans