-
Notifications
You must be signed in to change notification settings - Fork 0
443 lines (384 loc) · 14.1 KB
/
Copy pathci-cd-enhanced.yml
File metadata and controls
443 lines (384 loc) · 14.1 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
name: Enhanced CI/CD Pipeline
on:
push:
branches: [ main, develop, staging ]
pull_request:
branches: [ main, develop, staging ]
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: false
default: 'staging'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# ============================================================================
# STAGE 1: Code Quality & Testing
# ============================================================================
code-quality:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tnp_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:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
# Backend Quality Checks
- name: Backend - Install dependencies
run: cd backend && npm ci
- name: Backend - Type checking
run: cd backend && npm run typecheck || true
- name: Backend - Linter
run: cd backend && npm run lint 2>/dev/null || true
- name: Backend - Unit tests
run: cd backend && npm run test:unit 2>/dev/null || true
continue-on-error: true
- name: Backend - Integration tests
run: cd backend && npm run test:integration 2>/dev/null || true
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/tnp_test
REDIS_URL: redis://localhost:6379
continue-on-error: true
# Frontend Quality Checks
- name: Frontend - Install dependencies
run: cd frontend && npm ci
- name: Frontend - Type checking
run: cd frontend && npx tsc --noEmit || true
- name: Frontend - Linter
run: cd frontend && npm run lint 2>/dev/null || true
- name: Frontend - Build verification
run: cd frontend && npm run build
env:
VITE_API_BASE_URL: http://localhost:3001
# ============================================================================
# STAGE 2: Docker Build Verification
# ============================================================================
docker-build-test:
runs-on: ubuntu-latest
needs: code-quality
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Backend image (test)
uses: docker/build-push-action@v5
with:
context: ./backend
push: false
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build Frontend image (test)
uses: docker/build-push-action@v5
with:
context: ./frontend
push: false
cache-from: type=gha
cache-to: type=gha,mode=max
# ============================================================================
# STAGE 3: Security Scanning
# ============================================================================
security-scan:
runs-on: ubuntu-latest
needs: code-quality
steps:
- uses: actions/checkout@v4
# Trivy filesystem scan - Backend
- name: Trivy scan - Backend filesystem
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: 'backend'
format: 'sarif'
output: 'trivy-backend.sarif'
severity: 'HIGH,CRITICAL'
# Trivy filesystem scan - Frontend
- name: Trivy scan - Frontend filesystem
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: 'frontend'
format: 'sarif'
output: 'trivy-frontend.sarif'
severity: 'HIGH,CRITICAL'
# Upload results
- name: Upload Trivy results
uses: github/codeql-action/upload-sarif@v2
if: always()
with:
sarif_file: 'trivy-*.sarif'
# Dependency check (optional)
- name: Check npm dependencies
run: |
cd backend && npm audit --production || true
cd ../frontend && npm audit --production || true
# ============================================================================
# STAGE 4: Build & Push Docker Images
# ============================================================================
docker-build-push:
runs-on: ubuntu-latest
needs: [code-quality, docker-build-test, security-scan]
if: github.event_name == 'push'
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# ========== BACKEND IMAGE ==========
- name: Extract metadata - Backend
id: meta-backend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-backend
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Backend image
uses: docker/build-push-action@v5
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
# ========== FRONTEND IMAGE ==========
- name: Extract metadata - Frontend
id: meta-frontend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Frontend image
uses: docker/build-push-action@v5
with:
context: ./frontend
file: ./frontend/Dockerfile
push: true
tags: ${{ steps.meta-frontend.outputs.tags }}
labels: ${{ steps.meta-frontend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILDKIT_INLINE_CACHE=1
VITE_API_BASE_URL=${{ secrets.VITE_API_BASE_URL || 'http://localhost:3001' }}
- name: Image digest
run: |
echo "Backend: ${{ steps.meta-backend.outputs.digest }}"
echo "Frontend: ${{ steps.meta-frontend.outputs.digest }}"
# ============================================================================
# STAGE 5: Deploy to Staging
# ============================================================================
deploy-staging:
runs-on: ubuntu-latest
needs: docker-build-push
if: github.ref == 'refs/heads/develop' && github.event_name == 'push'
environment:
name: staging
steps:
- uses: actions/checkout@v4
- name: Deploy to Staging
env:
DEPLOY_KEY: ${{ secrets.STAGING_SSH_KEY }}
DEPLOY_HOST: ${{ secrets.STAGING_SERVER_IP }}
DEPLOY_USER: ${{ secrets.STAGING_SSH_USER }}
run: |
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H $DEPLOY_HOST >> ~/.ssh/known_hosts 2>/dev/null || true
# SSH into server and pull latest images
ssh -i ~/.ssh/deploy_key $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
cd /opt/tnp-app
docker-compose -f docker-compose.prod.yml pull
docker-compose -f docker-compose.prod.yml up -d
docker-compose -f docker-compose.prod.yml exec backend npm run migrate || true
EOF
- name: Run smoke tests
run: |
echo "Running health checks..."
curl -f https://staging.yourdomain.com/health || exit 1
curl -f https://api-staging.yourdomain.com/health || exit 1
# ============================================================================
# STAGE 6: Deploy to Production (Manual Approval)
# ============================================================================
deploy-production:
runs-on: ubuntu-latest
needs: docker-build-push
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment:
name: production
steps:
- uses: actions/checkout@v4
- name: Create deployment
uses: actions/github-script@v6
with:
script: |
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
environment: 'production',
required_contexts: [],
auto_merge: false,
});
console.log('Deployment created:', deployment.data.id);
- name: Deploy to Production
env:
DEPLOY_KEY: ${{ secrets.PROD_SSH_KEY }}
DEPLOY_HOST: ${{ secrets.PROD_SERVER_IP }}
DEPLOY_USER: ${{ secrets.PROD_SSH_USER }}
run: |
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H $DEPLOY_HOST >> ~/.ssh/known_hosts 2>/dev/null || true
# SSH into server and pull latest images with blue-green deployment
ssh -i ~/.ssh/deploy_key $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
cd /opt/tnp-app
# Backup current state
docker-compose -f docker-compose.prod.yml ps > backup_state.txt
# Pull latest images
docker-compose -f docker-compose.prod.yml pull
# Start new containers (blue-green)
docker-compose -f docker-compose.prod.yml up -d
# Run migrations
docker-compose -f docker-compose.prod.yml exec backend npm run migrate || true
# Health check
sleep 10
curl -f http://localhost:3001/health || (docker-compose -f docker-compose.prod.yml down && exit 1)
EOF
- name: Production health check
run: |
echo "Verifying production deployment..."
for i in {1..30}; do
if curl -f https://yourdomain.com/health 2>/dev/null; then
echo "✓ Frontend healthy"
break
fi
echo "Waiting for frontend... ($i/30)"
sleep 2
done
for i in {1..30}; do
if curl -f https://api.yourdomain.com/health 2>/dev/null; then
echo "✓ Backend healthy"
break
fi
echo "Waiting for backend... ($i/30)"
sleep 2
done
- name: Create deployment status success
if: success()
uses: actions/github-script@v6
with:
script: |
github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ github.event.deployment.id }},
state: 'success',
environment_url: 'https://yourdomain.com',
description: 'Deployment successful'
});
- name: Create deployment status failure
if: failure()
uses: actions/github-script@v6
with:
script: |
github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ github.event.deployment.id }},
state: 'failure',
description: 'Deployment failed - check logs'
});
# ============================================================================
# Notifications
# ============================================================================
notify:
runs-on: ubuntu-latest
needs: [code-quality, docker-build-push]
if: always()
steps:
- name: Notify Slack (Success)
if: success()
uses: slackapi/slack-github-action@v1
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }}
payload: |
{
"text": "✅ CI/CD Pipeline Successful",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Build Status:* ✅ Successful\n*Branch:* `${{ github.ref }}`\n*Commit:* `${{ github.sha }}`\n*Author:* ${{ github.actor }}"
}
}
]
}
- name: Notify Slack (Failure)
if: failure()
uses: slackapi/slack-github-action@v1
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }}
payload: |
{
"text": "❌ CI/CD Pipeline Failed",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Build Status:* ❌ Failed\n*Branch:* `${{ github.ref }}`\n*Commit:* `${{ github.sha }}`\n*Author:* ${{ github.actor }}"
}
}
]
}