-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
175 lines (158 loc) · 8.5 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
pipeline {
agent any
tools {
jdk("JAVA 17")
}
environment {
dockerImage = ''
APP_NAME = 'hwans-api-server'
IMAGE_NAME = 'akon47/hwans-api-server'
IMAGE_TAG = "${env.APP_VERSION}.${env.BUILD_NUMBER}"
ACTIVE_PROFILE = 'prod'
SPRING_PROD_PROPERTIES_PATH = "src/main/resources/application-${ACTIVE_PROFILE}.yml"
SPRING_DATASOURCE_URL = credentials('spring-datasource-url')
SPRING_DATASOURCE_USERNAME = credentials('spring-datasource-username')
SPRING_DATASOURCE_PASSWORD = credentials('spring-datasource-password')
SPRING_JWT_ACCESS_SECRET_KEY = credentials('spring-jwt-base64-access-secret')
SPRING_JWT_REFRESH_SECRET_KEY = credentials('spring-jwt-base64-refresh-secret')
SPRING_JWT_REGISTER_SECRET_KEY = credentials('spring-jwt-base64-register-secret')
SPRING_REDIS_HOST = credentials('spring-redis-host')
SPRING_REDIS_PORT = credentials('spring-redis-port')
SPRING_MAIL_HOST = credentials('spring-mail-host')
SPRING_MAIL_USERNAME = credentials('spring-mail-username')
SPRING_MAIL_PASSWORD = credentials('spring-mail-password')
SPRING_MAIL_PORT = credentials('spring-mail-port')
SPRING_OAUTH2_GOOGLE_CLIENT_ID = credentials('spring-oauth2-google-client-id')
SPRING_OAUTH2_GOOGLE_SECRET = credentials('spring-oauth2-google-secret')
SPRING_OAUTH2_GITHUB_CLIENT_ID = credentials('spring-oauth2-github-client-id')
SPRING_OAUTH2_GITHUB_SECRET = credentials('spring-oauth2-github-secret')
SPRING_OAUTH2_FACEBOOK_CLIENT_ID = credentials('spring-oauth2-facebook-client-id')
SPRING_OAUTH2_FACEBOOK_SECRET = credentials('spring-oauth2-facebook-secret')
SPRING_OAUTH2_NAVER_CLIENT_ID = credentials('spring-oauth2-naver-client-id')
SPRING_OAUTH2_NAVER_SECRET = credentials('spring-oauth2-naver-secret')
SPRING_OAUTH2_KAKAO_CLIENT_ID = credentials('spring-oauth2-kakao-client-id')
SPRING_OAUTH2_KAKAO_SECRET = credentials('spring-oauth2-kakao-secret')
SPRING_OAUTH2_DISCORD_CLIENT_ID = credentials('spring-oauth2-discord-client-id')
SPRING_OAUTH2_DISCORD_SECRET = credentials('spring-oauth2-discord-secret')
SPRING_OAUTH2_MICROSOFT_CLIENT_ID = credentials('spring-oauth2-microsoft-client-id')
SPRING_OAUTH2_MICROSOFT_SECRET = credentials('spring-oauth2-microsoft-secret')
GITHUB_CREDENTIALS_ID = 'git-hub'
DOCKER_CREDENTIALS_ID = 'docker-hub'
}
stages {
stage('Clone') {
steps {
echo 'Clonning Repository'
git url: '[email protected]:akon47/hwans-api-server.git', branch: 'master', credentialsId: GITHUB_CREDENTIALS_ID, changelog: false
}
post {
success {
echo 'Successfully Cloned Repository'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Prepare') {
steps {
echo "Pre-Processing for ${ACTIVE_PROFILE} profile"
script {
prodProperties = readFile file: SPRING_PROD_PROPERTIES_PATH
prodProperties = prodProperties.replaceAll(/\{datasource-url\}/, SPRING_DATASOURCE_URL)
prodProperties = prodProperties.replaceAll(/\{jwt-base64-access-secret\}/, SPRING_JWT_ACCESS_SECRET_KEY)
prodProperties = prodProperties.replaceAll(/\{jwt-base64-refresh-secret\}/, SPRING_JWT_REFRESH_SECRET_KEY)
prodProperties = prodProperties.replaceAll(/\{jwt-base64-register-secret\}/, SPRING_JWT_REGISTER_SECRET_KEY)
prodProperties = prodProperties.replaceAll(/\{redis-host\}/, SPRING_REDIS_HOST)
prodProperties = prodProperties.replaceAll(/\{redis-port\}/, SPRING_REDIS_PORT)
prodProperties = prodProperties.replaceAll(/\{oauth2-google-client-id\}/, SPRING_OAUTH2_GOOGLE_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-google-secret\}/, SPRING_OAUTH2_GOOGLE_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-github-client-id\}/, SPRING_OAUTH2_GITHUB_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-github-secret\}/, SPRING_OAUTH2_GITHUB_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-facebook-client-id\}/, SPRING_OAUTH2_FACEBOOK_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-facebook-secret\}/, SPRING_OAUTH2_FACEBOOK_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-naver-client-id\}/, SPRING_OAUTH2_NAVER_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-naver-secret\}/, SPRING_OAUTH2_NAVER_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-kakao-client-id\}/, SPRING_OAUTH2_KAKAO_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-kakao-secret\}/, SPRING_OAUTH2_KAKAO_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-discord-client-id\}/, SPRING_OAUTH2_DISCORD_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-discord-secret\}/, SPRING_OAUTH2_DISCORD_SECRET)
prodProperties = prodProperties.replaceAll(/\{oauth2-microsoft-client-id\}/, SPRING_OAUTH2_MICROSOFT_CLIENT_ID)
prodProperties = prodProperties.replaceAll(/\{oauth2-microsoft-secret\}/, SPRING_OAUTH2_MICROSOFT_SECRET)
writeFile file: SPRING_PROD_PROPERTIES_PATH, text: prodProperties
}
}
post {
success {
echo 'Successfully Pre-Processing'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Bulid Gradle') {
steps {
echo 'Bulid Gradle'
dir('.') {
sh 'chmod +x gradlew'
sh "SPRING_PROFILES_ACTIVE=${ACTIVE_PROFILE} ./gradlew clean build -x test --info"
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('Bulid Docker') {
steps {
echo 'Bulid Docker'
script {
dockerImage = docker.build("${IMAGE_NAME}")
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
stage('Push Docker') {
steps {
echo 'Push Docker'
script {
docker.withRegistry('', DOCKER_CREDENTIALS_ID) {
dockerImage.push("${IMAGE_TAG}")
dockerImage.push("latest")
}
}
}
post {
success {
sh 'docker rmi $(docker images -q -f dangling=true) || true'
}
failure {
error 'This pipeline stops here...'
}
}
}
stage('Docker Run') {
steps {
echo 'Pull Docker Image & Docker Image Run'
sshagent(credentials: ['ssh']) {
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker pull ${IMAGE_NAME}'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker ps -aq --filter name=${APP_NAME} | grep -q . && docker rm -f \$(docker ps -aq --filter name=${APP_NAME}) || true'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker run -d --restart always --name ${APP_NAME} -v /home/kimhwan/blog-datas:/var/attachments -v /home/kimhwan/blog-config:/config -v /etc/localtime:/etc/localtime:ro -v /usr/share/zoneinfo/Asia/Seoul:/etc/timezone:ro --net=host ${IMAGE_NAME}'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker images -qf dangling=true | xargs -I{} docker rmi {} || true'"
sh "ssh -o StrictHostKeyChecking=no [email protected] 'docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || true'"
}
}
post {
failure {
error 'This pipeline stops here...'
}
}
}
}
}