forked from brush-buddy/brush-buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
250 lines (226 loc) · 10.1 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
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
pipeline {
environment{
back_repository = "fangdol888onssafy/brush-buddy-spring"
front_repository = "fangdol888onssafy/brush-buddy-vue"
ai_repository = "summa1234/brushbuddy"
back_img_name = "brush-buddy-spring"
front_img_name ="brush-buddy-vue"
ai_img_name = "brush-buddy-ai"
docker_compose_back_name = "spring"
docker_compose_front_name = "vue"
docker_compose_ai_name = "ai"
DOCKERHUB_CREDENTIALS = credentials("my-dockerhub-token")
dockerImage = ''
SSH_CONNECTION = "[email protected]"
}
agent any
stages {
stage('Git Clone') {
steps {
git branch: 'deploy', credentialsId: "my-gitlab-token", url: 'https://lab.ssafy.com/s10-webmobile3-sub2/S10P12A205.git'
}
}
stage('Login'){
steps{
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin' // docker hub 로그인
}
}
stage('BE - Build') {
steps {
dir("./Backend/brush-buddy") {
sh "./gradlew clean build"
}
}
}
stage('BE - Docker Image Build') {
steps {
dir("./Backend/brush-buddy") {
script{
dockerImage = docker.build back_repository
}
}
}
}
stage('BE - Docker Image Push') {
steps {
echo 'Push Docker'
script{
dockerImage.push("latest")
}
}
}
stage('FE - Build'){
steps{
dir("./Frontend/brush-buddy"){
sh "npm install"
sh "npm run build"
}
}
}
stage('FE - Docker Image Build') {
steps {
dir("./Frontend/brush-buddy") {
script{
dockerImage = docker.build front_repository
}
}
}
}
stage('FE - Docker Image Push') {
steps {
echo 'Push Docker'
script{
dockerImage.push("latest")
}
}
}
stage('BE - Remove Stopped Containers') {
steps {
script {
// Find stopped containers with the specified name
def stoppedContainers = sh(script: "ssh -t ${SSH_CONNECTION} \'docker ps -a --filter \"name=${back_img_name}\" --filter \"status=exited\" --format \'{{.ID}}\'\'", returnStdout: true).trim()
// Check if there are any stopped containers
if (stoppedContainers) {
// Remove stopped containers
sh "ssh -t ${SSH_CONNECTION} 'docker rm ${stoppedContainers}'"
echo "Stopped containers with name '${back_img_name}' removed."
} else {
echo "No stopped containers with name '${back_img_name}' found."
}
}
}
}
stage('BE - Deploy') {
steps {
sshagent (credentials: ['aws_key']) {
script{
sh "ssh -o StrictHostKeyChecking=no ${SSH_CONNECTION} uptime"
// sh "ssh -t ${SSH_CONNECTION} 'docker container prune -f'" // 오류난 컨테이너 종료
//실행중인 컨테이너 종료 및 삭제
script {
def existingContainerId = sh(script: "ssh -t ${SSH_CONNECTION} 'docker ps -q -f name=${back_img_name}'", returnStdout: true).trim()
if (existingContainerId) {
sh "ssh -t ${SSH_CONNECTION} 'docker stop ${existingContainerId}'"
sh "ssh -t ${SSH_CONNECTION} 'docker rm ${existingContainerId}'"
} else {
echo "No existing container with the name ${back_img_name} found."
}
}
script {
// 기존 이미지 삭제
def existingImageId = sh(script: "ssh -t ${SSH_CONNECTION} 'docker images -q ${back_repository}:latest'", returnStdout: true).trim()
if (existingImageId) {
sh "ssh -t ${SSH_CONNECTION} 'docker rmi ${existingImageId}'"
} else {
echo "No existing image with the name ${back_repository}:latest found."
}
}
//docker-compose
sh "ssh -t ${SSH_CONNECTION} 'docker-compose pull ${docker_compose_back_name}'"
sh "ssh -t ${SSH_CONNECTION} 'docker-compose up -d ${docker_compose_back_name}'"
}
}
}
}
stage('FE - Remove Stopped Containers') {
steps {
script {
// Find stopped containers with the specified name
def stoppedContainers = sh(script: "ssh -t ${SSH_CONNECTION} \'docker ps -a --filter \"name=${front_img_name}\" --filter \"status=exited\" --format \'{{.ID}}\'\'", returnStdout: true).trim()
// Check if there are any stopped containers
if (stoppedContainers) {
// Remove stopped containers
sh "ssh -t ${SSH_CONNECTION} 'docker rm ${stoppedContainers}'"
echo "Stopped containers with name '${front_img_name}' removed."
} else {
echo "No stopped containers with name '${front_img_name}' found."
}
}
}
}
stage('FE - Deploy') {
steps {
sshagent (credentials: ['aws_key']) {
script{
sh "ssh -o StrictHostKeyChecking=no ${SSH_CONNECTION} uptime"
//실행중인 컨테이너 종료 및 삭제
script {
def existingContainerId = sh(script: "ssh -t ${SSH_CONNECTION} 'docker ps -q -f name=${front_img_name}'", returnStdout: true).trim()
if (existingContainerId) {
sh "ssh -t ${SSH_CONNECTION} 'docker stop ${existingContainerId}'"
sh "ssh -t ${SSH_CONNECTION} 'docker rm ${existingContainerId}'"
} else {
echo "No existing container with the name ${front_img_name} found."
}
}
script {
// 기존 이미지 삭제
def existingImageId = sh(script: "ssh -t ${SSH_CONNECTION} 'docker images -q ${front_repository}:latest'", returnStdout: true).trim()
if (existingImageId) {
sh "ssh -t ${SSH_CONNECTION} 'docker rmi ${existingImageId}'"
} else {
echo "No existing image with the name ${front_repository}:latest found."
}
}
//docker-compose
sh "ssh -t ${SSH_CONNECTION} 'docker-compose pull ${docker_compose_front_name}'"
sh "ssh -t ${SSH_CONNECTION} 'docker-compose up -d ${docker_compose_front_name}'"
}
}
}
}
stage('AI - Docker Image Build') {
steps {
dir("./Ai/brush-buddy") {
script{
dockerImage = docker.build ai_repository
}
}
}
}
stage('AI - Docker Image Push') {
steps {
echo 'Push Docker'
script{
dockerImage.push("latest")
}
}
}
stage('AI - Deploy') {
steps {
sshagent (credentials: ['aws_key']) {
script{
sh "ssh -o StrictHostKeyChecking=no ${SSH_CONNECTION} uptime"
//docker-compose
sh "ssh -t ${SSH_CONNECTION} 'docker-compose pull ${docker_compose_ai_name}'"
sh "ssh -t ${SSH_CONNECTION} 'docker-compose up -d ${docker_compose_ai_name}'"
}
}
}
}
}
post {
success {
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (color: 'good',
message: "빌드 성공: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)",
endpoint: 'https://meeting.ssafy.com/hooks/sztjehk9gf85dgxu5cps7u8exy',
channel: 'jenkins'
)
}
}
failure {
script {
def Author_ID = sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Name = sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
mattermostSend (color: 'danger',
message: "빌드 실패: ${env.JOB_NAME} #${env.BUILD_NUMBER} by ${Author_ID}(${Author_Name})\n(<${env.BUILD_URL}|Details>)",
endpoint: 'https://meeting.ssafy.com/hooks/sztjehk9gf85dgxu5cps7u8exy',
channel: 'jenkins'
)
}
}
}
}