-
Notifications
You must be signed in to change notification settings - Fork 0
500 lines (441 loc) · 15.9 KB
/
Copy pathci.yml
File metadata and controls
500 lines (441 loc) · 15.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
name: Educational CI/CD Pipeline - From Basic to Advanced
# STUDY REPOSITORY: Live DevOps Implementation
# This file demonstrates the evolution from basic CI to comprehensive DevOps pipeline
# Perfect for learning Docker, Kubernetes, Argo CD, and GitOps implementation
on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master, develop ]
jobs:
# ============================================================================
# 📚 ORIGINAL BASIC CI PIPELINE (For Reference)
# ============================================================================
# This is the original simple CI pipeline that was used before adding DevOps tech
# Uncomment this section to use the basic version, or study it to understand progression
# name: CI Pipeline
#
# on:
# push:
# branches: [ main, master, develop ]
# pull_request:
# branches: [ main, master, develop ]
#
# jobs:
# build-and-test:
# runs-on: ubuntu-latest
#
# strategy:
# matrix:
# node-version: [18.x, 20.x]
#
# steps:
# - name: Checkout code
# uses: actions/checkout@v4
#
# - name: Setup Node.js ${{ matrix.node-version }}
# uses: actions/setup-node@v4
# with:
# node-version: ${{ matrix.node-version }}
#
# - name: Install dependencies
# run: npm install
#
# - name: Verify server file exists
# run: |
# if [ ! -f server.js ]; then
# echo "Error: server.js not found"
# exit 1
# fi
# echo "✓ server.js found"
#
# - name: Verify HTML file exists
# run: |
# if [ ! -f index.html ]; then
# echo "Error: index.html not found"
# exit 1
# fi
# echo "✓ index.html found"
#
# - name: Check JavaScript syntax
# run: node -c server.js
#
# - name: Start server in background
# run: |
# node server.js &
# SERVER_PID=$!
# echo "SERVER_PID=$SERVER_PID" >> $GITHUB_ENV
# sleep 3
#
# - name: Test server is running
# run: |
# response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000)
# if [ "$response" -eq 200 ]; then
# echo "✓ Server is running and responding with status 200"
# else
# echo "✗ Server responded with status $response"
# exit 1
# fi
#
# - name: Test HTML content is served
# run: |
# content=$(curl -s http://localhost:3000)
# if echo "$content" | grep -q "Calculator"; then
# echo "✓ Calculator HTML is being served correctly"
# else
# echo "✗ Calculator HTML not found in response"
# exit 1
# fi
#
# - name: Stop server
# if: always()
# run: |
# if [ ! -z "$SERVER_PID" ]; then
# kill $SERVER_PID || true
# fi
# pkill -f "node server.js" || true
#
# lint:
# runs-on: ubuntu-latest
#
# steps:
# - name: Checkout code
# uses: actions/checkout@v4
#
# - name: Setup Node.js
# uses: actions/setup-node@v4
# with:
# node-version: '20.x'
#
# - name: Validate HTML
# run: |
# echo "Checking HTML structure..."
# if grep -q "<!DOCTYPE html>" index.html; then
# echo "✓ Valid HTML5 doctype found"
# else
# echo "✗ HTML5 doctype missing"
# exit 1
# fi
#
# - name: Check for required HTML elements
# run: |
# required_elements=("calculator" "display" "buttons")
# for element in "${required_elements[@]}"; do
# if grep -q "$element" index.html; then
# echo "✓ Found: $element"
# else
# echo "✗ Missing: $element"
# exit 1
# fi
# done
#
# - name: Validate package.json
# run: |
# if [ -f package.json ]; then
# node -e "JSON.parse(require('fs').readFileSync('package.json', 'utf8'))"
# echo "✓ package.json is valid JSON"
# else
# echo "✗ package.json not found"
# exit 1
# fi
# ============================================================================
# 📚 PHASE 1: BASIC APPLICATION TESTING (Original Simple Pipeline)
# ============================================================================
# This is what a basic CI pipeline looks like - just testing the core app
# Perfect starting point for any Node.js project
build-and-test:
name: "📦 Basic App Testing (Phase 1)"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
# EDUCATIONAL NOTE: Basic File Validation
# This is the foundation - ensuring core files exist before testing
- name: Verify core files exist
run: |
if [ ! -f server.js ]; then echo "✗ server.js not found" && exit 1; fi
if [ ! -f index.html ]; then echo "✗ index.html not found" && exit 1; fi
if [ ! -f package.json ]; then echo "✗ package.json not found" && exit 1; fi
echo "✓ All core files found"
- name: Check JavaScript syntax
run: node -c server.js
# EDUCATIONAL NOTE: Application Startup Testing
# This tests if your app actually starts and responds
# Critical for catching runtime issues early
- name: Start server in background
run: |
node server.js &
SERVER_PID=$!
echo "SERVER_PID=$SERVER_PID" >> $GITHUB_ENV
sleep 5
- name: Test server is running
run: |
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000)
if [ "$response" -eq 200 ]; then
echo "✓ Server is running and responding with status 200"
else
echo "✗ Server responded with status $response"
exit 1
fi
- name: Test HTML content is served
run: |
content=$(curl -s http://localhost:3000)
if echo "$content" | grep -q "Calculator"; then
echo "✓ Calculator HTML is being served correctly"
else
echo "✗ Calculator HTML not found in response"
exit 1
fi
- name: Stop server
if: always()
run: |
if [ ! -z "$SERVER_PID" ]; then kill $SERVER_PID || true; fi
pkill -f "node server.js" || true
# ============================================================================
# PHASE 2: DOCKER CONTAINERIZATION (Adding Container Tech)
# ============================================================================
# This demonstrates how to test Docker integration in CI/CD
docker-build:
name: "🐳 Docker Testing (Phase 2)"
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v4
# EDUCATIONAL NOTE: Docker Setup in CI
# This is how you set up Docker in GitHub Actions for testing
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# EDUCATIONAL NOTE: Multi-Stage Docker Build Testing
# This tests your production Dockerfile with all optimizations
- name: Build Docker image
run: |
docker build -f docker/Dockerfile -t simple-calculator:latest .
echo "✓ Docker image built successfully"
# EDUCATIONAL NOTE: Container Runtime Testing
# This validates that your containerized app works correctly
- name: Test Docker image
run: |
docker run -d -p 3000:3000 --name test-container simple-calculator:latest
sleep 5
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000)
if [ "$response" -eq 200 ]; then
echo "✓ Docker container is running and responding"
else
echo "✗ Docker container responded with status $response"
exit 1
fi
content=$(curl -s http://localhost:3000)
if echo "$content" | grep -q "Calculator"; then
echo "✓ Calculator HTML is served correctly from Docker"
else
echo "✗ Calculator HTML not found in Docker response"
exit 1
fi
- name: Cleanup Docker container
if: always()
run: |
docker stop test-container || true
docker rm test-container || true
# ============================================================================
# PHASE 3: KUBERNETES MANIFEST VALIDATION (Adding K8s Tech)
# ============================================================================
kubernetes-validation:
name: "☸️ Kubernetes Validation (Phase 3)"
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v4
# EDUCATIONAL NOTE: Kubernetes Setup in CI
- name: Install kubectl
uses: azure/setup-kubectl@v3
# EDUCATIONAL NOTE: Manifest File Validation
- name: Validate Kubernetes manifests
run: |
if [ ! -f k8s/deployment.yaml ]; then echo "✗ k8s/deployment.yaml not found" && exit 1; fi
if [ ! -f k8s/service.yaml ]; then echo "✗ k8s/service.yaml not found" && exit 1; fi
echo "✓ Kubernetes manifests found"
# EDUCATIONAL NOTE: YAML Syntax Validation
- name: Validate YAML syntax
run: |
for file in k8s/*.yaml; do
if python3 -c "import yaml; yaml.safe_load(open('$file'))" 2>/dev/null; then
echo "✓ $file has valid YAML syntax"
else
echo "✗ $file has invalid YAML syntax"
exit 1
fi
done
# EDUCATIONAL NOTE: Configuration Validation
- name: Check required fields in deployment
run: |
if ! grep -q "kind: Deployment" k8s/deployment.yaml; then
echo "✗ Deployment kind not found"
exit 1
fi
if ! grep -q "simple-calculator:latest" k8s/deployment.yaml; then
echo "✗ Docker image reference not found"
exit 1
fi
echo "✓ Deployment manifest is properly configured"
# ============================================================================
# PHASE 4: ARGOCD GITOPS VALIDATION (Adding GitOps Tech)
# ============================================================================
argocd-validation:
name: "🔄 Argo CD Validation (Phase 4)"
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v4
# EDUCATIONAL NOTE: GitOps Configuration Validation
- name: Validate Argo CD application
run: |
if [ ! -f argocd/application.yaml ]; then
echo "✗ argocd/application.yaml not found"
exit 1
fi
echo "✓ Argo CD application manifest found"
- name: Validate Argo CD YAML syntax
run: |
if python3 -c "import yaml; yaml.safe_load(open('argocd/application.yaml'))" 2>/dev/null; then
echo "✓ Argo CD application has valid YAML syntax"
else
echo "✗ Argo CD application has invalid YAML syntax"
exit 1
fi
# EDUCATIONAL NOTE: GitOps Configuration Checking
- name: Check Argo CD configuration
run: |
if ! grep -q "repoURL:" argocd/application.yaml; then
echo "✗ Argo CD repoURL not configured"
exit 1
fi
if ! grep -q "path: k8s" argocd/application.yaml; then
echo "✗ Argo CD path not configured correctly"
exit 1
fi
if ! grep -q "automated:" argocd/application.yaml; then
echo "✗ Argo CD sync policy not configured"
exit 1
fi
echo "✓ Argo CD application is properly configured"
# ============================================================================
# PHASE 5: END-TO-END TESTING (Complete DevOps Stack)
# ============================================================================
e2e-test:
name: "🔬 E2E Testing (Phase 5)"
runs-on: ubuntu-latest
needs: [docker-build, kubernetes-validation, lint]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# EDUCATIONAL NOTE: Local Kubernetes Testing with KinD
- name: Create KinD cluster
run: |
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
kind create cluster --name test-cluster --config - <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.27.3
EOF
echo "✓ KinD cluster created"
# EDUCATIONAL NOTE: Image Loading into Cluster
- name: Load Docker image into KinD
run: |
docker build -f docker/Dockerfile -t simple-calculator:latest .
kind load docker-image simple-calculator:latest --name test-cluster
echo "✓ Docker image loaded into KinD"
# EDUCATIONAL NOTE: Kubernetes Deployment Testing
- name: Deploy to Kubernetes
run: |
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl wait --for=condition=available --timeout=60s deployment/simple-calculator
echo "✓ Application deployed to Kubernetes"
# EDUCATIONAL NOTE: Full Stack Testing
- name: Test Kubernetes deployment
run: |
kubectl port-forward service/simple-calculator-service 3000:80 &
FORWARD_PID=$!
sleep 5
response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000)
if [ "$response" -eq 200 ]; then
echo "✓ Kubernetes deployment is working"
else
echo "✗ Kubernetes deployment failed with status $response"
exit 1
fi
content=$(curl -s http://localhost:3000)
if echo "$content" | grep -q "Calculator"; then
echo "✓ Calculator is accessible through Kubernetes"
else
echo "✗ Calculator content not found in Kubernetes response"
exit 1
fi
kill $FORWARD_PID || true
- name: Cleanup KinD cluster
if: always()
run: |
kind delete cluster --name test-cluster || true
# ============================================================================
# PHASE 1 CONTINUED: CODE LINTING (Enhanced Original)
# ============================================================================
lint:
name: "🔍 Code Linting (Phase 1 Enhanced)"
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
# EDUCATIONAL NOTE: HTML Validation
- name: Validate HTML
run: |
if grep -q "<!DOCTYPE html>" index.html; then
echo "✓ Valid HTML5 doctype found"
else
echo "✗ HTML5 doctype missing"
exit 1
fi
# EDUCATIONAL NOTE: Content Validation
- name: Check for required HTML elements
run: |
required_elements=("calculator" "display" "buttons")
for element in "${required_elements[@]}"; do
if grep -q "$element" index.html; then
echo "✓ Found: $element"
else
echo "✗ Missing: $element"
exit 1
fi
done
# EDUCATIONAL NOTE: Package Configuration Validation
- name: Validate package.json
run: |
if [ -f package.json ]; then
node -e "JSON.parse(require('fs').readFileSync('package.json', 'utf8'))"
echo "✓ package.json is valid JSON"
else
echo "✗ package.json not found"
exit 1
fi