-
Notifications
You must be signed in to change notification settings - Fork 5
/
Jenkinsfile
335 lines (278 loc) · 9.23 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
pipeline {
agent any
stages {
stage('Setup') {
parallel {
stage('Setup') {
steps {
sh '''#!/bin/bash
export VERSION="0.0.0CI"
echo ---------------------------------------------------------------------------
echo Build Information
echo ---------------------------------------------------------------------------
echo "Working on : $JOB_NAME"
echo "Workspace : $WORKSPACE"
echo "Revision : $SVN_REVISION"
echo "Build : $BUILD_NUMBER"
echo "Deploy to : $DEPLOY_TO"
echo ---------------------------------------------------------------------------
echo "GIT_COMMIT : $GIT_COMMIT"
echo "GIT_BRANCH : $GIT_BRANCH"
echo ---------------------------------------------------------------------------
'''
sh '''echo "Clean Microservice Containers"
docker stop orderservicems || true
docker stop productservicems || true
docker stop customerservicems || true
docker stop ordermg || true
docker stop productmg || true
docker stop customermg || true
echo "Clean Test Containers"
sleep 1
runningCount=`docker ps -a -q --filter ancestor=jmeter:latest | wc -l`
if [ $runningCount -gt 0 ]; then
docker stop $(docker ps -a -q --filter ancestor=jmeter:latest --format="{{.ID}}") > /dev/nul
else
echo "No Jmeter Containers running"
fi
echo "Clean Build Assets"
rm -rf target
rm -rf jmeter
'''
}
}
stage('Get Version Number') {
steps {
echo 'Get Version Number'
load 'versionInput.groovy'
}
}
}
}
stage('Build') {
steps {
echo 'Build Project'
sh '''if [[ -z "$VERSION" ]]; then
VERSION=ci
fi
echo Version is: $VERSION
#CompileTest Microservice
echo "Compile Microservice"
docker run --rm --name service-maven -v "$PWD":/usr/share/mymaven -v "$HOME/.m2":/root/.m2 -v "$PWD"/target:/usr/share/mymaven/target -w /usr/share/mymaven maven:3.6-jdk-8 mvn compile'''
sh '''#Package the microservice
echo "Package the Microservice"
docker run --rm --name service-maven -v "$PWD":/usr/share/mymaven -v "$HOME/.m2":/root/.m2 -v "$PWD"/target:/usr/share/mymaven/target -w /usr/share/mymaven maven:3.6-jdk-8 mvn package'''
sh '''echo "Move Package for Docker Build"
cp $WORKSPACE/target/product-service-0.0.1.jar $WORKSPACE/service.jar'''
sh '''#Modify Alias depending on stage
if [ $GIT_BRANCH = "staging" ]; then
sudo sed -i \'s/\\[gateway\\]/apiworldref\\:5555/g\' $WORKSPACE/microgateway/config.yml
sudo sed -i \'s/\\[microservice\\]/localhost\\:8090/g\' $WORKSPACE/microgateway/config.yml
exit
fi
if [ $GIT_BRANCH = "master" ]; then
sudo sed -i \'s/\\[gateway\\]/apiworldref\\:5555/g\' $WORKSPACE/microgateway/config.yml
sudo sed -i \'s/\\[microservice\\]/localhost\\:8090/g\' $WORKSPACE/microgateway/config.yml
exit
fi
#Else assume its a development branch and set accordingly
sudo sed -i \'s/\\[gateway\\]/apiworldbuild\\:5555/g\' $WORKSPACE/microgateway/config.yml
sudo sed -i \'s/\\[microservice\\]/apiworldbuild\\:8090/g\' $WORKSPACE/microgateway/config.yml
'''
sh '''#Build MicroGateway
cd /opt/softwareag/microgateway
./microgateway.sh createDockerFile --docker_dir . -p 9090 -a $WORKSPACE/microgateway/Product.zip -dof ./Dockerfile -c $WORKSPACE/microgateway/config.yml'''
}
}
stage('Containerize') {
steps {
sh '''#Containerize Microservice
docker build -t productservice:$VERSION --build-arg PORT=8090 --build-arg JAR_FILE=service.jar .
#docker tag productservice:$VERSION productservice:$VERSION'''
sh '''#Containerize Microgateway
cd /opt/softwareag/microgateway
docker build -t productmg:$VERSION .
'''
}
}
stage('Deployment') {
parallel {
stage('Start MicroSvc') {
steps {
sh '''#Run the container read for testing
docker run --rm --name productservicems -d -p 8090:8090 productservice:$VERSION
'''
}
}
stage('Start MicroGW') {
steps {
sh '''#Run MicroGateway Container
docker run --rm --name productmg -d -p 9090:9090 --net=host productmg:$VERSION
'''
}
}
}
}
stage('Test Operational') {
parallel {
stage('Test MicroSvc Operational') {
steps {
sh '''#Are services operational
timeout 60 bash -c \'while [[ "$(curl -s -o /dev/null -w \'\'%{http_code}\'\' localhost:8090/product)" != "200" ]]; do sleep 5; done\' || false
'''
}
}
stage('Test MicroGW Operational') {
steps {
sh '''#Is MicroGW Operational
timeout 60 bash -c \'while [[ "$(curl -s -o /dev/null -w \'\'%{http_code}\'\' localhost:9090/gateway/Product/1.0/product)" != "200" ]]; do sleep 5; done\' || false'''
}
}
}
}
stage('Testing') {
parallel {
stage('Load Test') {
steps {
sh '''rm -rf jmeter
mkdir jmeter
mkdir jmeter/output
cp src/main/loadtest.jmx jmeter/
docker run --rm --name jmeter --volume $WORKSPACE/jmeter/:/mnt/jmeter vmarrazzo/jmeter:latest -n -t /mnt/jmeter/loadtest.jmx -l /mnt/jmeter/result.jtl -j /mnt/jmeter/result.log -e -o /mnt/jmeter/output'''
perfReport(sourceDataFiles: 'jmeter/result.jtl', compareBuildPrevious: true, errorUnstableResponseTimeThreshold: '5000')
}
}
stage('Unit Test') {
steps {
sh '''#Unit Test Microservice
echo "Unit Test Microservice"
docker run --rm --name service-maven -v "$PWD":/usr/share/mymaven -v "$HOME/.m2":/root/.m2 -v "$PWD"/target:/usr/share/mymaven/target -w /usr/share/mymaven maven:3.6-jdk-8 mvn test'''
}
}
stage('Interface Test') {
steps {
echo 'Test Microservice'
sh '''#Test Microservice
curl http://apiworldbuild:8090/product/1
test=`curl -s http://apiworldbuild:8090/product/1 | grep foo | wc -l`
if [ $test -gt 0 ]; then
echo "Test Passed"
else
echo "Error in interface test for MicroService"
exit 1
fi'''
echo 'Test Gateway'
sh '''#Test Gateway
test=`curl -s http://apiworldbuild:9090/gateway/Product/1.0/product/1 | grep foo | wc -l`
if [ $test -gt 0 ]; then
echo "Test Passed"
else
echo "Error in interface test for MicroGateway"
exit 1
fi'''
}
}
}
}
stage('Register Images') {
when {
anyOf {
branch 'staging'
branch 'master'
}
}
steps {
sh '''#push image to registry
#First tag
docker tag productservice:$VERSION apiworldref:5000/productservice:$VERSION
docker tag productmg:$VERSION apiworldref:5000/productmg:$VERSION
#second push
docker push apiworldref:5000/productservice:$VERSION
docker push apiworldref:5000/productmg:$VERSION'''
}
}
stage('Release To Test') {
agent {
node {
label 'RefEnv-root'
}
}
when {
anyOf {
branch 'staging'
}
}
steps {
echo 'Release to test'
sh '''#Release into staging
deployActive=`kubectl get deployments.apps | grep product-service-deployment | wc -l`
if [ $deployActive -gt 0 ]; then
echo "Perform Rolling Update"
#Do a rolling update
kubectl set image deployment.v1.apps/product-service-deployment product-service=apiworldref:5000/productservice:$VERSION
kubectl set image deployment.v1.apps/product-service-deployment product-service-sidecar=apiworldref:5000/productmg:$VERSION
#Now wait for deploy
kubectl rollout status deployment.v1.apps/product-service-deployment
else
echo "NEW Deployment"
#Inject the version
sed -i \'s/latest/$VERSION/g\' k8s-deployment.yml
#Register the K8S deployment
kubectl apply -f k8s-deployment.yml
kubectl apply -f k8s-services.yml
fi
'''
}
}
stage('Release To Production') {
agent {
node {
label 'RefEnv-root'
}
}
when {
branch 'master'
}
steps {
echo 'Release to Prod'
sh '''#Release into production
deployActive=`kubectl get deployments.apps | grep product-service-deployment | wc -l`
if [ $deployActive -gt 0 ]; then
echo "Perform Rolling Update"
#Do a rolling update
kubectl set image deployment.v1.apps/product-service-deployment product-service=apiworldref:5000/productservice:$VERSION
kubectl set image deployment.v1.apps/product-service-deployment product-service-sidecar=apiworldref:5000/productmg:$VERSION
#Now wait for deploy
kubectl rollout status deployment.v1.apps/product-service-deployment
else
echo "NEW Deployment"
#Inject the version
sed -i \'s/latest/$VERSION/g\' k8s-deployment.yml
#Register the K8S deployment
kubectl apply -f k8s-deployment.yml
kubectl apply -f k8s-services.yml
fi
'''
}
}
stage('Clean') {
steps {
sh '''#Tidy up after build
#Stop containers
#docker stop productmg
#docker stop productservicems
#Prune
docker image prune -f
docker volume prune -f
'''
}
}
}
post {
always {
junit 'target/surefire-reports/**/*.xml'
perfReport(sourceDataFiles: 'jmeter/result.jtl', compareBuildPrevious: true, errorUnstableResponseTimeThreshold: '5000')
archiveArtifacts(artifacts: 'jmeter/result.*', fingerprint: true)
}
}
}