This repository was archived by the owner on Aug 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
542 lines (463 loc) · 16.3 KB
/
ci-cd.yml
File metadata and controls
542 lines (463 loc) · 16.3 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
name: Optimized CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 2 * * *' # Daily at 2 AM for monitoring
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: false
default: 'staging'
type: choice
options:
- staging
- production
skip_tests:
description: 'Skip test execution'
required: false
default: false
type: boolean
# Cancel in-progress workflows for the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
NODE_VERSION: '20'
DOCKER_REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ================================
# FAST QUALITY GATES
# ================================
quality-gates:
name: Quality Gates (Fast)
runs-on: ubuntu-latest
timeout-minutes: 8
outputs:
cache-key: ${{ steps.cache-key.outputs.key }}
should-deploy: ${{ steps.deploy-check.outputs.should-deploy }}
changes-detected: ${{ steps.changes.outputs.any }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect changes
id: changes
uses: dorny/paths-filter@v3
with:
filters: |
src:
- 'src/**'
config:
- 'package*.json'
- 'tsconfig*.json'
- 'vite.config.ts'
- '.github/workflows/**'
tests:
- 'test/**'
- 'e2e/**'
- name: Generate cache key
id: cache-key
run: echo "key=${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}" >> $GITHUB_OUTPUT
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ steps.cache-key.outputs.key }}
restore-keys: ${{ steps.cache-key.outputs.key }}-
- name: Install dependencies
run: npm ci --prefer-offline --no-audit --progress=false
# CRITICAL GATES (must pass - fast feedback)
- name: 🚨 Critical - Type checking
run: npm run type-check
timeout-minutes: 2
- name: 🚨 Critical - Lint (essential rules only)
run: npm run lint
timeout-minutes: 2
- name: ⚡ Fast - Format check
run: npm run format:check
continue-on-error: true
timeout-minutes: 1
# MOVED TO SCHEDULED WORKFLOW:
# - Security audit (now runs nightly + on security changes)
# - Code complexity (now advisory in quality monitoring)
# - Performance analysis (now in dedicated workflow)
# Determine deployment strategy
- name: Check deployment requirements
id: deploy-check
run: |
if [[ "${{ github.event_name }}" == "push" ]] && [[ "${{ github.ref }}" == "refs/heads/main" || "${{ github.ref }}" == "refs/heads/develop" ]]; then
echo "should-deploy=true" >> $GITHUB_OUTPUT
else
echo "should-deploy=false" >> $GITHUB_OUTPUT
fi
# ================================
# OPTIMIZED TESTING SUITE (CRITICAL PATH ONLY)
# ================================
test:
name: Critical Tests
runs-on: ubuntu-latest
needs: quality-gates
if: ${{ !inputs.skip_tests && needs.quality-gates.outputs.changes-detected == 'true' }}
timeout-minutes: 12
strategy:
matrix:
test-type: [unit] # Removed e2e from critical path
fail-fast: true
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ needs.quality-gates.outputs.cache-key }}
restore-keys: ${{ needs.quality-gates.outputs.cache-key }}-
- name: Install dependencies
run: npm ci --prefer-offline --no-audit --progress=false
- name: ⚡ Unit tests (fast CI mode)
run: npm run test:ci
timeout-minutes: 2
- name: Upload basic coverage (async)
if: always()
run: |
echo "📊 Test execution completed"
echo "✅ Fast CI tests passed - comprehensive testing runs on schedule"
# ================================
# E2E TESTS (PARALLEL, NON-BLOCKING)
# ================================
e2e-tests:
name: E2E Tests (Parallel)
runs-on: ubuntu-latest
needs: quality-gates
if: ${{ !inputs.skip_tests && (github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') }}
timeout-minutes: 30
continue-on-error: true # Don't block other jobs if E2E fails
strategy:
matrix:
shard: [1, 2] # Split E2E tests into shards for parallel execution
fail-fast: false # Allow other shards to complete
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ needs.quality-gates.outputs.cache-key }}
restore-keys: ${{ needs.quality-gates.outputs.cache-key }}-
- name: Install dependencies
run: npm ci --prefer-offline --no-audit --progress=false
- name: Cache Playwright browsers
uses: actions/cache@v4
with:
path: ~/.cache/ms-playwright
key: playwright-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
timeout-minutes: 5
- name: ⚡ E2E tests (sharded)
run: npm run test:e2e -- --project=chromium --shard=${{ matrix.shard }}/2
timeout-minutes: 20
- name: Upload E2E artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: e2e-results-shard-${{ matrix.shard }}
path: |
test-results/
playwright-report/
retention-days: 3
# ================================
# BUILD & CONTAINERIZE
# ================================
build:
name: Build & Package
runs-on: ubuntu-latest
needs: [quality-gates]
timeout-minutes: 15
outputs:
image-digest: ${{ steps.docker-build.outputs.digest }}
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Generate version
id: version
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
VERSION="$(cat package.json | jq -r .version)"
else
VERSION="$(cat package.json | jq -r .version)-dev.$(date +%s)"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Generated version: $VERSION"
- name: Build application
run: npm run build
env:
VITE_BUILD_DATE: ${{ github.run_number }}
VITE_GIT_COMMIT: ${{ github.sha }}
VITE_VERSION: ${{ steps.version.outputs.version }}
- name: Upload build artifacts
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
retention-days: 3
# Docker Build (Optimized - only for deployable branches)
- name: Set up Docker Buildx
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
uses: docker/setup-buildx-action@v3
- name: Build Docker image (fast test)
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
id: docker-build
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: false
tags: ${{ env.IMAGE_NAME }}:test
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
target: production # Skip dev dependencies
- name: Quick Docker health check
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
run: |
docker run -d --name test-container -p 8080:8080 ${{ env.IMAGE_NAME }}:test
sleep 3 # Reduced wait time
curl -f http://localhost:8080/ --max-time 10 || exit 1
docker stop test-container && docker rm test-container
echo "✅ Docker image verified"
timeout-minutes: 2
# ================================
# QUALITY MONITORING (NON-BLOCKING, SCHEDULED)
# ================================
quality-check:
name: Quality Monitoring
runs-on: ubuntu-latest
needs: [quality-gates]
# Only run on schedule or manual trigger to avoid blocking
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
timeout-minutes: 8
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci --prefer-offline --no-audit --progress=false
- name: Build for analysis (cached)
run: npm run build -- --mode=production
env:
NODE_ENV: production
# Security Analysis (moved from critical path)
- name: 🔒 Security audit (non-blocking)
run: npm run security:audit --audit-level=moderate
continue-on-error: true
timeout-minutes: 3
# Code Complexity (advisory only)
- name: 📊 Code complexity analysis
run: npm run complexity:check
continue-on-error: true
timeout-minutes: 2
# ================================
# DEPLOYMENT
# ================================
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: [quality-gates, test, build] # E2E tests run in parallel, don't block staging
if: needs.quality-gates.outputs.should-deploy == 'true' && (github.ref == 'refs/heads/develop' || inputs.environment == 'staging')
environment:
name: staging
url: https://staging-organism-simulation.pages.dev
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
- name: Configure staging environment
run: |
echo "VITE_ENVIRONMENT=staging" >> .env
echo "VITE_BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> .env
echo "VITE_GIT_COMMIT=${{ github.sha }}" >> .env
echo "VITE_VERSION=${{ needs.build.outputs.version }}" >> .env
- name: Deploy to Cloudflare Pages (Staging)
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: organism-simulation-staging
directory: dist
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy Docker container (Staging)
if: github.event_name != 'pull_request'
run: |
echo "🐳 Would deploy Docker container to staging..."
# Add actual staging deployment logic here
- name: Run staging smoke tests
run: npm run test:smoke:staging
continue-on-error: true
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [quality-gates, test, build, deploy-staging]
if: needs.quality-gates.outputs.should-deploy == 'true' && (github.ref == 'refs/heads/main' || inputs.environment == 'production')
environment:
name: production
url: https://organism-simulation.pages.dev
timeout-minutes: 20
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
- name: Configure production environment
run: |
echo "VITE_ENVIRONMENT=production" >> .env
echo "VITE_BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> .env
echo "VITE_GIT_COMMIT=${{ github.sha }}" >> .env
echo "VITE_VERSION=${{ needs.build.outputs.version }}" >> .env
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push production Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:latest
${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build.outputs.version }}
${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
platforms: linux/amd64,linux/arm64
- name: Deploy to Cloudflare Pages (Production)
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: organism-simulation
directory: dist
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
wranglerVersion: '3'
- name: Run production smoke tests
run: npm run test:smoke:production
continue-on-error: true
- name: Notify deployment success
if: success()
run: |
echo "🎉 Production deployment successful!"
echo "Version: ${{ needs.build.outputs.version }}"
echo "Commit: ${{ github.sha }}"
- name: Notify deployment failure
if: failure()
run: |
echo "❌ Production deployment failed!"
echo "Please check the logs and take immediate action."
# ================================
# MONITORING & MAINTENANCE
# ================================
monitoring:
name: Health & Performance Monitoring
runs-on: ubuntu-latest
if: github.event_name == 'schedule' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Check deployment health
run: |
echo "🏥 Checking application health..."
# Health check staging
if curl -f "https://staging-organism-simulation.pages.dev/health" 2>/dev/null; then
echo "✅ Staging environment healthy"
else
echo "⚠️ Staging environment health check failed"
fi
# Health check production
if curl -f "https://organism-simulation.pages.dev/health" 2>/dev/null; then
echo "✅ Production environment healthy"
else
echo "❌ Production environment health check failed"
fi
- name: Performance monitoring
run: |
echo "📈 Running performance checks..."
# Add performance monitoring logic
echo "Performance monitoring completed"
- name: Security monitoring
run: |
echo "🔒 Running security checks..."
# Add security monitoring logic
echo "Security monitoring completed"
# ================================
# CLEANUP
# ================================
cleanup:
name: Cleanup & Maintenance
runs-on: ubuntu-latest
needs: [quality-gates, test, build] # E2E tests run independently
if: always() && github.event_name != 'pull_request'
timeout-minutes: 5
steps:
- name: Cleanup old artifacts
run: |
echo "🧹 Cleanup completed"
# Artifacts are automatically cleaned up based on retention policy
- name: Summary report
run: |
echo "📋 Pipeline Summary:"
echo "- Commit: ${{ github.sha }}"
echo "- Branch: ${{ github.ref_name }}"
echo "- Build Status: ${{ needs.build.result || 'N/A' }}"
echo "- Tests Status: ${{ needs.test.result || 'N/A' }}"
echo "- Overall Status: ${{ job.status }}"