-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJenkinsfile
More file actions
96 lines (84 loc) · 3.17 KB
/
Jenkinsfile
File metadata and controls
96 lines (84 loc) · 3.17 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
pipeline {
agent any
environment {
VITE_BASE_URL = ''
}
stages {
stage('Detect changed services') {
steps {
script {
def changedPaths = sh(
script: '''git diff --name-only HEAD~1 HEAD''',
returnStdout: true
).trim().split('\n')
def targets = []
for (path in changedPaths) {
if (path.startsWith('frontend/')) {
targets << 'frontend'
} else if (path.startsWith('backend/')) {
def subdir = path.split('/')[1]
if (['agent-service', 'storage-service', 'notification-service', 'user-service', 'auth-service', 'job-service', 'schedule-service'].contains(subdir)) {
targets << "backend-${subdir}"
}
}
}
env.CHANGED_TARGETS = targets.unique().join(',')
echo "Changed targets: ${env.CHANGED_TARGETS}"
}
}
}
stage('Build & Push Docker Images') {
when {
expression { return env.CHANGED_TARGETS }
}
steps {
script {
def targets = env.CHANGED_TARGETS.split(',')
def BUILD_NUMBER = env.BUILD_NUMBER
def GIT_COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
withCredentials([
usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')
]) {
for (target in targets) {
def contextPath = ''
def imageName = ''
def imageTag = "${GIT_COMMIT_SHORT}-${BUILD_NUMBER}"
if (target == 'frontend') {
contextPath = 'frontend'
imageName = "k12s101ss/react"
withCredentials([string(credentialsId: 'VITE_BASE_URL', variable: 'VITE_BASE_URL')]) {
dir(contextPath) {
sh """
echo \$DOCKER_PASS | docker login -u \$DOCKER_USER --password-stdin
docker build \\
--build-arg VITE_BASE_URL=\$VITE_BASE_URL \\
-t ${imageName}:${imageTag} -t ${imageName}:latest .
docker push ${imageName}:${imageTag}
docker push ${imageName}:latest
"""
}
}
} else if (target.startsWith('backend-')) {
def serviceName = target.replace('backend-', '')
contextPath = "backend/${serviceName}"
imageName = "k12s101ss/${serviceName}"
}
echo "Building and pushing ${imageName}"
dir(contextPath) {
if (target == 'backend-user-service') {
sh './gradlew clean build -x test'
}
sh """
echo \$DOCKER_PASS | docker login -u \$DOCKER_USER --password-stdin
docker build -t ${imageName}:${imageTag} -t ${imageName}:latest .
docker push ${imageName}:${imageTag}
docker push ${imageName}:latest
"""
}
}
}
}
}
}
}
}