-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
397 lines (377 loc) · 15.8 KB
/
Copy pathJenkinsfile
File metadata and controls
397 lines (377 loc) · 15.8 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
pipeline {
agent any
triggers {
githubPush()
}
environment {
DOCKERHUB_REGISTRY = "chewiclinton"
KUBECONFIG = "/var/lib/jenkins/.kube/config"
NAMESPACE = "lexcam"
PIP_CACHE_DIR = "/var/jenkins_home/.pip-cache"
}
stages {
// ── Stage 1: Checkout ──────────────────────────────────────────────────
stage('Checkout') {
steps {
checkout scm
echo "Branch: ${env.GIT_BRANCH} | Commit: ${env.GIT_COMMIT[0..7]}"
}
}
// ── Stage 2: Test (parallel per service) ───────────────────────────────
stage('Test') {
parallel {
stage('user-management') {
steps {
dir('services/user-management') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('lawyer-service') {
steps {
dir('services/lawyer-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('document-service') {
steps {
dir('services/document-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('payment-service') {
steps {
dir('services/payment-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('rag-service') {
steps {
dir('services/rag-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('knowledge-base-service') {
steps {
dir('services/knowledge-base-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('notification-service') {
steps {
dir('services/notification-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('feedback-service') {
steps {
dir('services/feedback-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('admin-panel') {
steps {
dir('services/admin-panel') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('scraper-service') {
steps {
dir('services/scraper-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('embedding-service') {
steps {
dir('services/embedding-service') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('doc-worker') {
steps {
dir('workers/doc-worker') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('notification-worker') {
steps {
dir('workers/notification-worker') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('indexing-worker') {
steps {
dir('workers/indexing-worker') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
stage('lawyer-ingest-worker') {
steps {
dir('workers/lawyer-ingest-worker') {
sh 'python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -q && .venv/bin/pytest --tb=short -q'
}
}
}
}
}
// ── Stage 3: Coverage Check ────────────────────────────────────────────
stage('Coverage') {
steps {
script {
def services = [
'user-management', 'lawyer-service', 'document-service',
'payment-service', 'admin-panel'
]
def fastapi = [
'rag-service', 'knowledge-base-service', 'embedding-service',
'notification-service', 'feedback-service', 'scraper-service'
]
services.each { svc ->
dir("services/${svc}") {
sh ".venv/bin/pytest --cov=apps --cov-report=term-missing --cov-report=xml:coverage.xml --cov-fail-under=80 -q"
}
}
fastapi.each { svc ->
dir("services/${svc}") {
sh ".venv/bin/pytest --cov=app --cov-report=term-missing --cov-report=xml:coverage.xml --cov-fail-under=80 -q"
}
}
def workers = [
'doc-worker', 'notification-worker', 'indexing-worker', 'lawyer-ingest-worker'
]
workers.each { w ->
dir("workers/${w}") {
sh ".venv/bin/pytest --cov=services --cov-report=term-missing --cov-report=xml:coverage.xml --cov-fail-under=80 -q"
}
}
}
}
}
// ── Stage 4: SonarCloud Analysis ───────────────────────────────────────
stage('SonarCloud') {
steps {
withSonarQubeEnv('SonarCloud') {
withCredentials([string(credentialsId: 'sonarcloud-token', variable: 'SONAR_TOKEN')]) {
script {
def scannerHome = tool 'SonarScanner'
def branch = env.GIT_BRANCH.replaceAll('origin/', '')
sh "${scannerHome}/bin/sonar-scanner -Dsonar.token=${SONAR_TOKEN} -Dsonar.branch.name=${branch}"
}
}
}
}
}
// ── Stage 5: SonarCloud Quality Gate ──────────────────────────────────
stage('Quality Gate') {
steps {
timeout(time: 5, unit: 'MINUTES') {
script {
try {
waitForQualityGate abortPipeline: false
} catch (Exception e) {
echo "Quality Gate polling skipped: ${e.message}"
}
}
}
}
}
// ── Stage 6: Build Docker Images (main branch only) ────────────────────
stage('Build') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
script {
def tag = env.GIT_COMMIT[0..7]
def services = [
'user-management', 'lawyer-service', 'document-service',
'payment-service', 'notification-service', 'feedback-service',
'admin-panel', 'scraper-service', 'rag-service',
'knowledge-base-service', 'embedding-service'
]
def workers = [
'doc-worker', 'notification-worker', 'indexing-worker', 'lawyer-ingest-worker'
]
services.each { svc ->
sh "docker build -t ${DOCKERHUB_REGISTRY}/${svc}:${tag} -t ${DOCKERHUB_REGISTRY}/${svc}:latest services/${svc}/"
}
workers.each { w ->
sh "docker build -t ${DOCKERHUB_REGISTRY}/${w}:${tag} -t ${DOCKERHUB_REGISTRY}/${w}:latest workers/${w}/"
}
sh "docker build -t ${DOCKERHUB_REGISTRY}/frontend:${tag} -t ${DOCKERHUB_REGISTRY}/frontend:latest frontend/"
}
}
}
// ── Stage 7: Security Scan with Trivy (main branch only) ──────────────
stage('Security Scan') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
script {
def tag = env.GIT_COMMIT[0..7]
def images = [
'user-management', 'lawyer-service', 'document-service',
'payment-service', 'notification-service', 'feedback-service',
'admin-panel', 'scraper-service', 'rag-service',
'knowledge-base-service', 'embedding-service',
'doc-worker', 'notification-worker', 'indexing-worker', 'lawyer-ingest-worker',
'frontend'
]
images.each { img ->
sh "trivy image --exit-code 0 --severity HIGH,CRITICAL --no-progress ${DOCKERHUB_REGISTRY}/${img}:${tag}"
}
}
}
}
// ── Stage 8: Push to DockerHub (main branch only) ──────────────────────
stage('Push') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-creds',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
sh "echo ${DOCKER_PASS} | docker login -u ${DOCKER_USER} --password-stdin"
script {
def tag = env.GIT_COMMIT[0..7]
def images = [
'user-management', 'lawyer-service', 'document-service',
'payment-service', 'notification-service', 'feedback-service',
'admin-panel', 'scraper-service', 'rag-service',
'knowledge-base-service', 'embedding-service',
'doc-worker', 'notification-worker', 'indexing-worker', 'lawyer-ingest-worker',
'frontend'
]
images.each { img ->
sh "docker push ${DOCKERHUB_REGISTRY}/${img}:${tag}"
sh "docker push ${DOCKERHUB_REGISTRY}/${img}:latest"
}
}
}
}
}
// ── Stage 9: Deploy Infrastructure (main branch only) ─────────────────
stage('Deploy Infrastructure') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
script {
sh "kubectl apply -f infrastructure/k8s/namespace.yaml"
sh "kubectl apply -f infrastructure/k8s/databases/"
sh "kubectl apply -f infrastructure/k8s/monitoring/"
sh "kubectl apply -f infrastructure/k8s/gateway/"
sh "kubectl rollout restart deployment/kong -n ${NAMESPACE}"
sh "kubectl apply -f infrastructure/k8s/ingress.yaml"
// Install Metrics Server for HPA (K3s requires --kubelet-insecure-tls)
sh """
if ! kubectl get deployment metrics-server -n kube-system &>/dev/null; then
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl patch deployment metrics-server -n kube-system \
--type=json \
-p='[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
fi
"""
def dbStatefulSets = [
'postgres-users', 'postgres-lawyers', 'postgres-documents',
'postgres-payments', 'postgres-rag', 'postgres-knowledge',
'postgres-notif', 'postgres-feedback', 'postgres-admin', 'postgres-scraping'
]
dbStatefulSets.each { db ->
sh "kubectl rollout status statefulset/${db} -n ${NAMESPACE} --timeout=180s"
}
sh "kubectl rollout status statefulset/redis -n ${NAMESPACE} --timeout=120s"
sh "kubectl rollout status statefulset/rabbitmq -n ${NAMESPACE} --timeout=120s"
sh "kubectl rollout status statefulset/qdrant -n ${NAMESPACE} --timeout=120s"
sh "kubectl rollout status statefulset/minio -n ${NAMESPACE} --timeout=120s"
sh "kubectl rollout status deployment/kong -n ${NAMESPACE} --timeout=120s"
}
}
}
// ── Stage 10: Deploy via Helm (main branch only) ──────────────────────
stage('Deploy') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
script {
// Remove any releases stuck in failed/pending state from previous runs
sh """
helm list --failed --short -n ${NAMESPACE} 2>/dev/null | xargs -r helm uninstall -n ${NAMESPACE} || true
helm list --pending --short -n ${NAMESPACE} 2>/dev/null | xargs -r helm uninstall -n ${NAMESPACE} || true
"""
def tag = env.GIT_COMMIT[0..7]
def charts = [
[chart: 'user-management', release: 'user-management'],
[chart: 'lawyer-service', release: 'lawyer-service'],
[chart: 'document-service', release: 'document-service'],
[chart: 'payment-service', release: 'payment-service'],
[chart: 'notification-service', release: 'notification-service'],
[chart: 'feedback-service', release: 'feedback-service'],
[chart: 'admin-panel', release: 'admin-panel'],
[chart: 'scraper-service', release: 'scraper-service'],
[chart: 'rag-service', release: 'rag-service'],
[chart: 'knowledge-base-service', release: 'knowledge-base-service'],
[chart: 'embedding-service', release: 'embedding-service'],
[chart: 'doc-worker', release: 'doc-worker'],
[chart: 'notification-worker', release: 'notification-worker'],
[chart: 'indexing-worker', release: 'indexing-worker'],
[chart: 'lawyer-ingest-worker', release: 'lawyer-ingest-worker'],
[chart: 'frontend', release: 'frontend'],
]
charts.each { c ->
sh """
helm upgrade --install ${c.release} infrastructure/helm/${c.chart} \
--namespace ${NAMESPACE} \
--create-namespace \
--set image.tag=${tag} \
--cleanup-on-fail \
--wait --timeout 300s
"""
}
// Deploy Ollama (uses public image, no image.tag needed)
sh """
helm upgrade --install ollama infrastructure/helm/ollama \
--namespace ${NAMESPACE} \
--wait --timeout 600s
"""
}
}
}
// ── Stage 11: Health Check (main branch only) ─────────────────────────
stage('Health Check') {
when { expression { env.GIT_BRANCH == 'origin/main' } }
steps {
script {
def deployments = [
'user-management', 'lawyer-service', 'document-service',
'payment-service', 'notification-service', 'feedback-service',
'admin-panel', 'scraper-service', 'rag-service',
'knowledge-base-service', 'embedding-service',
'doc-worker', 'notification-worker', 'indexing-worker', 'lawyer-ingest-worker',
'frontend', 'ollama'
]
deployments.each { d ->
sh "kubectl rollout status deployment/${d} -n ${NAMESPACE} --timeout=90s"
}
sh "kubectl get pods -n ${NAMESPACE}"
}
}
}
}
post {
success {
echo "Pipeline passed — all ${env.GIT_COMMIT[0..7]} images deployed to K3s."
}
failure {
echo "Pipeline FAILED at stage: ${env.STAGE_NAME}. Check logs above."
}
}
}