Repository navigation
407 lines (351 loc) · 13.1 KB
/
Copy pathci.yml
File metadata and controls
407 lines (351 loc) · 13.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
# ==============================================================================
# GitHub Actions CI Workflow for Minimal Full-Stack App (Student Notes)
# ==============================================================================
# Purpose:
# - Automatically test code on every push and pull request
# - Run linting, unit tests, and smoke tests
# - Build Docker images to catch build failures early
# - Ensure code quality before merging to main branches
#
# Teaching points:
# - CI (Continuous Integration) runs automated checks on every code change
# - This catches errors early before they reach production
# - GitHub Actions uses YAML syntax for configuration
# - Jobs can run in parallel or sequentially with dependencies
# - Services (like MySQL) can be started as Docker containers for testing
# - Secrets and environment variables manage sensitive data
# - Status badges show build status on README (green = passing, red = failing)
#
# Workflow Triggers:
# - Push to develop or feature/hanoi2025 branches
# - Pull requests targeting develop branch
# - Manual trigger (workflow_dispatch)
#
# How to view results:
# 1. Go to repository on GitHub
# 2. Click "Actions" tab
# 3. See workflow runs and their status
# 4. Click a run to see detailed logs
# ==============================================================================
name: CI - Build, Lint, and Test
# When to run this workflow
on:
# Run on pushes to these branches
push:
branches:
- develop
- feature/hanoi2025
- main
# Run on pull requests targeting these branches
pull_request:
branches:
- develop
- main
# Allow manual trigger from Actions tab
workflow_dispatch:
# Cancel in-progress runs if a new one is triggered
# Saves GitHub Actions minutes and speeds up feedback
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# Environment variables available to all jobs
env:
NODE_VERSION: "22.x"
DB_HOST: localhost
DB_PORT: 3306
DB_USER: testuser
DB_PASSWORD: testpassword
DB_NAME: test_db
jobs:
# ===========================================================================
# Job 1: Lint Backend Code
# ===========================================================================
lint-backend:
name: Lint Backend (ESLint)
runs-on: ubuntu-latest
steps:
# Checkout code from repository
- name: Checkout code
uses: actions/checkout@v4
# Setup Node.js with caching for faster installs
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
# Install dependencies
# Teaching note: npm ci is preferred in CI (clean install from lockfile)
- name: Install dependencies
run: npm ci
# Run ESLint on backend code
- name: Run ESLint
run: npm run lint --workspace=backend
# ===========================================================================
# Job 2: Lint Frontend Code
# ===========================================================================
lint-frontend:
name: Lint Frontend (ESLint)
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 ESLint
run: npm run lint --workspace=frontend
# ===========================================================================
# Job 3: Build Frontend
# ===========================================================================
build-frontend:
name: Build Frontend (Vite)
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
# Build frontend with Vite
# Teaching note: This catches build errors (TypeScript, import issues, etc.)
- name: Build frontend
run: npm run build --workspace=frontend
env:
# Provide backend URL for build (embedded in frontend bundle)
VITE_BACKEND_URL: http://localhost:4000
# Upload build artifacts so we can inspect them if needed
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: frontend-dist
path: frontend/dist/
retention-days: 7
# ===========================================================================
# Job 4: Test Backend with MySQL
# ===========================================================================
test-backend:
name: Test Backend (Smoke Tests)
runs-on: ubuntu-latest
# Start MySQL as a service container
# Teaching note: GitHub Actions can run services alongside your job
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: test_db
MYSQL_USER: testuser
MYSQL_PASSWORD: testpassword
ports:
- 3306:3306
# Health check ensures MySQL is ready before tests run
options: >-
--health-cmd="mysqladmin ping -h localhost -u$MYSQL_USER -p$MYSQL_PASSWORD"
--health-interval=10s
--health-timeout=5s
--health-retries=5
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"
# Install MySQL client for initializing the test database
- name: Install MySQL client
run: |
sudo apt-get update
sudo apt-get install -y mysql-client
- name: Install dependencies
run: npm ci
# Initialize database with schema and sample data
- name: Initialize database
run: |
mysql -h 127.0.0.1 -u testuser -ptestpassword test_db < db/init.sql
env:
MYSQL_PWD: testpassword
# Wait for backend to start, then run smoke tests
# Teaching note: Smoke tests verify basic API functionality
- name: Start backend and run smoke tests
run: |
# Start backend in background
npm run start --workspace=backend &
BACKEND_PID=$!
# Wait for backend to be ready (max 30 seconds)
echo "Waiting for backend to start..."
for i in {1..30}; do
if curl -f http://localhost:4000/health > /dev/null 2>&1; then
echo "Backend is ready!"
break
fi
echo "Waiting... ($i/30)"
sleep 1
done
# Run smoke tests
npm test --workspace=backend
TEST_EXIT_CODE=$?
# Stop backend
kill $BACKEND_PID || true
# Exit with test result
exit $TEST_EXIT_CODE
env:
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_USER: testuser
DB_PASSWORD: testpassword
DB_NAME: test_db
PORT: 4000
# ===========================================================================
# Job 5: Build Docker Images (Backend)
# ===========================================================================
docker-build-backend:
name: Build Backend Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# Setup Docker Buildx for better build performance
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# Build backend Docker image (but don't push)
# Teaching note: This verifies Dockerfile syntax and build process
- name: Build backend image
uses: docker/build-push-action@v5
with:
context: .
file: ./backend/Dockerfile
push: false
tags: minimal-app-backend:test
cache-from: type=gha
cache-to: type=gha,mode=max
# ===========================================================================
# Job 6: Build Docker Images (Frontend)
# ===========================================================================
docker-build-frontend:
name: Build Frontend Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build frontend image
uses: docker/build-push-action@v5
with:
context: .
file: ./frontend/Dockerfile
push: false
tags: minimal-app-frontend:test
cache-from: type=gha
cache-to: type=gha,mode=max
# ===========================================================================
# Job 7: Docker Compose Integration Test (Optional)
# ===========================================================================
docker-compose-test:
name: Docker Compose Integration Test
runs-on: ubuntu-latest
# This job only runs if all previous jobs succeed
needs: [lint-backend, lint-frontend, build-frontend, test-backend]
steps:
- name: Checkout code
uses: actions/checkout@v4
# Build and start all services with docker-compose
- name: Build and start services
run: |
docker compose up --build -d
echo "Waiting for services to be healthy..."
sleep 30
# Check service health
- name: Check service health
run: |
# Check backend health endpoint
curl -f http://localhost:4000/health || exit 1
# Check frontend is serving content
curl -f http://localhost:5173 || exit 1
# Check MySQL is accessible (via backend)
curl -f http://localhost:4000/api/categories || exit 1
echo "✅ All services are healthy!"
# Show logs if tests fail
- name: Show logs on failure
if: failure()
run: docker compose logs
# Cleanup
- name: Stop services
if: always()
run: docker compose down -v
# ===========================================================================
# Job 8: Summary Report
# ===========================================================================
ci-summary:
name: CI Summary
runs-on: ubuntu-latest
# Run after all other jobs complete
needs:
[
lint-backend,
lint-frontend,
build-frontend,
test-backend,
docker-build-backend,
docker-build-frontend,
docker-compose-test,
]
if: always()
steps:
- name: Check results
run: |
echo "==================================="
echo "CI Workflow Summary"
echo "==================================="
echo "Lint Backend: ${{ needs.lint-backend.result }}"
echo "Lint Frontend: ${{ needs.lint-frontend.result }}"
echo "Build Frontend: ${{ needs.build-frontend.result }}"
echo "Test Backend: ${{ needs.test-backend.result }}"
echo "Docker Build Backend: ${{ needs.docker-build-backend.result }}"
echo "Docker Build Frontend: ${{ needs.docker-build-frontend.result }}"
echo "Docker Compose Test: ${{ needs.docker-compose-test.result }}"
echo "==================================="
# Fail if any job failed
if [[ "${{ needs.lint-backend.result }}" == "failure" ]] || \
[[ "${{ needs.lint-frontend.result }}" == "failure" ]] || \
[[ "${{ needs.build-frontend.result }}" == "failure" ]] || \
[[ "${{ needs.test-backend.result }}" == "failure" ]] || \
[[ "${{ needs.docker-build-backend.result }}" == "failure" ]] || \
[[ "${{ needs.docker-build-frontend.result }}" == "failure" ]] || \
[[ "${{ needs.docker-compose-test.result }}" == "failure" ]]; then
echo "❌ CI failed - see logs above"
exit 1
fi
echo "✅ All CI checks passed!"
# ==============================================================================
# Extension Points for Students
# ==============================================================================
# EXTENSION_POINT: ci.coverage | Add code coverage reporting | intermediate
# - Install nyc or c8 for code coverage
# - Upload coverage to Codecov or Coveralls
# - Add coverage badge to README
#
# EXTENSION_POINT: ci.security | Add security scanning | intermediate
# - npm audit for dependency vulnerabilities
# - Snyk or Dependabot for automated security updates
# - OWASP ZAP for security testing
#
# EXTENSION_POINT: ci.performance | Add performance testing | advanced
# - Lighthouse CI for frontend performance
# - Load testing with k6 or Artillery
# - Bundle size analysis
#
# EXTENSION_POINT: ci.deploy | Add deployment workflow | advanced
# - Deploy to staging on develop branch
# - Deploy to production on main branch
# - Use Docker Hub, AWS, Azure, or Heroku
# ==============================================================================