ci.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. name: CI
  2. on:
  3. push:
  4. branches: [main]
  5. pull_request:
  6. branches: [main]
  7. workflow_dispatch:
  8. # Run on PRs targeting main, but skip for repo owner (runs local tests)
  9. # Skip CI for PRs authored by repo owner (they run tests locally)
  10. # Uses PR author instead of triggering actor so rebasing by owner doesn't skip CI
  11. env:
  12. PYTHON_VERSION: '3.11'
  13. NODE_VERSION: '22'
  14. # Cancel in-progress runs for the same branch
  15. concurrency:
  16. group: ${{ github.workflow }}-${{ github.ref }}
  17. cancel-in-progress: true
  18. # Minimum permissions for all jobs
  19. permissions:
  20. contents: read
  21. jobs:
  22. # ============================================================================
  23. # Backend Checks
  24. # ============================================================================
  25. backend-lint:
  26. name: Backend Lint
  27. runs-on: ubuntu-latest
  28. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  29. steps:
  30. - uses: actions/checkout@v6
  31. - name: Set up Python
  32. uses: actions/setup-python@v6
  33. with:
  34. python-version: ${{ env.PYTHON_VERSION }}
  35. - name: Install ruff
  36. # Install the exact pin from requirements-dev.txt rather than the latest
  37. # release, so CI and contributors run the same linter. `pip install ruff`
  38. # silently drifted ahead of every local venv.
  39. run: pip install "$(grep -E '^ruff==' requirements-dev.txt)"
  40. - name: Run ruff check
  41. run: ruff check backend/
  42. - name: Run ruff format check
  43. run: ruff format --check backend/
  44. backend-security:
  45. name: Backend Security
  46. runs-on: ubuntu-latest
  47. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  48. continue-on-error: true
  49. steps:
  50. - uses: actions/checkout@v6
  51. - name: Set up Python
  52. uses: actions/setup-python@v6
  53. with:
  54. python-version: ${{ env.PYTHON_VERSION }}
  55. - name: Install dependencies
  56. run: |
  57. python -m pip install --upgrade pip
  58. pip install -r requirements.txt
  59. pip install pip-audit
  60. - name: Run pip-audit
  61. run: |
  62. # CVE-2026-4539: low-severity ReDoS in Pygments AdlLexer (indirect dep via mkdocs-material/pytest/rich).
  63. # No fix available yet. Remove --ignore-vuln once Pygments releases a patched version.
  64. #
  65. # CVE-2025-45768 (PYSEC-2025-183 / GHSA-65pc-fj4g-8rjx): disputed by PyJWT maintainers.
  66. # Advisory says "key length is chosen by the application that uses the library" — no
  67. # PyJWT fix exists or will exist. Bambuddy is safe: backend/app/core/auth.py:184 uses
  68. # secrets.token_urlsafe(64) (~86 chars of entropy) for auto-generated secrets and
  69. # rejects file-loaded secrets shorter than 32 chars at :177. Keep ignored permanently.
  70. pip-audit --desc on \
  71. --ignore-vuln CVE-2026-4539 \
  72. --ignore-vuln CVE-2025-45768
  73. backend-tests:
  74. name: Backend Tests (shard ${{ matrix.shard }}/4)
  75. runs-on: ubuntu-latest
  76. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  77. needs: backend-lint
  78. strategy:
  79. # Don't cancel sibling shards if one fails — we want every shard's
  80. # failure list, not just the first one, so a single PR push shows
  81. # all broken tests in one go.
  82. fail-fast: false
  83. matrix:
  84. shard: [1, 2, 3, 4]
  85. steps:
  86. - uses: actions/checkout@v6
  87. - name: Set up Python
  88. uses: actions/setup-python@v6
  89. with:
  90. python-version: ${{ env.PYTHON_VERSION }}
  91. - name: Cache pip
  92. uses: actions/cache@v5
  93. with:
  94. path: ~/.cache/pip
  95. key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
  96. restore-keys: |
  97. ${{ runner.os }}-pip-
  98. - name: Install dependencies
  99. run: |
  100. python -m pip install --upgrade pip
  101. pip install -r requirements.txt
  102. pip install -r requirements-dev.txt
  103. - name: Run tests (shard ${{ matrix.shard }}/4)
  104. timeout-minutes: 10
  105. run: |
  106. cd backend
  107. # -v dropped: 5300+ "PASSED foo::bar" lines per worker eat 30-60s
  108. # of stdout I/O time on 2-vCPU runners. --tb=short is enough.
  109. # --splits 4 --group N uses pytest-split to slice the collected
  110. # test set roughly evenly across the 4 matrix shards; first run
  111. # is name-hash-based, subsequent runs improve via .test_durations
  112. # if you ever commit one (we don't — even the naive hash split
  113. # gets us ≈25% per shard given the test mix here).
  114. python -m pytest tests/ \
  115. --tb=short \
  116. --timeout=60 --timeout-method=thread \
  117. -n auto \
  118. --splits 4 --group ${{ matrix.shard }}
  119. # ============================================================================
  120. # Frontend Checks
  121. # ============================================================================
  122. frontend-lint:
  123. name: Frontend Lint
  124. runs-on: ubuntu-latest
  125. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  126. steps:
  127. - uses: actions/checkout@v6
  128. - name: Set up Node.js
  129. uses: actions/setup-node@v6
  130. with:
  131. node-version: ${{ env.NODE_VERSION }}
  132. cache: 'npm'
  133. cache-dependency-path: frontend/package-lock.json
  134. - name: Install dependencies
  135. working-directory: frontend
  136. run: npm ci
  137. - name: Run ESLint
  138. working-directory: frontend
  139. run: npm run lint
  140. frontend-security:
  141. name: Frontend Security
  142. runs-on: ubuntu-latest
  143. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  144. continue-on-error: true
  145. steps:
  146. - uses: actions/checkout@v6
  147. - name: Set up Node.js
  148. uses: actions/setup-node@v6
  149. with:
  150. node-version: ${{ env.NODE_VERSION }}
  151. cache: 'npm'
  152. cache-dependency-path: frontend/package-lock.json
  153. - name: Install dependencies
  154. working-directory: frontend
  155. run: npm ci
  156. - name: Run npm audit
  157. working-directory: frontend
  158. run: |
  159. # Only audit production dependencies and filter out npm-internal packages.
  160. # npm 10.x audit/ls reports vulns in its own bundled deps (npm, tar, minimatch)
  161. # so we parse package-lock.json directly to get the real prod dep list.
  162. npm audit --omit=dev --json > /tmp/audit.json 2>/dev/null || true
  163. python3 -c "
  164. import json, sys
  165. data = json.load(open('/tmp/audit.json'))
  166. lock = json.load(open('package-lock.json'))
  167. prod = set()
  168. for path, info in lock.get('packages', {}).items():
  169. if path and not info.get('dev') and not info.get('devOptional'):
  170. prod.add(path.split('node_modules/')[-1])
  171. vulns = data.get('vulnerabilities', {})
  172. fixable = {n: v for n, v in vulns.items()
  173. if n in prod and v.get('severity') in ('high', 'critical') and v.get('fixAvailable')}
  174. skipped = len(vulns) - len({n: v for n, v in vulns.items() if n in prod})
  175. if fixable:
  176. for name, v in fixable.items():
  177. print(f'FIXABLE {v[\"severity\"].upper()}: {name}')
  178. sys.exit(1)
  179. total = sum(1 for n, v in vulns.items() if n in prod and v.get('severity') in ('high', 'critical'))
  180. print(f'npm audit: {total} high/critical (0 fixable), {len(vulns)} total ({skipped} npm-internal filtered)')
  181. "
  182. frontend-typecheck:
  183. name: Frontend Type Check
  184. runs-on: ubuntu-latest
  185. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  186. steps:
  187. - uses: actions/checkout@v6
  188. - name: Set up Node.js
  189. uses: actions/setup-node@v6
  190. with:
  191. node-version: ${{ env.NODE_VERSION }}
  192. cache: 'npm'
  193. cache-dependency-path: frontend/package-lock.json
  194. - name: Install dependencies
  195. working-directory: frontend
  196. run: npm ci
  197. - name: Run TypeScript check
  198. working-directory: frontend
  199. run: npx tsc --noEmit
  200. frontend-tests:
  201. name: Frontend Tests
  202. runs-on: ubuntu-latest
  203. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  204. needs: [frontend-lint, frontend-typecheck]
  205. steps:
  206. - uses: actions/checkout@v6
  207. - name: Set up Node.js
  208. uses: actions/setup-node@v6
  209. with:
  210. node-version: ${{ env.NODE_VERSION }}
  211. cache: 'npm'
  212. cache-dependency-path: frontend/package-lock.json
  213. - name: Install dependencies
  214. working-directory: frontend
  215. run: npm ci
  216. - name: Run tests
  217. timeout-minutes: 10
  218. working-directory: frontend
  219. run: npm run test:run
  220. frontend-build:
  221. name: Frontend Build
  222. runs-on: ubuntu-latest
  223. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  224. needs: [frontend-tests]
  225. steps:
  226. - uses: actions/checkout@v6
  227. - name: Set up Node.js
  228. uses: actions/setup-node@v6
  229. with:
  230. node-version: ${{ env.NODE_VERSION }}
  231. cache: 'npm'
  232. cache-dependency-path: frontend/package-lock.json
  233. - name: Install dependencies
  234. working-directory: frontend
  235. run: npm ci
  236. - name: Build
  237. working-directory: frontend
  238. run: npm run build
  239. # ============================================================================
  240. # Docker Tests (matches test_docker.sh)
  241. # ============================================================================
  242. # Run the FULL backend test suite inside the test image, sharded 4-way
  243. # so wall-clock matches the host-side backend-tests job. Catches the
  244. # rare-but-real cases where a test passes on the GHA host but fails in
  245. # the python:3.13-slim test image (system-binary version differences,
  246. # locale/timezone, container vs host user, cwd assumptions). Without
  247. # sharding this was a 5-10 min single-runner job; with sharding it's
  248. # ~120-150s per shard running in parallel, gated by max(shard).
  249. docker-backend-tests:
  250. name: Docker Backend Tests (shard ${{ matrix.shard }}/4)
  251. runs-on: ubuntu-latest
  252. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  253. timeout-minutes: 15
  254. strategy:
  255. fail-fast: false
  256. matrix:
  257. shard: [1, 2, 3, 4]
  258. steps:
  259. - uses: actions/checkout@v6
  260. - name: Set up Docker Buildx
  261. uses: docker/setup-buildx-action@v4
  262. # Build the backend-test image with GHA BuildKit cache backend so
  263. # the pip-install layer is shared across the 4 matrix shards AND
  264. # across CI runs. First run on a given requirements.txt is cold
  265. # (~60-90s); subsequent runs are ~5-10s.
  266. - name: Build backend test image (cached)
  267. uses: docker/build-push-action@v7
  268. with:
  269. context: .
  270. file: Dockerfile.test
  271. target: backend-test
  272. load: true
  273. tags: bambuddy-backend-test:latest
  274. cache-from: type=gha,scope=backend-test
  275. cache-to: type=gha,scope=backend-test,mode=max
  276. - name: Run backend tests in Docker (shard ${{ matrix.shard }}/4)
  277. run: |
  278. docker run --rm \
  279. -e TESTING=1 \
  280. -e PYTHONUNBUFFERED=1 \
  281. bambuddy-backend-test:latest \
  282. pytest backend/tests/ \
  283. --tb=short \
  284. --timeout=60 --timeout-method=thread \
  285. -p no:cacheprovider \
  286. -n auto \
  287. --splits 4 --group ${{ matrix.shard }}
  288. docker-test:
  289. name: Docker Build
  290. runs-on: ubuntu-latest
  291. if: github.event_name == 'push' || github.event.pull_request.user.login != github.repository_owner
  292. timeout-minutes: 20
  293. needs: [backend-tests, frontend-build]
  294. steps:
  295. - uses: actions/checkout@v6
  296. # Test 1: Docker Build
  297. - name: Build production image
  298. run: docker build -t bambuddy:test .
  299. - name: Verify backend imports
  300. run: docker run --rm bambuddy:test python -c "import backend.app.main; print('Backend imports OK')"
  301. - name: Verify static files exist
  302. run: docker run --rm bambuddy:test test -d /app/static
  303. # Test 4: Integration Tests
  304. - name: Build integration container
  305. run: docker compose -f docker-compose.test.yml build integration
  306. - name: Start integration container
  307. run: |
  308. docker compose -f docker-compose.test.yml up -d integration
  309. echo "Waiting for container to be healthy..."
  310. for i in {1..30}; do
  311. if docker compose -f docker-compose.test.yml ps integration | grep -q "healthy"; then
  312. echo "Container is healthy"
  313. break
  314. fi
  315. sleep 2
  316. done
  317. - name: Test health endpoint
  318. run: |
  319. HEALTH=$(docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/health)
  320. echo "$HEALTH"
  321. echo "$HEALTH" | grep -q "healthy"
  322. - name: Test API endpoint
  323. run: |
  324. docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:8000/api/v1/settings
  325. - name: Test static files served
  326. run: |
  327. STATUS=$(docker compose -f docker-compose.test.yml exec -T integration curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/)
  328. echo "Static files HTTP status: $STATUS"
  329. [ "$STATUS" = "200" ]
  330. # Test 5: Integration Test Suite (pytest)
  331. - name: Build integration test runner
  332. run: docker compose -f docker-compose.test.yml build integration-test-runner
  333. - name: Run integration test suite
  334. run: docker compose -f docker-compose.test.yml run --rm integration-test-runner
  335. - name: Show logs on failure
  336. if: failure()
  337. run: docker compose -f docker-compose.test.yml logs
  338. - name: Cleanup
  339. if: always()
  340. run: docker compose -f docker-compose.test.yml down -v --remove-orphans