-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile.web.prod
More file actions
383 lines (322 loc) · 15.3 KB
/
Copy pathJenkinsfile.web.prod
File metadata and controls
383 lines (322 loc) · 15.3 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
pipeline {
agent any
environment {
GHCR_URL = 'ghcr.io/poolchaos'
IMAGE_NAME = 'serelo-web'
// DELTA-12: Production server from Jenkins credential (not hardcoded)
PROD_SERVER = credentials('SERELO_PROD_SERVER')
DOCKER_BUILDKIT = '1'
}
options {
skipDefaultCheckout(true)
timeout(time: 45, unit: 'MINUTES')
disableConcurrentBuilds()
}
stages {
stage('Safe Memory Management') {
steps {
script {
echo "🚦 === SAFE MEMORY MANAGEMENT ==="
sh '''
echo "🧹 Starting SAFE memory cleanup for web build..."
# Basic Docker cleanup
echo "🧹 Basic Docker cleanup..."
docker container prune -f 2>/dev/null || echo "Container cleanup completed"
docker image prune -f 2>/dev/null || echo "Image cleanup completed"
# Check available memory
AVAILABLE_MB=$(free -m | awk 'NR==2{printf "%d", $7}')
echo "💾 Available memory after cleanup: ${AVAILABLE_MB}MB"
if [ "$AVAILABLE_MB" -lt "300" ]; then
echo "❌ ERROR: Insufficient memory for web build (${AVAILABLE_MB}MB < 300MB required)"
exit 1
fi
echo "✅ Safe memory cleanup completed - Ready for web build"
'''
}
}
}
stage('Workspace Setup') {
steps {
echo "🧹 === WORKSPACE SETUP ==="
script {
deleteDir()
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'CleanBeforeCheckout'],
[$class: 'CloneOption', depth: 1, shallow: true]
],
userRemoteConfigs: [[
url: 'https://github.com/Poolchaos/serelo.git',
credentialsId: 'github-repo-access'
]]
])
env.IMAGE_TAG = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.FULL_IMAGE_NAME = "${GHCR_URL}/${IMAGE_NAME}:${env.IMAGE_TAG}"
sh '''
echo "✅ Checkout completed"
echo "📋 Image: ${FULL_IMAGE_NAME}"
# Verify frontend structure
echo "🔍 Verifying frontend structure..."
if [ ! -d "frontend" ]; then
echo "❌ ERROR: frontend directory not found!"
exit 1
fi
if [ ! -f "frontend/package.json" ]; then
echo "❌ ERROR: frontend/package.json not found!"
exit 1
fi
if [ ! -f "frontend/Dockerfile" ]; then
echo "❌ ERROR: frontend/Dockerfile not found!"
exit 1
fi
echo "✅ Frontend structure verified"
'''
}
}
}
stage('Run Tests') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
script {
echo "🧪 === RUNNING WEB TESTS ==="
sh '''
set +e
echo "Running frontend tests in Docker container..."
docker run --rm \
-e CI=true \
node:20-alpine \
sh -c "npm install --no-audit && npm test -- --watchAll=false --passWithNoTests" 2>&1 || echo "Tests encountered issues but continuing..."
echo "✅ Test stage completed"
'''
}
}
}
post {
always {
sh 'docker rmi node:20-alpine 2>/dev/null || true'
}
}
}
stage('Docker Build') {
steps {
withCredentials([
string(credentialsId: 'SERELO_VITE_API_URL', variable: 'VITE_API_URL'),
string(credentialsId: 'SERELO_PAYSTACK_PUBLIC', variable: 'VITE_PAYSTACK_PUBLIC_KEY')
]) {
script {
echo "🐳 === WEB DOCKER BUILD ==="
echo "Building: ${env.FULL_IMAGE_NAME}"
sh '''
set -e
echo "🔍 Pre-build verification..."
echo "Build context: $(pwd)/frontend"
# Pull latest image for cache
echo "📥 Pulling latest image for build cache..."
docker pull ${GHCR_URL}/${IMAGE_NAME}:latest || echo "No cache image available, will build from scratch"
# Memory-adaptive build strategy
AVAILABLE_MB=$(free -m | awk 'NR==2{printf "%d", $7}')
echo "💾 Available memory: ${AVAILABLE_MB}MB"
if [ "$AVAILABLE_MB" -gt "1200" ]; then
echo "💪 Using optimal build settings..."
MEMORY_LIMIT="1024m"
SWAP_LIMIT="1536m"
elif [ "$AVAILABLE_MB" -gt "800" ]; then
echo "⚖️ Using balanced build settings..."
MEMORY_LIMIT="800m"
SWAP_LIMIT="1200m"
else
echo "🐌 Using minimal build settings..."
MEMORY_LIMIT="600m"
SWAP_LIMIT="800m"
fi
# Build Web Docker image
echo "📦 Building Serelo Web Docker image..."
docker build \\
--progress=plain \\
--cache-from ${GHCR_URL}/${IMAGE_NAME}:latest \\
--build-arg BUILDKIT_INLINE_CACHE=1 \\
--build-arg VITE_API_URL="${VITE_API_URL}" \\
--build-arg VITE_PAYSTACK_PUBLIC_KEY="${VITE_PAYSTACK_PUBLIC_KEY}" \\
--memory=$MEMORY_LIMIT \\
--memory-swap=$SWAP_LIMIT \\
-f frontend/Dockerfile \\
-t ${FULL_IMAGE_NAME} \\
frontend/
echo "✅ Serelo Web Docker image built successfully"
'''
}
}
}
}
stage('Container Security Scan') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'UNSTABLE') {
script {
echo "🔍 === CONTAINER SECURITY SCAN ==="
sh '''
set +e
echo "Scanning ${FULL_IMAGE_NAME} for vulnerabilities..."
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /tmp/trivy-cache:/root/.cache \
aquasec/trivy:latest image \
--cache-dir /tmp/trivy-cache \
--exit-code 0 \
--severity HIGH,CRITICAL \
--format table \
${FULL_IMAGE_NAME} 2>&1 || echo "Security scan encountered issues but continuing..."
echo "✅ Security scan completed"
'''
}
}
}
post {
always {
sh 'rm -rf /tmp/trivy-cache 2>/dev/null || true'
}
}
}
stage('Push to Registry') {
steps {
withCredentials([
string(credentialsId: 'GHCR_USERNAME', variable: 'GHCR_USER'),
string(credentialsId: 'GHCR_TOKEN', variable: 'GHCR_PAT')
]) {
script {
echo "📤 === WEB PUSH TO REGISTRY ==="
sh '''
set -e
echo "📤 Pushing Serelo Web Docker image..."
echo "${GHCR_PAT}" | docker login ghcr.io -u "${GHCR_USER}" --password-stdin
docker push ${FULL_IMAGE_NAME}
echo "🏷️ Tagging and pushing as latest..."
docker tag ${FULL_IMAGE_NAME} ${GHCR_URL}/${IMAGE_NAME}:latest
docker push ${GHCR_URL}/${IMAGE_NAME}:latest
echo "✅ Web image pushed successfully"
'''
}
}
}
}
stage('Approve Deploy') {
steps {
script {
echo "🤔 === DEPLOY CONFIRMATION REQUIRED ==="
echo "📋 Ready to deploy: ${env.FULL_IMAGE_NAME}"
echo "🎯 Target: Serelo Web Production"
input message: '🚀 Deploy Serelo Web to Production?', ok: 'Deploy Now'
echo "✅ Deployment approved"
}
}
}
stage('Deploy to Production') {
steps {
withCredentials([
string(credentialsId: 'GHCR_USERNAME', variable: 'GHCR_USER'),
string(credentialsId: 'GHCR_TOKEN', variable: 'GHCR_PAT')
]) {
script {
echo "🚀 === DEPLOYING SERELO WEB TO PRODUCTION ==="
sh '''
echo "🔗 Connecting to production server..."
ssh -o StrictHostKeyChecking=accept-new ${PROD_SERVER} "
set -e
echo '🎯 Starting Serelo Web deployment...'
# Login to registry
echo '🔐 Logging into container registry...'
echo '${GHCR_PAT}' | docker login ghcr.io -u '${GHCR_USER}' --password-stdin
# Pull new image
echo '📥 Pulling new Serelo Web image...'
docker pull ${FULL_IMAGE_NAME}
# Stop and remove old container
echo '🔄 Stopping old container...'
docker stop serelo-web-prod 2>/dev/null || echo 'No running container to stop'
docker rm serelo-web-prod 2>/dev/null || echo 'No container to remove'
# Start new container
echo '🚀 Starting new Serelo Web container...'
docker run -d --name serelo-web-prod \\
-p 127.0.0.1:3001:80 \\
--restart unless-stopped \\
--network serelo-net \\
${FULL_IMAGE_NAME}
# Verify container is running
echo '✅ Verifying container...'
sleep 5
if docker ps --filter name=serelo-web-prod --format '{{.Status}}' | grep -q 'Up'; then
echo '✅ Container verified running'
else
echo '❌ Container failed to start'
docker logs serelo-web-prod --tail 20 2>/dev/null || echo 'No logs available'
exit 1
fi
echo '✅ Serelo Web deployment completed'
"
'''
echo "✅ Serelo Web deployed successfully"
}
}
}
}
stage('Post-Deploy Verification') {
steps {
script {
echo "🔍 === POST-DEPLOY VERIFICATION ==="
sh '''
echo "🔍 Verifying Serelo Web deployment..."
ssh -o StrictHostKeyChecking=accept-new ${PROD_SERVER} "
echo '=== Container Status ==='
docker ps --filter name=serelo-web
echo '=== Web Logs (last 10 lines) ==='
docker logs --tail 10 serelo-web-prod
echo '=== Health Check ==='
sleep 3
curl -f http://localhost:3001/health || echo 'Health check failed'
"
'''
echo "✅ Post-deploy verification completed"
}
}
}
}
post {
always {
script {
echo "🧹 === POST-BUILD CLEANUP ==="
sh '''
set +e
echo "🐳 Docker cleanup..."
# Remove build image
docker rmi ${FULL_IMAGE_NAME} 2>/dev/null || echo "Build image already removed"
docker rmi ${GHCR_URL}/${IMAGE_NAME}:latest 2>/dev/null || echo "Latest tag already removed"
# Remove base images pulled during build/test
docker rmi node:20-alpine 2>/dev/null || echo "node:20-alpine already removed"
# Remove Trivy cache
rm -rf /tmp/trivy-cache 2>/dev/null || true
# Prune all unused images older than 1h (safe - running containers retain their refs)
docker image prune -a --filter "until=1h" -f 2>/dev/null || echo "Image prune completed"
docker container prune -f 2>/dev/null || echo "Container prune completed"
docker builder prune -f 2>/dev/null || echo "Builder prune completed"
echo "📊 Final status:"
free -h
df -h /
'''
}
}
success {
script {
echo "🎉 === BUILD SUCCESS ==="
echo "✅ Serelo Web deployed successfully"
echo "📋 Image: ${env.FULL_IMAGE_NAME}"
}
}
failure {
script {
echo "💥 === BUILD FAILED ==="
echo "❌ Serelo Web build or deployment failed"
}
}
}
}