-
Notifications
You must be signed in to change notification settings - Fork 1
672 lines (570 loc) Β· 22.7 KB
/
ci-cd.yml
File metadata and controls
672 lines (570 loc) Β· 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
name: CI/CD Pipeline
on:
push:
branches:
- main
- develop
tags:
- 'v*'
pull_request:
branches:
- main
- develop
release:
types: [published]
workflow_dispatch:
inputs:
ignore_scan_errors:
description: 'Ignore container scan errors (registry access issues)'
required: false
default: false
type: boolean
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ============================================================================
# SECURITY SCANNING (runs first, fast fail)
# ============================================================================
security:
name: Security Scan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install security tools
run: |
pip install bandit safety
- name: Run bandit (Python security)
run: |
bandit -r apps/backend/src -f json -o bandit-report.json || true
bandit -r apps/backend/src --severity-level medium
- name: Run safety (Python vulnerabilities)
run: |
safety check --file apps/backend/requirements.txt --json || true
safety check --file apps/backend/requirements.txt
continue-on-error: true
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Run npm audit (Frontend vulnerabilities)
working-directory: apps/frontend
run: |
npm audit --audit-level=moderate || true
npm audit --production --audit-level=high
continue-on-error: true
# ============================================================================
# CODE FORMATTING (runs parallel with security)
# ============================================================================
format:
name: Check Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install black
run: pip install black==26.1.0
- name: Check Python formatting
run: |
black --check apps/backend/
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Install frontend dependencies
run: npm ci --prefer-offline
- name: Check TypeScript/React formatting
working-directory: apps/frontend
run: npx prettier --check "src/**/*.{js,jsx,ts,tsx,json,css}"
# ============================================================================
# LINTING (runs after security/format)
# ============================================================================
lint:
name: Lint Code
runs-on: ubuntu-latest
needs: [security, format]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install linting tools
run: |
pip install ruff mypy
- name: Run ruff (Python linter)
run: |
ruff check apps/backend/ --select=E,F,W --ignore=E501
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Install frontend dependencies
run: npm ci --prefer-offline
- name: Run ESLint (TypeScript/React linter)
working-directory: apps/frontend
run: npm run lint
# ============================================================================
# BACKEND TESTS (runs parallel with lint)
# ============================================================================
backend-tests:
name: Backend Tests
runs-on: ubuntu-latest
needs: [security, format]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Cache Python dependencies
uses: actions/cache@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('apps/backend/requirements-dev.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install -e apps/backend
pip install -r apps/backend/requirements-dev.txt
- name: Run tests with coverage
run: |
cd apps/backend
pytest --cov=opencloudtouch --cov-report=xml --cov-report=json --cov-report=term-missing --cov-fail-under=80
env:
OCT_MOCK_MODE: "true"
OCT_HAS_DEVICES: "false"
CI: "true"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./.out/coverage/backend/coverage.xml
flags: backend
name: backend-coverage
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage JSON (for summary)
uses: actions/upload-artifact@v7
if: always()
with:
name: backend-coverage
path: .out/coverage/backend/coverage.json
retention-days: 1
# ============================================================================
# FRONTEND TESTS (runs parallel with backend-tests)
# ============================================================================
frontend-tests:
name: Frontend Tests
runs-on: ubuntu-latest
needs: [security, format]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Install dependencies
run: |
npm ci
npm install @rollup/rollup-linux-x64-gnu --save-optional
- name: Build frontend
working-directory: apps/frontend
run: npm run build
- name: Run unit tests with coverage
working-directory: apps/frontend
run: npm run test:coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./.out/coverage/frontend/lcov.info
flags: frontend
name: frontend-coverage
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage JSON (for summary)
uses: actions/upload-artifact@v7
if: always()
with:
name: frontend-coverage
path: .out/coverage/frontend/coverage-summary.json
retention-days: 1
- name: Upload frontend build (for Docker)
uses: actions/upload-artifact@v7
with:
name: frontend-dist
path: .out/dist/
retention-days: 1
# ============================================================================
# E2E TESTS (runs parallel with unit tests after security/format)
# ============================================================================
e2e-tests:
name: E2E Tests
runs-on: ubuntu-latest
needs: [security, format]
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Cache Python dependencies
uses: actions/cache@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('apps/backend/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install backend dependencies
run: pip install -e apps/backend
- name: Install frontend dependencies
run: |
npm ci
npm install @rollup/rollup-linux-x64-gnu --save-optional
- name: Build frontend
working-directory: apps/frontend
run: npm run build
- name: Start backend server
run: |
cd apps/backend
OCT_MOCK_MODE=true OCT_LOG_LEVEL=WARNING OCT_ALLOW_DANGEROUS_OPERATIONS=true python -m uvicorn opencloudtouch.main:app --host 0.0.0.0 --port 7778 &
sleep 10
curl --retry 10 --retry-delay 2 --retry-connrefused http://localhost:7778/health
- name: Start frontend preview server
run: |
cd apps/frontend
npm run preview -- --port 4173 --strictPort &
sleep 5
curl --retry 10 --retry-delay 2 --retry-connrefused http://localhost:4173
- name: Run Cypress E2E tests
working-directory: apps/frontend
run: npm run test:e2e
continue-on-error: true # E2E tests may fail due to environment differences
env:
CYPRESS_BASE_URL: http://localhost:4173
CYPRESS_API_URL: http://localhost:7778/api
- name: Upload Cypress screenshots on failure
uses: actions/upload-artifact@v7
if: failure()
with:
name: cypress-screenshots
path: apps/frontend/tests/e2e/screenshots
retention-days: 7
- name: Upload Cypress videos on failure
uses: actions/upload-artifact@v7
if: failure()
with:
name: cypress-videos
path: apps/frontend/tests/e2e/videos
retention-days: 7
# ============================================================================
# DOCKER SECURITY SCAN (gate before multi-arch build - BUILD-04)
# ============================================================================
docker-security:
name: Docker Security Scan
runs-on: ubuntu-latest
needs: [e2e-tests, frontend-tests]
if: (github.event_name == 'push' || github.event_name == 'release') && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Download pre-built frontend
uses: actions/download-artifact@v8
with:
name: frontend-dist
path: .out/dist
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build test image for scanning
uses: docker/build-push-action@v7
with:
context: .
file: deployment/Dockerfile
platforms: linux/amd64
push: false
load: true
tags: opencloudtouch:test
cache-from: type=gha,scope=trivy-scan
cache-to: type=gha,mode=max,scope=trivy-scan
- name: Run Trivy vulnerability scanner (blocking)
uses: aquasecurity/trivy-action@master
with:
image-ref: 'opencloudtouch:test'
format: 'table'
severity: 'HIGH,CRITICAL'
exit-code: '1' # Fail build on HIGH/CRITICAL vulnerabilities
ignore-unfixed: true
- name: Run Trivy with JSON output for artifact
uses: aquasecurity/trivy-action@master
if: always()
with:
image-ref: 'opencloudtouch:test'
format: 'json'
output: 'trivy-results.json'
severity: 'HIGH,CRITICAL,MEDIUM,LOW'
- name: Upload Trivy scan results
uses: actions/upload-artifact@v7
if: always()
with:
name: trivy-scan-results
path: trivy-results.json
retention-days: 30
# ============================================================================
# DOCKER BUILD (parallel multi-arch, AFTER security scan passes)
# ============================================================================
build:
name: Build Docker Image (${{ matrix.platform }})
runs-on: ubuntu-latest
needs: [e2e-tests, docker-security, frontend-tests] # Only build if tests AND security scan pass
if: (github.event_name == 'push' || github.event_name == 'release') && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
strategy:
matrix:
platform: [linux/amd64, linux/arm64, linux/arm/v7]
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Download pre-built frontend
uses: actions/download-artifact@v8
with:
name: frontend-dist
path: .out/dist
- name: Set up QEMU
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=sha,prefix=sha-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v7
with:
context: .
file: deployment/Dockerfile
platforms: ${{ matrix.platform }}
push: true
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=build-${{ matrix.platform }}
cache-to: type=gha,mode=max,scope=build-${{ matrix.platform }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,annotation-index.org.opencontainers.image.description=OpenCloudTouch for ${{ matrix.platform }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v7
with:
name: digests-${{ strategy.job-index }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
# ============================================================================
# DOCKER PUSH (merge digests and push multi-arch manifest)
# ============================================================================
push:
name: Push Docker Image
runs-on: ubuntu-latest
needs: [build, e2e-tests]
if: (github.event_name == 'push' || github.event_name == 'release') && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
permissions:
contents: read
packages: write
steps:
- name: Download digests
uses: actions/download-artifact@v8
with:
pattern: digests-*
path: /tmp/digests
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=sha,prefix=sha-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
# ============================================================================
# CONTAINER SECURITY SCAN (runs after push)
# ============================================================================
container-scan:
name: Scan Container for Vulnerabilities
runs-on: ubuntu-latest
needs: [push]
if: (github.event_name == 'push' || github.event_name == 'release') && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
permissions:
contents: read
security-events: write # Required for uploading SARIF
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Extract metadata (for tag)
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
continue-on-error: ${{ inputs.ignore_scan_errors == true }}
with:
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always() && hashFiles('trivy-results.sarif') != ''
with:
sarif_file: 'trivy-results.sarif'
- name: Run Trivy vulnerability scanner (table output)
uses: aquasecurity/trivy-action@master
continue-on-error: ${{ inputs.ignore_scan_errors == true }}
with:
image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
format: 'table'
severity: 'CRITICAL,HIGH,MEDIUM'
# ============================================================================
# BUILD SUMMARY (runs always at the end)
# ============================================================================
summary:
name: Build Summary
runs-on: ubuntu-latest
needs: [security, format, lint, backend-tests, frontend-tests, e2e-tests, container-scan]
if: always()
steps:
- name: Download backend coverage
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: backend-coverage
path: ./coverage
- name: Download frontend coverage
uses: actions/download-artifact@v8
continue-on-error: true
with:
name: frontend-coverage
path: ./coverage
- name: Parse coverage and generate summary
run: |
echo "# π CI/CD Pipeline Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Job status overview
echo "## π Job Status" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Job | Status |" >> $GITHUB_STEP_SUMMARY
echo "|-----|--------|" >> $GITHUB_STEP_SUMMARY
echo "| Security Scan | ${{ needs.security.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| Format Check | ${{ needs.format.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| Lint | ${{ needs.lint.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| Backend Tests | ${{ needs.backend-tests.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| Frontend Tests | ${{ needs.frontend-tests.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| E2E Tests | ${{ needs.e2e-tests.result == 'success' && 'β
Passed' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "| Container Scan | ${{ needs.container-scan.result == 'success' && 'β
Passed' || needs.container-scan.result == 'skipped' && 'βοΈ Skipped' || 'β Failed' }} |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Coverage
echo "## π Test Coverage" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Backend coverage
if [ -f ./coverage/coverage.json ]; then
BACKEND_COV=$(python3 -c "import json; data=json.load(open('./coverage/coverage.json')); print(f\"{data['totals']['percent_covered']:.1f}%\")" 2>/dev/null || echo "N/A")
echo "**Backend:** $BACKEND_COV" >> $GITHUB_STEP_SUMMARY
else
echo "**Backend:** Coverage data not available" >> $GITHUB_STEP_SUMMARY
fi
# Frontend coverage
if [ -f ./coverage/coverage-summary.json ]; then
FRONTEND_COV=$(python3 -c "import json; data=json.load(open('./coverage/coverage-summary.json')); total=data.get('total', {}); lines=total.get('lines', {}); print(f\"{lines.get('pct', 0):.1f}%\")" 2>/dev/null || echo "N/A")
echo "**Frontend:** $FRONTEND_COV" >> $GITHUB_STEP_SUMMARY
else
echo "**Frontend:** Coverage data not available" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "_π‘ Tip: Click on job names above to see detailed results_" >> $GITHUB_STEP_SUMMARY
- name: Check overall status
run: |
if [[ "${{ needs.security.result }}" != "success" ]] || \
[[ "${{ needs.format.result }}" != "success" ]] || \
[[ "${{ needs.lint.result }}" != "success" ]] || \
[[ "${{ needs.backend-tests.result }}" != "success" ]] || \
[[ "${{ needs.frontend-tests.result }}" != "success" ]] || \
[[ "${{ needs.e2e-tests.result }}" != "success" ]]; then
echo "β Some jobs failed - see summary above" >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "β
All jobs passed successfully!" >> $GITHUB_STEP_SUMMARY
fi