-
Notifications
You must be signed in to change notification settings - Fork 0
423 lines (351 loc) · 10.9 KB
/
Copy pathci-cd.yml
File metadata and controls
423 lines (351 loc) · 10.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
name: Switchboard CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
release:
types: [ published ]
env:
NODE_VERSION: '18'
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ============================================================================
# Code Quality & Testing
# ============================================================================
lint-and-typecheck:
name: Lint & Type Check
runs-on: ubuntu-latest
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: Run linting
run: npm run lint
- name: Run type checking
run: npm run typecheck
- name: Check formatting
run: npm run format:check
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
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: Run unit tests
run: npm run test:unit
env:
CI: true
- name: Upload test results
uses: actions/upload-artifact@v3
if: always()
with:
name: unit-test-results
path: |
test-results/
coverage/
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
services:
mongodb:
image: mongo:7.0
env:
MONGO_INITDB_ROOT_USERNAME: test
MONGO_INITDB_ROOT_PASSWORD: test
ports:
- 27017:27017
redis:
image: redis:7.2-alpine
ports:
- 6379:6379
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: Build services
run: npm run build:services
- name: Run integration tests
run: npm run test:integration
env:
CI: true
TEST_DATABASE_URL: mongodb://test:test@localhost:27017/switchboard-test
TEST_REDIS_URL: redis://localhost:6379
SKIP_STRIPE_TESTS: true
- name: Upload integration test results
uses: actions/upload-artifact@v3
if: always()
with:
name: integration-test-results
path: test-results/
security-scan:
name: Security Scan
runs-on: ubuntu-latest
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: Run security audit
run: npm audit --audit-level moderate
- name: Run Snyk security scan
uses: snyk/actions/node@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --severity-threshold=medium
# ============================================================================
# Build & Package
# ============================================================================
build:
name: Build Services
runs-on: ubuntu-latest
needs: [lint-and-typecheck, unit-tests]
strategy:
matrix:
service: [api, billing, oracle-service]
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: Build service
run: npm run build:${{ matrix.service }}
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.service }}-build
path: |
packages/services/${{ matrix.service }}/dist/
packages/services/${{ matrix.service }}/package.json
docker-build:
name: Build Docker Images
runs-on: ubuntu-latest
needs: [build, security-scan]
if: github.event_name == 'push' || github.event_name == 'release'
permissions:
contents: read
packages: write
strategy:
matrix:
service: [api, billing, oracle-service]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login 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 }}/${{ env.IMAGE_NAME }}/${{ matrix.service }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: packages/services/${{ matrix.service }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
# ============================================================================
# End-to-End Testing
# ============================================================================
e2e-tests:
name: End-to-End Tests
runs-on: ubuntu-latest
needs: [docker-build, integration-tests]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
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: Start services with Docker Compose
run: |
cp .env.example .env
docker-compose up -d
sleep 60 # Wait for services to start
- name: Wait for services to be healthy
run: |
timeout 300 bash -c 'until curl -f http://localhost:3000/health; do sleep 5; done'
timeout 300 bash -c 'until curl -f http://localhost:3001/health; do sleep 5; done'
- name: Run E2E tests
run: npm run test:e2e
env:
CI: true
API_BASE_URL: http://localhost:3000
BILLING_BASE_URL: http://localhost:3001
SKIP_STRIPE_TESTS: true
- name: Upload E2E test results
uses: actions/upload-artifact@v3
if: always()
with:
name: e2e-test-results
path: test-results/
- name: Cleanup
if: always()
run: docker-compose down -v
# ============================================================================
# Performance Testing
# ============================================================================
performance-tests:
name: Performance Tests
runs-on: ubuntu-latest
needs: [docker-build]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
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: Start services
run: |
cp .env.example .env
docker-compose up -d api-service billing-service
sleep 45
- name: Run performance tests
run: npm run test:performance
env:
CI: true
LOAD_TEST_USERS: 20
LOAD_TEST_REQUESTS: 100
LOAD_TEST_DURATION: 60000
- name: Upload performance results
uses: actions/upload-artifact@v3
if: always()
with:
name: performance-test-results
path: test-results/
- name: Cleanup
if: always()
run: docker-compose down
# ============================================================================
# Deployment
# ============================================================================
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: [e2e-tests, performance-tests]
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
environment:
name: staging
url: https://staging.switchboard.dev
steps:
- name: Deploy to staging
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.STAGING_HOST }}
username: ${{ secrets.STAGING_USER }}
key: ${{ secrets.STAGING_SSH_KEY }}
script: |
cd /opt/switchboard
git pull origin develop
docker-compose -f docker-compose.staging.yml pull
docker-compose -f docker-compose.staging.yml up -d
docker system prune -f
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [e2e-tests, performance-tests]
if: github.event_name == 'release' && github.event.action == 'published'
environment:
name: production
url: https://switchboard.dev
steps:
- name: Deploy to production
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.PRODUCTION_SSH_KEY }}
script: |
cd /opt/switchboard
git pull origin main
docker-compose pull
docker-compose up -d
docker system prune -f
- name: Health check
run: |
sleep 60
curl -f https://switchboard.dev/health || exit 1
curl -f https://api.switchboard.dev/health || exit 1
- name: Notify deployment
uses: 8398a7/action-slack@v3
if: always()
with:
status: ${{ job.status }}
text: "Switchboard ${{ github.event.release.tag_name }} deployed to production"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
# ============================================================================
# Monitoring & Notifications
# ============================================================================
notify-failure:
name: Notify on Failure
runs-on: ubuntu-latest
needs: [lint-and-typecheck, unit-tests, integration-tests, security-scan, build, docker-build]
if: failure()
steps:
- name: Notify team of failure
uses: 8398a7/action-slack@v3
with:
status: failure
text: "Switchboard CI/CD pipeline failed on ${{ github.ref_name }}"
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}