-
Notifications
You must be signed in to change notification settings - Fork 1
/
jenkinsfile
101 lines (93 loc) · 3.88 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
pipeline {
agent any
environment{
TARGET_HOST = "[email protected]"
IMAGE_NAME = "spring-server"
CONTAINER_1_NAME ="spring-server-1"
CONTAINER_2_NAME ="spring-server-2"
CONTAINER_3_NAME ="spring-server-3"
LAST_VERSION = "latest"
NEW_VERSION = "latest"
PROJECT_PATH = "/home/ubuntu/app"
RESOURCE_PATH = "/home/ubuntu/jenkins-data/workspace/back/src/main/resources"
}
stages {
stage('Clone') {
steps {
git branch: 'backend', credentialsId: 'mungplace', url: 'https://lab.ssafy.com/s11-bigdata-dist-sub1/S11P21E106'
}
}
stage('Build'){
steps{
sshagent (credentials: ['SSH-Credential']){
script{
sh 'chmod +x gradlew'
sh './gradlew clean build -x test'
}
}
}
}
stage('Build and Replace') {
steps {
sshagent (credentials: ['SSH-Credential']) {
script {
sh 'scp -o StrictHostKeyChecking=no ./dockerfile ${TARGET_HOST}:${PROJECT_PATH}'
sh 'scp -o StrictHostKeyChecking=no ./build/libs/mungplace-0.0.1-SNAPSHOT.jar ${TARGET_HOST}:${PROJECT_PATH}'
// 비밀 파일을 작업 공간으로 가져오기
withCredentials([file(credentialsId: 'application-prod.yml', variable: 'PROD_YML'), file(credentialsId: 'application-secret.yml', variable: 'SECRET_YML')]) {
sh'''
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "rm -f ${PROJECT_PATH}/application-prod.yml ${PROJECT_PATH}/application-secret.yml"
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "rm -f ${RESOURCE_PATH}/application-prod.yml ${RESOURCE_PATH}/application-secret.yml"
'''
// 비밀 파일을 대상 서버로 전송
sh 'scp -o StrictHostKeyChecking=no $PROD_YML ${TARGET_HOST}:${PROJECT_PATH}'
sh 'scp -o StrictHostKeyChecking=no $SECRET_YML ${TARGET_HOST}:${PROJECT_PATH}'
}
}
}
}
}
stage('Stop process'){
steps{
sshagent (credentials: ['SSH-Credential']){
script{
try{
sh'''
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "docker compose -f backend-compose.yml down"
'''
} catch (e){
echo 'Remove Container Failed : ${CONTAINER_NAME}'
}
}
}
}
}
stage('Remove Docker Image') {
steps {
sshagent (credentials: ['SSH-Credential']) {
sh'''
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "docker rmi ${IMAGE_NAME}:${LAST_VERSION}"
'''
}
}
}
stage('Build Docker Image') {
steps {
sshagent (credentials: ['SSH-Credential']) {
sh'''
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "docker build -f ${PROJECT_PATH}/dockerfile --tag ${IMAGE_NAME}:${NEW_VERSION} ${PROJECT_PATH}"
'''
}
}
}
stage('Deploy'){
steps{
sshagent (credentials: ['SSH-Credential']){
sh '''
ssh -o StrictHostKeyChecking=no ${TARGET_HOST} "docker compose -f backend-compose.yml up -d"
'''
}
}
}
}
}