-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJenkinsfile
More file actions
174 lines (160 loc) · 5.63 KB
/
Jenkinsfile
File metadata and controls
174 lines (160 loc) · 5.63 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
def containerImages = []
pipeline {
agent any;
environment {
IMAGE_STORAGE_CREDENTIAL = 'dockerhub-access-token'
IMAGE_STORAGE = 'https://registry.hub.docker.com'
}
stages {
stage('Docker Login Test') {
steps {
script {
echo 'Testing Docker Login...'
docker.withRegistry(IMAGE_STORAGE, IMAGE_STORAGE_CREDENTIAL) {
echo 'Docker login successful!'
}
}
}
post {
failure {
script {
echo 'Docker login failed. Exiting...'
currentBuild.result = 'FAILURE'
error('Stopping the pipeline due to Docker login failure.')
}
}
}
}
stage('Checkout') {
steps {
script {
echo 'Checking out code...'
}
}
post {
failure {
sendErrorNotification('Checkout')
}
}
}
stage('Parallel Build') {
parallel {
stage('Nginx Docker Build') {
steps {
script {
echo 'Starting Nginx Build...'
containerImages.push(buildDockerImage(
directory: 'nginx',
imageName: 'leovim5072/moheng-nginx:latest',
context: '-f Dockerfile.prod ../'
))
echo 'Nginx Build Completed!'
}
}
post {
failure {
sendErrorNotification('Nginx Build')
}
}
}
stage('Backend Docker Build') {
steps {
script {
echo 'Starting Backend Build...'
containerImages.push(buildDockerImage(
directory: 'backend',
imageName: 'leovim5072/moheng-backend:latest',
context: '-f Dockerfile.prod .'
))
echo 'Backend Build Completed!'
}
}
post {
failure {
sendErrorNotification('Backend Build')
}
}
}
stage('AI Docker Build') {
steps {
script {
echo 'Starting AI Build...'
containerImages.push(buildDockerImage(
directory: 'ai',
imageName: 'leovim5072/moheng-ai:latest',
context: '-f Dockerfile .'
))
echo 'AI Build Completed!'
}
}
post {
failure {
sendErrorNotification('AI Build')
}
}
}
}
}
stage('Upload Container Registry') {
steps {
script {
echo 'Uploading images to container registry...'
pushContainerRegistry(images: containerImages, targetBranch: 'develop')
echo 'Upload Completed!'
}
}
post {
failure {
sendErrorNotification('Upload Container Registry')
}
}
}
}
post {
always {
script {
echo "Pipeline Completed!"
def buildStatus = currentBuild.result ?: 'SUCCESS'
def durationMinutes = (currentBuild.duration / 60000).intValue()
def durationSeconds = ((currentBuild.duration % 60000) / 1000).intValue()
def duration = "${durationMinutes}m ${durationSeconds}s"
sendDiscordNotification(buildStatus, duration)
}
}
}
}
def buildDockerImage(Map params) {
def directory = params.directory
def imageName = params.imageName
def context = params.context
dir(directory) {
def image = docker.build(imageName, context)
return [image: image, imageName: imageName]
}
}
def pushContainerRegistry(Map params) {
def images = params.images
def targetBranch = params.targetBranch
if (env.BRANCH_NAME == targetBranch) {
images.each { imageData ->
def image = imageData.image
def imageName = imageData.imageName
echo "Pushing image ${imageName} to container registry..."
docker.withRegistry(IMAGE_STORAGE, IMAGE_STORAGE_CREDENTIAL) {
image.push("latest")
}
}
}
}
def sendDiscordNotification(buildStatus, duration) {
withCredentials([string(credentialsId: 'discord-webhook', variable: 'DISCORD')]) {
discordSend description: """
제목 : ${currentBuild.displayName}
결과 : ${buildStatus}
실행 시간 : ${duration}
""",
link: env.BUILD_URL, result: buildStatus,
title: "${env.JOB_NAME} : ${currentBuild.displayName} ${buildStatus}",
webhookURL: "$DISCORD"
}
}