-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.jenkinsfile
234 lines (233 loc) · 7.49 KB
/
build.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
def springboot = false
def flask = false
def android = false
def react = false
pipeline {
agent any
post {
failure {
updateGitlabCommitStatus name: 'result', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'result', state: 'success'
}
aborted {
updateGitlabCommitStatus name: 'result', state: 'canceled'
}
}
stages {
stage('init') {
steps {
echo 'init pipeline, check changes'
// git 'fetch'
script {
def buildCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
echo "manually started by ${buildCause}"
if(!buildCause.isEmpty()) {
echo "triggerd by user"
springboot = true
flask = true
android = true
react = true
}
else {
def changedFiles = sh(returnStdout: true, script: 'git diff --name-only --diff-filter=ACMRT HEAD^ HEAD').trim().split('\n')
def changedDirs = new HashSet()
echo "files changed : ${changedFiles}"
for(def file : changedFiles) {
echo "new file : ${file}"
if(file.contains('/')) {
// def dir = file.substring(0, file.lastIndexOf('/'))
def dir = file.split('/')[0]
changedDirs.add(dir)
echo "modified : ${dir}"
}
else {
changedDirs.add(file)
}
}
springboot = changedDirs.contains('back-end')
flask = changedDirs.contains('domain')
android = changedDirs.contains('front-end')
react = changedDirs.contains('intro-page')
}
}
echo "spring : ${springboot}, flask : ${flask}, android : ${android}, react : ${react}"
}
}
stage('cofing') {
steps {
echo 'copy configuration files'
sh 'pwd'
sh 'cp /var/jenkins_home/workspace/Groot_config/application.properties ./back-end/backend/src/main/resources'
sh 'cp /var/jenkins_home/workspace/Groot_config/application-keys.properties ./back-end/backend/src/main/resources'
sh 'cp /var/jenkins_home/workspace/Groot_config/config.py ./domain'
sh 'cp /var/jenkins_home/workspace/Groot_config/groot-e4de7-firebase-adminsdk-lew5e-83aab40438.json ./back-end/backend/src/main/resources/firebase'
}
}
stage('build-spring') {
when {
expression { springboot }
}
steps {
updateGitlabCommitStatus name: 'build-spring', state: 'pending'
echo 'move directory'
dir('./back-end/backend/') {
sh 'pwd'
sh 'ls -l'
echo 'start build'
sh 'chmod +x ./gradlew'
sh './gradlew clean build'
}
updateGitlabCommitStatus name: 'build-spring', state: 'success'
}
}
stage('build-flask') {
when {
expression { flask }
}
steps {
updateGitlabCommitStatus name: 'build-flask', state: 'pending'
echo 'move directory'
dir('./domain/') {
sh 'pwd'
sh 'ls -l'
echo 'start build'
sh 'docker build -t flask .'
}
updateGitlabCommitStatus name: 'build-flask', state: 'success'
}
}
stage('build-react') {
when {
expression { react }
}
steps {
updateGitlabCommitStatus name: 'build-react', state: 'pending'
echo 'move directory'
dir('./intro-page/our-groot/') {
echo 'start build'
sh 'docker build -t react .'
}
updateGitlabCommitStatus name: 'build-react', state: 'success'
}
}
stage('unload') {
parallel {
stage('down-spring') {
environment {
// CONTAINER_ID = sh 'docker ps -a | grep spring | awk \'{print $1\'}'
SPRING_CONTAINER_ID = sh(returnStdout: true, script: 'docker ps -a | grep spring | awk \'{print $1\'}').trim()
}
when {
expression{ springboot }
}
steps {
script {
if(env.SPRING_CONTAINER_ID != null) {
updateGitlabCommitStatus name: 'unload', state: 'pending'
echo 'unload docker container'
echo "${env.SPRING_CONTAINER_ID}"
sh "docker stop ${env.SPRING_CONTAINER_ID}"
echo 'remove docker container'
sh "docker rm ${env.SPRING_CONTAINER_ID}"
updateGitlabCommitStatus name: 'unload', state: 'success'
}
}
}
}
stage('down-flask') {
environment {
FLASK_CONTAINER_ID = sh(returnStdout: true, script: 'docker ps -a | grep flask | awk \'{print $1\'}').trim()
}
when {
expression { flask }
}
steps {
script {
if(env.FLASK_CONTAINER_ID != null) {
echo 'unload flask container'
echo "${env.FLASK_CONTAINER_ID}"
sh "docker stop ${env.FLASK_CONTAINER_ID}"
echo 'remove docker container'
sh "docker rm ${env.FLASK_CONTAINER_ID}"
}
}
}
}
stage('down-react') {
environment {
REACT_CONTAINER_ID = sh(returnStdout: true, script: 'docker ps -a | grep react | awk \'{print $1\'}').trim()
}
when {
expression { react }
}
steps {
script {
if(env.REACT_CONTAINER_ID != null) {
echo 'unload react container'
echo "${env.REACT_CONTAINER_ID}"
sh "docker stop ${env.REACT_CONTAINER_ID}"
echo 'remove docker container'
sh "docker rm ${env.REACT_CONTAINER_ID}"
}
}
}
}
}
}
stage('deploy') {
parallel {
stage('deploy-spring') {
when {
expression { springboot }
}
steps {
updateGitlabCommitStatus name: 'deploy-spring', state: 'pending'
sh 'pwd'
dir('./back-end/backend/') {
echo 'build docker image'
sh 'docker build --no-cache -t springboot .'
echo 'run docker image'
sh 'docker run --name spring -d -p 8080:8080 springboot'
}
updateGitlabCommitStatus name: 'deploy-spring', state: 'success'
}
}
stage('deploy-flask') {
when {
expression { flask }
}
steps {
updateGitlabCommitStatus name: 'deploy-flask', state: 'pending'
dir('./domain/') {
echo 'run docker container'
sh 'docker run -d -v /tmp/flask:/tmp --name flask flask'
}
updateGitlabCommitStatus name: 'deploy-flask', state: 'success'
}
}
stage('deploy-react') {
when {
expression { react }
}
steps {
updateGitlabCommitStatus name: 'deploy-react', state: 'pending'
dir('./intro-page/our-groot/') {
echo 'run docker container'
sh 'docker run --name react -d -p 3000:3000 react'
}
updateGitlabCommitStatus name: 'deploy-react', state: 'success'
}
}
}
}
// will be integrated with python server clean
stage('clean') {
steps {
echo 'clean unused image'
sh 'docker image prune --force'
}
}
}
}