-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
114 lines (100 loc) · 4.76 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
pipeline {
agent any
stages {
stage('Set Variables') {
steps {
echo 'Set Variables'
script {
// BASIC
PROJECT_NAME = 'beat'
REPOSITORY_URL = 'https://github.com/TEAM-BEAT/BEAT-SERVER.git'
PROD_BRANCH = 'main'
DEV_BRANCH = 'develop'
BRANCH_NAME = env.BRANCH_NAME
OPERATION_ENV = BRANCH_NAME.equals(PROD_BRANCH) ? 'prod' : 'dev'
// DOCKER
DOCKER_HUB_URL = 'registry.hub.docker.com'
DOCKER_HUB_FULL_URL = 'https://' + DOCKER_HUB_URL
DOCKER_HUB_DEV_CREDENTIAL_ID = 'DOCKER_HUB_DEV_CREDENTIALS'
DOCKER_HUB_PROD_CREDENTIAL_ID = 'DOCKER_HUB_PROD_CREDENTIALS'
DOCKER_IMAGE_NAME = BRANCH_NAME.equals(PROD_BRANCH) ? 'donghoon0203/beat-prod' : 'hoonyworld/beat-dev'
// SSH
SSH_CREDENTIAL_ID = OPERATION_ENV.toUpperCase() + '_SSH'
SSH_PORT_CREDENTIAL_ID = OPERATION_ENV.toUpperCase() + '_SSH_PORT'
SSH_HOST_CREDENTIAL_ID = OPERATION_ENV.toUpperCase() + '_SSH_HOST'
// PORT
PORT_PROPERTIES_FILE = 'application-' + OPERATION_ENV + '.yml'
}
}
}
stage('Parse Internal Port') {
steps {
script {
INTERNAL_PORT = sh(script: "yq e '.server.port' ./src/main/resources/${PORT_PROPERTIES_FILE}", returnStdout: true).trim()
echo "Internal port: ${INTERNAL_PORT}"
}
}
}
stage('Git Checkout') {
steps {
echo 'Checkout Remote Repository'
git branch: "${env.BRANCH_NAME}",
url: REPOSITORY_URL
}
}
stage('Deploy to Server') {
steps {
echo 'Deploy to Server'
script {
def DOCKER_HUB_CREDENTIAL_ID = BRANCH_NAME.equals(PROD_BRANCH) ? DOCKER_HUB_PROD_CREDENTIAL_ID : DOCKER_HUB_DEV_CREDENTIAL_ID
withCredentials([
usernamePassword(credentialsId: DOCKER_HUB_CREDENTIAL_ID,
usernameVariable: 'DOCKER_HUB_ID',
passwordVariable: 'DOCKER_HUB_PW'),
sshUserPrivateKey(credentialsId: SSH_CREDENTIAL_ID,
keyFileVariable: 'KEY_FILE',
usernameVariable: 'USERNAME'),
string(credentialsId: SSH_HOST_CREDENTIAL_ID, variable: 'HOST'),
string(credentialsId: SSH_PORT_CREDENTIAL_ID, variable: 'PORT')]) {
def remote = [:]
remote.name = OPERATION_ENV
remote.host = HOST
remote.user = USERNAME
remote.identityFile = KEY_FILE
remote.port = 22
remote.allowAnyHosts = true
// SSH 연결 테스트
sshCommand remote: remote, command: 'echo "SSH 연결 성공"'
// Docker 이미지 pull
sshCommand remote: remote, command: 'docker pull ' + DOCKER_IMAGE_NAME + ":latest"
// 기존 컨테이너 제거
sshCommand remote: remote, failOnError: false, command: 'docker rm -f springboot'
// 새로운 컨테이너 실행
sshCommand remote: remote, command: (
'docker run -d --name springboot' +
' --network beat-network' +
' -p 8080:' + INTERNAL_PORT +
' -e "SPRING_PROFILES_ACTIVE=' + OPERATION_ENV + '"' +
' ' + DOCKER_IMAGE_NAME + ':latest'
)
// Docker dangling 이미지 정리
sshCommand remote: remote, command: '''
docker images -f "dangling=true" -q | \
xargs -r docker rmi
'''
}
}
}
}
}
post {
success {
sh 'echo "success"'
slackSend(color: '#00FF00', message: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
unsuccessful {
sh 'echo "fail"'
slackSend(color: '#FF0000', message: "FAIL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}