-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
293 lines (275 loc) · 11.7 KB
/
Copy pathJenkinsfile
File metadata and controls
293 lines (275 loc) · 11.7 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
pipeline {
agent any
parameters {
string(name: 'DOCKERHUB_REPO', defaultValue: 'hieupahmet', trim: true, description: 'Docker Hub repository namespace')
string(name: 'RAG_REPO_URL', defaultValue: 'https://github.com/HieuPahm-R2/Medical_RAG_PJI_latest.git', trim: true, description: 'Git URL for the RAG service repository')
string(name: 'DEPLOY_HOST', defaultValue: '', trim: true, description: 'Production VPS hostname or IP')
string(name: 'DEPLOY_USER', defaultValue: 'root', trim: true, description: 'SSH user for deployment')
string(name: 'DEPLOY_PATH', defaultValue: '/opt/pji-advisor', trim: true, description: 'Remote directory containing docker-compose.yml')
booleanParam(name: 'RUN_SONAR', defaultValue: false, description: 'Run SonarQube analysis')
booleanParam(name: 'DEPLOY', defaultValue: false, description: 'Deploy after pushing images')
}
environment {
RAG_SERVICE_DIR = 'Medical_RAG_PJI_latest'
DOCKER_BUILDKIT = '1'
}
options {
timeout(time: 45, unit: 'MINUTES')
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
timestamps()
}
stages {
stage('Checkout') {
steps {
checkout scm
script {
env.GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.SAFE_BRANCH = (env.BRANCH_NAME ?: 'manual').replaceAll('[^A-Za-z0-9_.-]', '-')
env.IMAGE_TAG = "${env.SAFE_BRANCH}-${env.BUILD_NUMBER}"
}
sh '''
if [ ! -d "${RAG_SERVICE_DIR}/.git" ]; then
git clone --depth 1 "${RAG_REPO_URL}" "${RAG_SERVICE_DIR}"
fi
'''
}
}
stage('Validate Layout') {
steps {
sh '''
test -f backend/pom.xml
test -f backend/Dockerfile
test -f frontend/package.json
test -f frontend/Dockerfile
test -f docker-compose.yml
test -f Caddyfile
test -f "${RAG_SERVICE_DIR}/pyproject.toml"
test -f "${RAG_SERVICE_DIR}/Dockerfile"
'''
}
}
stage('Test') {
parallel {
stage('Backend') {
steps {
dir('backend') {
sh './mvnw -B test -Dspring.profiles.active=test'
}
}
post {
always {
junit testResults: 'backend/target/surefire-reports/*.xml', allowEmptyResults: true
}
}
}
stage('Frontend') {
steps {
dir('frontend') {
sh '''
npm ci
npm run build
'''
}
}
}
stage('RAG Service') {
steps {
dir("${env.RAG_SERVICE_DIR}") {
sh '''
python3 -m pip install --upgrade pip uv
uv sync --frozen --dev
uv run pytest --junitxml=test-results.xml -v
'''
}
}
post {
always {
junit testResults: "${env.RAG_SERVICE_DIR}/test-results.xml", allowEmptyResults: true
}
}
}
}
}
stage('SonarQube Analysis') {
when {
expression { return params.RUN_SONAR }
}
steps {
withSonarQubeEnv('sonarqube') {
dir('backend') {
sh './mvnw -B sonar:sonar -Dsonar.projectKey=pji-backend -Dsonar.projectName="PJI Backend"'
}
dir('frontend') {
sh '''
npm ci
npx sonar-scanner \
-Dsonar.projectKey=pji-frontend \
-Dsonar.projectName="PJI Frontend" \
-Dsonar.sources=src
'''
}
dir("${env.RAG_SERVICE_DIR}") {
sh '''
npx sonar-scanner \
-Dsonar.projectKey=pji-rag-service \
-Dsonar.projectName="PJI RAG Service" \
-Dsonar.sources=app \
-Dsonar.python.version=3.11
'''
}
}
}
}
stage('Quality Gate') {
when {
expression { return params.RUN_SONAR }
}
steps {
timeout(time: 10, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage('Build Images') {
parallel {
stage('Backend Image') {
steps {
dir('backend') {
sh """
docker build \
-t ${params.DOCKERHUB_REPO}/pji-backend:${env.IMAGE_TAG} \
-t ${params.DOCKERHUB_REPO}/pji-backend:latest \
.
"""
}
}
}
stage('Frontend Image') {
steps {
dir('frontend') {
sh """
docker build \
-t ${params.DOCKERHUB_REPO}/pji-frontend:${env.IMAGE_TAG} \
-t ${params.DOCKERHUB_REPO}/pji-frontend:latest \
.
"""
}
}
}
stage('RAG Image') {
steps {
dir("${env.RAG_SERVICE_DIR}") {
sh """
docker build \
-t ${params.DOCKERHUB_REPO}/pji-rag-service:${env.IMAGE_TAG} \
-t ${params.DOCKERHUB_REPO}/pji-rag-service:latest \
.
"""
}
}
}
}
}
stage('Push Images') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh '''
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
docker push "${DOCKERHUB_REPO}/pji-backend:${IMAGE_TAG}"
docker push "${DOCKERHUB_REPO}/pji-backend:latest"
docker push "${DOCKERHUB_REPO}/pji-frontend:${IMAGE_TAG}"
docker push "${DOCKERHUB_REPO}/pji-frontend:latest"
docker push "${DOCKERHUB_REPO}/pji-rag-service:${IMAGE_TAG}"
docker push "${DOCKERHUB_REPO}/pji-rag-service:latest"
docker logout
'''
}
}
}
stage('Approve Production Deploy') {
when {
allOf {
expression { return params.DEPLOY }
branch 'main'
}
}
steps {
timeout(time: 15, unit: 'MINUTES') {
input message: "Deploy build ${env.IMAGE_TAG} to production?", ok: 'Deploy'
}
}
}
stage('Deploy to Production') {
when {
allOf {
expression { return params.DEPLOY }
branch 'main'
}
}
steps {
script {
if (!params.DEPLOY_HOST?.trim()) {
error('DEPLOY_HOST must be provided when DEPLOY=true')
}
}
sshagent(credentials: ['deploy-server-ssh-key']) {
sh '''
ssh -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p '${DEPLOY_PATH}/docker'"
scp -o StrictHostKeyChecking=no docker-compose.yml Caddyfile "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}/"
scp -o StrictHostKeyChecking=no -r docker/* "${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}/docker/"
ssh -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "
cd '${DEPLOY_PATH}' && \
DOCKERHUB_REPO='${DOCKERHUB_REPO}' IMAGE_TAG='${IMAGE_TAG}' docker compose pull pji-backend pji-frontend pji-rag-service caddy && \
DOCKERHUB_REPO='${DOCKERHUB_REPO}' IMAGE_TAG='${IMAGE_TAG}' docker compose up -d --remove-orphans caddy pji-backend pji-frontend pji-rag-service
"
'''
}
}
}
stage('Smoke Test') {
when {
allOf {
expression { return params.DEPLOY }
branch 'main'
}
}
steps {
sshagent(credentials: ['deploy-server-ssh-key']) {
sh '''
ssh -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" '
set -eu
for container in pji-backend pji-frontend pji-rag-service pji-caddy; do
tries=0
while [ "$tries" -lt 30 ]; do
status="$(docker inspect --format="{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}" "$container" 2>/dev/null || true)"
if [ "$status" = "healthy" ] || [ "$status" = "running" ]; then
break
fi
tries=$((tries + 1))
sleep 5
done
status="$(docker inspect --format="{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}" "$container" 2>/dev/null || true)"
if [ "$status" != "healthy" ] && [ "$status" != "running" ]; then
echo "Container $container is not healthy: $status"
exit 1
fi
done
curl -fsS http://localhost >/dev/null
'
'''
}
}
}
}
post {
always {
sh '''
docker image rm "${DOCKERHUB_REPO}/pji-backend:${IMAGE_TAG}" 2>/dev/null || true
docker image rm "${DOCKERHUB_REPO}/pji-frontend:${IMAGE_TAG}" 2>/dev/null || true
docker image rm "${DOCKERHUB_REPO}/pji-rag-service:${IMAGE_TAG}" 2>/dev/null || true
'''
cleanWs()
}
}
}