-
Notifications
You must be signed in to change notification settings - Fork 0
531 lines (471 loc) · 16.9 KB
/
ci-optimized.yml
File metadata and controls
531 lines (471 loc) · 16.9 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
# Optimized CI/CD Pipeline with caching, parallelization, and smart path detection
name: ChaosLabs CI/CD
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
workflow_dispatch:
inputs:
skip_tests:
description: 'Skip test execution'
required: false
default: 'false'
deploy_environment:
description: 'Deploy to environment'
required: false
default: 'none'
type: choice
options:
- none
- staging
- production
# Optimize workflow concurrency
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: chaoslabs
GO_VERSION: '1.21'
NODE_VERSION: '18'
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
jobs:
# Smart change detection to skip unnecessary work
detect-changes:
name: Detect Changes
runs-on: ubuntu-latest
outputs:
go-changed: ${{ steps.changes.outputs.go }}
frontend-changed: ${{ steps.changes.outputs.frontend }}
docs-changed: ${{ steps.changes.outputs.docs }}
infra-changed: ${{ steps.changes.outputs.infra }}
tests-changed: ${{ steps.changes.outputs.tests }}
should-deploy: ${{ steps.deploy-check.outputs.should-deploy }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect file changes
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
go:
- 'controller/**/*.go'
- 'agent/**/*.go'
- 'cli/**/*.go'
- 'go.mod'
- 'go.sum'
- '**/*_test.go'
frontend:
- 'dashboard-v2/**'
- 'Dashboard/**'
docs:
- 'docs/**'
- '*.md'
- '.github/**/*.md'
infra:
- 'infrastructure/**'
- 'docker-compose*.yml'
- '.github/workflows/**'
- 'Dockerfile*'
tests:
- 'tests/**'
- '**/*_test.go'
- 'test/**'
- name: Check if deployment needed
id: deploy-check
run: |
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "should-deploy=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.event.inputs.deploy_environment }}" != "none" ]]; then
echo "should-deploy=true" >> $GITHUB_OUTPUT
else
echo "should-deploy=false" >> $GITHUB_OUTPUT
fi
# Fast documentation-only path
docs-only:
name: Documentation Only
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.docs-changed == 'true' && needs.detect-changes.outputs.go-changed == 'false' && needs.detect-changes.outputs.frontend-changed == 'false'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: 'docs/package-lock.json'
- name: Build documentation
run: |
cd docs
npm ci
npm run build
- name: Deploy docs to GitHub Pages
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/dist
# Parallel linting stage
lint:
name: Lint & Format Check
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.go-changed == 'true' || needs.detect-changes.outputs.frontend-changed == 'true'
strategy:
matrix:
component: [go, frontend]
exclude:
- component: go
# Exclude if only frontend changed
condition: ${{ needs.detect-changes.outputs.go-changed == 'false' }}
- component: frontend
# Exclude if only go changed
condition: ${{ needs.detect-changes.outputs.frontend-changed == 'false' }}
steps:
- name: Checkout
uses: actions/checkout@v4
# Go linting
- name: Setup Go
if: matrix.component == 'go'
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache: true
- name: Go lint
if: matrix.component == 'go'
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=5m --config=.golangci.yml
skip-cache: false
skip-save-cache: false
# Frontend linting
- name: Setup Node.js
if: matrix.component == 'frontend'
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: 'dashboard-v2/package-lock.json'
- name: Install frontend dependencies
if: matrix.component == 'frontend'
run: |
cd dashboard-v2
npm ci --prefer-offline --no-audit
- name: Frontend lint
if: matrix.component == 'frontend'
run: |
cd dashboard-v2
npm run lint
npm run type-check
# Unit tests with matrix strategy
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
needs: [detect-changes, lint]
if: needs.detect-changes.outputs.go-changed == 'true' && github.event.inputs.skip_tests != 'true'
strategy:
matrix:
component: [controller, agent, cli]
go-version: ['1.21'] # Could add ['1.20', '1.21'] for multiple versions
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Download dependencies
run: go mod download
- name: Run unit tests
run: |
cd ${{ matrix.component }}
go test -race -coverprofile=coverage.out -covermode=atomic ./...
- name: Generate coverage report
run: |
cd ${{ matrix.component }}
go tool cover -html=coverage.out -o coverage.html
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./${{ matrix.component }}/coverage.out
flags: ${{ matrix.component }}
name: ${{ matrix.component }}-coverage
- name: Upload test artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: ${{ matrix.component }}-test-results
path: |
${{ matrix.component }}/coverage.out
${{ matrix.component }}/coverage.html
# Frontend tests
frontend-tests:
name: Frontend Tests
runs-on: ubuntu-latest
needs: [detect-changes, lint]
if: needs.detect-changes.outputs.frontend-changed == 'true'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: 'dashboard-v2/package-lock.json'
- name: Install dependencies
run: |
cd dashboard-v2
npm ci --prefer-offline --no-audit
- name: Run tests
run: |
cd dashboard-v2
npm run test:coverage
- name: Upload frontend coverage
uses: codecov/codecov-action@v3
with:
file: ./dashboard-v2/coverage/lcov.info
flags: frontend
name: frontend-coverage
# Integration tests with services
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
needs: [unit-tests, detect-changes]
if: needs.detect-changes.outputs.go-changed == 'true' || needs.detect-changes.outputs.infra-changed == 'true'
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
nats:
image: nats:2.10-alpine
ports:
- 4222:4222
options: >-
--health-cmd "wget --no-verbose --tries=1 --spider http://localhost:8222/healthz || exit 1"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache: true
- name: Wait for services
run: |
timeout 30s bash -c 'until redis-cli -h localhost ping; do sleep 1; done'
timeout 30s bash -c 'until curl -f http://localhost:8222/healthz; do sleep 1; done'
- name: Run integration tests
env:
REDIS_URL: redis://localhost:6379
NATS_URL: nats://localhost:4222
run: |
go test -tags=integration -v ./tests/integration/...
# Security scanning
security:
name: Security Scan
runs-on: ubuntu-latest
needs: detect-changes
if: needs.detect-changes.outputs.go-changed == 'true' || needs.detect-changes.outputs.infra-changed == 'true'
permissions:
security-events: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache: true
- name: Run Gosec Security Scanner
uses: securecodewarrior/github-action-gosec@master
with:
args: '-fmt sarif -out gosec.sarif ./...'
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: gosec.sarif
- name: Run govulncheck
run: |
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
# Build and push Docker images
build-images:
name: Build Images
runs-on: ubuntu-latest
needs: [unit-tests, integration-tests, detect-changes]
if: always() && !cancelled() && (needs.detect-changes.outputs.go-changed == 'true' || needs.detect-changes.outputs.infra-changed == 'true')
strategy:
matrix:
component: [controller, agent, dashboard]
outputs:
controller-digest: ${{ steps.build.outputs.controller-digest }}
agent-digest: ${{ steps.build.outputs.agent-digest }}
dashboard-digest: ${{ steps.build.outputs.dashboard-digest }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
network=host
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.component }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./infrastructure/Dockerfile.${{ matrix.component }}.optimized
target: production
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ matrix.component }}
cache-to: type=gha,mode=max,scope=${{ matrix.component }}
provenance: true
sbom: true
- name: Output digest
run: echo "${{ matrix.component }}-digest=${{ steps.build.outputs.digest }}" >> $GITHUB_OUTPUT
# Performance tests (soak tests)
performance-tests:
name: Performance Tests
runs-on: ubuntu-latest
needs: [build-images, detect-changes]
if: needs.detect-changes.outputs.should-deploy == 'true' || github.event.inputs.skip_tests != 'true'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup k6
run: |
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6
- name: Start test environment
run: |
docker-compose -f infrastructure/docker-compose.test.yml up -d
sleep 30
- name: Run performance tests
run: |
k6 run tests/performance/load-test.js
k6 run tests/performance/stress-test.js
- name: Cleanup test environment
if: always()
run: |
docker-compose -f infrastructure/docker-compose.test.yml down -v
# Deployment to staging/production
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: [build-images, performance-tests, detect-changes]
if: needs.detect-changes.outputs.should-deploy == 'true'
environment:
name: ${{ github.event.inputs.deploy_environment || 'staging' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Deploy to EKS
run: |
aws eks update-kubeconfig --name chaoslabs-cluster
envsubst < infrastructure/k8s/deployment.yaml | kubectl apply -f -
env:
ENVIRONMENT: ${{ github.event.inputs.deploy_environment || 'staging' }}
CONTROLLER_IMAGE: ${{ env.REGISTRY }}/${{ github.repository }}/controller@${{ needs.build-images.outputs.controller-digest }}
AGENT_IMAGE: ${{ env.REGISTRY }}/${{ github.repository }}/agent@${{ needs.build-images.outputs.agent-digest }}
DASHBOARD_IMAGE: ${{ env.REGISTRY }}/${{ github.repository }}/dashboard@${{ needs.build-images.outputs.dashboard-digest }}
# Generate reports and notifications
report:
name: Generate Report
runs-on: ubuntu-latest
needs: [unit-tests, integration-tests, performance-tests, security, build-images]
if: always()
steps:
- name: Download test artifacts
uses: actions/download-artifact@v3
with:
path: artifacts
- name: Generate CI report
run: |
echo "## ChaosLabs CI/CD Report" > ci-report.md
echo "" >> ci-report.md
echo "**Workflow:** ${{ github.workflow }}" >> ci-report.md
echo "**Run:** #${{ github.run_number }}" >> ci-report.md
echo "**Trigger:** ${{ github.event_name }}" >> ci-report.md
echo "**Branch:** ${{ github.ref_name }}" >> ci-report.md
echo "**Commit:** ${{ github.sha }}" >> ci-report.md
echo "" >> ci-report.md
echo "### Job Status" >> ci-report.md
echo "- Unit Tests: ${{ needs.unit-tests.result }}" >> ci-report.md
echo "- Integration Tests: ${{ needs.integration-tests.result }}" >> ci-report.md
echo "- Security Scan: ${{ needs.security.result }}" >> ci-report.md
echo "- Build Images: ${{ needs.build-images.result }}" >> ci-report.md
echo "- Performance Tests: ${{ needs.performance-tests.result }}" >> ci-report.md
echo "" >> ci-report.md
echo "### Performance Metrics" >> ci-report.md
echo "- Workflow Duration: ${{ github.event.head_commit.timestamp }}" >> ci-report.md
if [ -d "artifacts" ]; then
echo "### Artifacts" >> ci-report.md
find artifacts -name "*.out" -o -name "*.html" | while read file; do
echo "- [$(basename $file)]($file)" >> ci-report.md
done
fi
- name: Comment PR
if: github.event_name == 'pull_request'
uses: thollander/actions-comment-pull-request@v2
with:
filePath: ci-report.md
- name: Slack notification
if: always() && (github.ref == 'refs/heads/main' || failure())
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
channel: '#ci-cd'
text: |
ChaosLabs CI/CD ${{ job.status }}
Branch: ${{ github.ref_name }}
Commit: ${{ github.sha }}
Workflow: ${{ github.workflow }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}