forked from thrixxy-technologies/flavorsnap
-
Notifications
You must be signed in to change notification settings - Fork 0
617 lines (523 loc) · 17 KB
/
Copy pathci-cd-pipeline.yml
File metadata and controls
617 lines (523 loc) · 17 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
name: Advanced CI/CD Pipeline
on:
push:
branches: [ main, develop, feature/* ]
pull_request:
branches: [ main, develop ]
schedule:
# Daily at 2 AM UTC
- cron: '0 2 * * *'
workflow_dispatch:
inputs:
run_security_scan:
description: 'Run security scanning'
required: false
default: 'true'
type: boolean
run_performance_tests:
description: 'Run performance tests'
required: false
default: 'true'
type: boolean
deploy_to_staging:
description: 'Deploy to staging environment'
required: false
default: 'false'
type: boolean
env:
NODE_VERSION: '18'
PYTHON_VERSION: '3.9'
GO_VERSION: '1.21'
jobs:
# Code Quality and Static Analysis
code-quality:
runs-on: ubuntu-latest
outputs:
quality-score: ${{ steps.quality.outputs.score }}
coverage: ${{ steps.coverage.outputs.percentage }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Install Node.js dependencies
run: |
cd frontend && npm ci
- name: Run Python linting
run: |
pip install flake8 black isort mypy
flake8 ml-model-api/ --count --select=E9,F63,F7,F82 --show-source --statistics
black --check ml-model-api/
isort --check-only ml-model-api/
mypy ml-model-api/ --ignore-missing-imports
- name: Run JavaScript linting
run: |
cd frontend && npm run lint
- name: Run security scanning
id: security
run: |
pip install safety bandit semgrep trufflehog gitleaks
echo "Running security scans..."
# Dependency scanning
safety check --json --output dependency-security-reports/python-dependencies.json || true
# Code security analysis
bandit -r ml-model-api/ -f json -o code-security-reports/bandit-report.json || true
semgrep --config=auto --json --output=code-security-reports/semgrep-report.json ml-model-api/ || true
# Secret scanning
trufflehog filesystem . --json --output=secrets-security-reports/trufflehog-report.json || true
gitleaks detect --json --output=secrets-security-reports/gitleaks-report.json || true
# Generate summary
python scripts/security/generate_summary.py
- name: Run unit tests with coverage
id: coverage
run: |
pip install pytest pytest-cov pytest-mock coverage
pytest tests/unit/ --cov=ml_model_api --cov-report=json --cov-report=html --cov-report=xml --junitxml=reports/unit-tests.xml
COVERAGE=$(python -c "import json; print(json.load(open('coverage.json')['totals']['percent_covered']))")
echo "percentage=$COVERAGE" >> $GITHUB_OUTPUT
- name: Quality gate evaluation
id: quality
run: |
python -c "
import json
coverage = ${{ steps.coverage.outputs.percentage }}
quality_score = min(100, coverage + 10) # Simple quality score calculation
print(f'score={quality_score}')
print(f'score={quality_score}') >> $GITHUB_OUTPUT
"
- name: Upload coverage reports
uses: actions/upload-artifact@v3
if: always()
with:
name: coverage-reports
path: |
coverage.xml
htmlcov/
coverage.json
retention-days: 30
- name: Upload security reports
uses: actions/upload-artifact@v3
if: always()
with:
name: security-reports
path: |
dependency-security-reports/
code-security-reports/
secrets-security-reports/
retention-days: 30
# Integration Tests
integration-tests:
runs-on: ubuntu-latest
needs: code-quality
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: flavorsnap_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7-alpine
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-test.txt
- name: Wait for services
run: |
timeout 60 bash -c 'until nc -z localhost 5432; do sleep 1; done'
timeout 60 bash -c 'until nc -z localhost 6379; do sleep 1; done'
- name: Run database migrations
run: |
python manage.py migrate
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/flavorsnap_test
REDIS_URL: redis://localhost:6379
- name: Run integration tests
run: |
pytest tests/integration/ --junitxml=reports/integration-tests.xml -v
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/flavorsnap_test
REDIS_URL: redis://localhost:6379
- name: Upload integration test reports
uses: actions/upload-artifact@v3
if: always()
with:
name: integration-test-reports
path: reports/integration-tests.xml
retention-days: 30
# Performance Tests
performance-tests:
runs-on: ubuntu-latest
needs: code-quality
if: github.event.inputs.run_performance_tests == 'true' || github.event_name == 'schedule'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install locust pytest-benchmark
- name: Start application
run: |
python manage.py runserver &
sleep 10
- name: Run performance benchmarks
run: |
pytest tests/performance/ --benchmark-json=performance-results.json --benchmark-only
- name: Run load tests
run: |
locust --headless --users 100 --spawn-rate 10 --run-time 60s --host http://localhost:8000 tests/performance/locustfile.py
- name: Upload performance reports
uses: actions/upload-artifact@v3
if: always()
with:
name: performance-reports
path: |
performance-results.json
locust_report.html
retention-days: 30
# API Tests
api-tests:
runs-on: ubuntu-latest
needs: integration-tests
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest-httpx
- name: Start application
run: |
python manage.py runserver &
sleep 10
- name: Run API tests
run: |
pytest tests/api/ --junitxml=reports/api-tests.xml -v
- name: Upload API test reports
uses: actions/upload-artifact@v3
if: always()
with:
name: api-test-reports
path: reports/api-tests.xml
retention-days: 30
# Security Scanning (Enhanced)
security-scan:
runs-on: ubuntu-latest
needs: code-quality
if: github.event.inputs.run_security_scan == 'true' || github.event_name == 'schedule'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run comprehensive security scan
run: |
python scripts/security/vulnerability_scanner.py
- name: Generate remediation scripts
run: |
python scripts/security/generate_remediation.py
- name: Check for critical vulnerabilities
run: |
python scripts/security/check_critical_vulns.py --fail-threshold high
- name: Upload security scan results
uses: actions/upload-artifact@v3
if: always()
with:
name: enhanced-security-reports
path: |
security-reports/
remediation-scripts/
retention-days: 30
# Container Security
container-security:
runs-on: ubuntu-latest
needs: code-quality
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: flavorsnap:test
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run Docker security scan
run: |
# Install security tools
sudo apt-get update
sudo apt-get install -y docker.io trivy hadolint
# Dockerfile linting
hadolint Dockerfile > container-security-reports/hadolint-report.json || true
# Container image scanning
trivy image --format json --output container-security-reports/trivy-report.json flavorsnap:test || true
# Generate summary
python scripts/security/check_headers.py --output container-security-reports/headers-report.json
- name: Upload container security reports
uses: actions/upload-artifact@v3
if: always()
with:
name: container-security-reports
path: container-security-reports/
retention-days: 30
# Quality Gates
quality-gates:
runs-on: ubuntu-latest
needs: [code-quality, integration-tests, api-tests]
outputs:
gates-passed: ${{ steps.gates.outputs.passed }}
overall-score: ${{ steps.gates.outputs.score }}
steps:
- name: Download all test reports
uses: actions/download-artifact@v3
with:
path: all-reports/
- name: Evaluate quality gates
id: gates
run: |
python ml-model-api/quality_gates.py
- name: Generate comprehensive report
run: |
python ml-model-api/reporting.py
- name: Upload quality reports
uses: actions/upload-artifact@v3
if: always()
with:
name: quality-reports
path: reports/
retention-days: 30
# Build and Package
build:
runs-on: ubuntu-latest
needs: quality-gates
if: needs.quality-gates.outputs.gates-passed == 'true'
strategy:
matrix:
component: [api, frontend, worker]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Python
if: matrix.component == 'api' || matrix.component == 'worker'
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Setup Node.js
if: matrix.component == 'frontend'
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Build API
if: matrix.component == 'api'
run: |
pip install -r requirements.txt
pip install -r requirements-build.txt
python setup.py sdist bdist_wheel
- name: Build Frontend
if: matrix.component == 'frontend'
run: |
cd frontend
npm ci
npm run build
npm run test:unit
- name: Build Worker
if: matrix.component == 'worker'
run: |
pip install -r requirements.txt
pip install -r requirements-build.txt
python setup.py sdist bdist_wheel
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: build-${{ matrix.component }}
path: |
dist/
frontend/dist/
retention-days: 7
# Deploy to Staging
deploy-staging:
runs-on: ubuntu-latest
needs: [build, quality-gates]
if: |
needs.quality-gates.outputs.gates-passed == 'true' &&
(github.event.inputs.deploy_to_staging == 'true' || github.ref == 'refs/heads/develop')
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
path: build-artifacts/
- name: Deploy to staging
run: |
echo "Deploying to staging environment..."
# Add deployment logic here
# This would typically involve:
# - Pushing Docker images to registry
# - Updating Kubernetes deployments
# - Running database migrations
# - Health checks
- name: Run smoke tests
run: |
echo "Running smoke tests..."
# Add smoke test logic here
- name: Notify deployment
uses: 8398a7/action-slack@v3
if: always()
with:
status: ${{ job.status }}
channel: '#deployments'
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
# Deploy to Production
deploy-production:
runs-on: ubuntu-latest
needs: [build, quality-gates, deploy-staging]
if: |
needs.quality-gates.outputs.gates-passed == 'true' &&
github.ref == 'refs/heads/main'
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
path: build-artifacts/
- name: Deploy to production
run: |
echo "Deploying to production environment..."
# Add production deployment logic here
- name: Run production health checks
run: |
echo "Running production health checks..."
# Add health check logic here
- name: Create GitHub Release
if: startsWith(github.ref, 'refs/tags/')
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
# Monitoring and Alerting
monitoring:
runs-on: ubuntu-latest
needs: [deploy-staging, deploy-production]
if: always() && (needs.deploy-staging.result == 'success' || needs.deploy-production.result == 'success')
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup monitoring
run: |
pip install -r requirements.txt
pip install prometheus-client grafana-api
- name: Update monitoring dashboards
run: |
python ml-model-api/monitoring_system.py
- name: Check system health
run: |
python -c "
import requests
import sys
try:
response = requests.get('https://staging.flavorsnap.ai/health', timeout=10)
if response.status_code == 200:
print('Health check passed')
else:
print(f'Health check failed: {response.status_code}')
sys.exit(1)
except Exception as e:
print(f'Health check error: {e}')
sys.exit(1)
"
- name: Send notifications
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
channel: '#alerts'
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
# Cleanup
cleanup:
runs-on: ubuntu-latest
needs: [deploy-staging, deploy-production]
if: always()
steps:
- name: Cleanup old artifacts
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
});
const oldArtifacts = artifacts.data.artifacts.filter(artifact => {
const created = new Date(artifact.created_at);
const daysOld = (Date.now() - created) / (1000 * 60 * 60 * 24);
return daysOld > 30;
});
for (const artifact of oldArtifacts) {
await github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
});
console.log(`Deleted artifact: ${artifact.name}`);
}