-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
192 lines (160 loc) · 5.91 KB
/
Jenkinsfile
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
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS') // set timeout 1 hour
}
environment {
TIME_ZONE = 'Asia/Seoul'
//github
TARGET_BRANCH = 'develop'
REPOSITORY_URL= 'https://github.com/TEAM-FLOWBIT/BOARD_SERVICE_MSA.git'
//docker-hub
registryCredential = 'docker-hub'
CONTAINER_NAME = 'flowbit-boardservice'
IMAGE_NAME = 'kimminwoo1234/flowbit-boardservice'
}
stages {
stage('init') {
steps {
echo 'init stage'
deleteDir()
}
post {
success {
echo 'success init in pipeline'
}
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'fail init in pipeline'
}
}
}
stage('Prepare') {
steps {
echo 'Cloning Repository'
git branch: 'develop',
url: 'https://github.com/TEAM-FLOWBIT/BOARD_SERVICE_MSA.git'
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'This pipeline stops here...'
}
}
}
// 일단은 merge 하기전에 테스트통과함으로 테스트없이 빌드
stage('Build Maven') {
steps {
echo 'Build Maven'
dir('.'){
sh '''
pwd
cd /var/jenkins_home/workspace/flowbit-boardservice
./mvnw clean package -DskipTests
'''
}
}
post {
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'This pipeline stops here...'
}
}
}
// 도커 이미지를 만든다. build number로 latest 태그 부여한다.
stage('Build Docker') {
steps {
echo 'Build Docker'
sh """
cd /var/jenkins_home/workspace/flowbit-boardservice
docker build -t $IMAGE_NAME:latest .
"""
}
post {
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'This pipeline stops here...'
}
}
}
// 빌드넘버 latest
stage('Push Docker') {
steps {
echo 'Push Docker'
script {
docker.withRegistry('', registryCredential) {
docker.image("${IMAGE_NAME}:latest").push()
}
}
}
post {
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'This pipeline stops here...'
}
}
}
stage('rm container and rm images') {
steps {
echo 'rm container stage'
sh '''
docker rm -f $CONTAINER_NAME
docker image prune -f --filter "label=${IMAGE_NAME}"
'''
}
post {
success {
echo 'success rm container in pipeline'
}
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
error 'fail rm container in pipeline'
}
}
}
stage('Docker run') {
steps {
echo 'Pull Docker Image & Docker Image Run'
sh """
docker run -d \
--name ${CONTAINER_NAME} \
--restart always \
--network likelion_default \
-e SPRING_DATASOURCE_PASSWORD=${env.SPRING_DATASOURCE_PASSWORD} \
--link db-mysql \
--link discovery-service \
--link apigateway-service \
--link rabbitmq \
--link config-server \
${IMAGE_NAME}:latest
"""
}
post {
failure {
echo 'Docker Run failure !'
}
success {
echo 'Docker Run Success !'
}
}
}
stage('Clean Up Docker Images on Jenkins Server') {
steps {
echo 'Cleaning up unused Docker images on Jenkins server'
// Clean up unused Docker images, including those created within the last hour
sh "docker image prune -f --all --filter \"until=1m\""
}
}
}
post {
success {
slackSend (channel: '#backend', color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: '#backend', color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}