-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jenkinsfile
188 lines (159 loc) · 6.29 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
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/rework-kr/REWORK-SERVER'
//docker-hub
registryCredential = 'docker-hub'
CONTAINER_NAME = 'rework-backend'
IMAGE_NAME = 'kimminwoo1234/rework-backend'
}
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/rework-kr/REWORK-SERVER.git',
}
// post {
// success {
// echo 'Successfully Cloned Repository'
// }
// // failure {
// // slackSend (channel: '#backend', color: '#FF0000', message: "${env.CONTAINER_NAME} CI / CD 파이프라인 구동 실패, 젠킨스 확인 해주세요")
// // error 'This pipeline stops here...'
// // }
// }
}
stage('Build Gradle') {
steps {
echo 'Build Gradle'
dir('.'){
sh '''
pwd
cd /var/jenkins_home/workspace/rework-backend
chmod +x ./gradlew
./gradlew build
'''
}
}
// 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/rework-backend
docker build -t $IMAGE_NAME:$BUILD_NUMBER .
docker tag $IMAGE_NAME:$BUILD_NUMBER $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}:${BUILD_NUMBER}").push()
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'
script {
docker.withRegistry('', registryCredential) {
sshagent (credentials: ['ssh']) {
sh "ssh -o StrictHostKeyChecking=no ubuntu@43.202.187.4 'cd rework && sed -i \"s/^BUILD_NUMBER=.*/BUILD_NUMBER=${BUILD_NUMBER}/\" .env'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@43.202.187.4 'cd rework && ./deploy.sh'"
sh "ssh -o StrictHostKeyChecking=no ubuntu@43.202.187.4 'docker image prune -f --all'"
}
}
}
}
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\""
sh "docker image prune -f --all"
}
}
}
// 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})")
// }
// }
}