forked from devopsbc01/DevOps-Demo-WebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
336 lines (313 loc) · 15 KB
/
Jenkinsfile
File metadata and controls
336 lines (313 loc) · 15 KB
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
336
def server
def buildInfo
def rtMaven
pipeline {
agent any
stages {
stage('Initalize') {
parallel {
stage('Static Code Analysis') {
steps {
slackSend channel: 'notify', message: "Static Code Analysis started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
withSonarQubeEnv('sonarqube-server') {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
mvn "-Dsonar.test.exclusions=**/test/java/servlet/createpage_junit.java " -Dsonar.login=sonar -Dsonar.password=${SONAR_AUTH} -Dsonar.tests=. -Dsonar.inclusions=**/test/java/servlet/createpage_junit.java -Dsonar.sources=. sonar:sonar -Dsonar.host.url=${SONAR_HOST_URL}
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Static Code Analysis succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Static Code Analysis failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Build the project') {
steps {
slackSend channel: 'notify', message: "Build the project started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
mvn -B -f pom.xml compile
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Build the project succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Build the project failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Initalize Docker Compose on Test & Prod Servers') {
steps {
slackSend channel: 'notify', message: "Initialize Test & Prod Server started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
testserver=`grep test-server /etc/ansible/hosts | awk '{print $2}' | cut -d '=' -f2`
prodserver=`grep prod-server /etc/ansible/hosts | awk '{print $2}' | cut -d '=' -f2`
for server in $testserver $prodserver
do
sudo scp -o StrictHostKeyChecking=no docker-compose.yml root@${server}:/root/docker-compose.yml
sudo ssh -o StrictHostKeyChecking=no root@${server} '
if [[ `docker ps -q | wc -l` -gt 0 ]]
then
docker-compose down
fi
rm -fr /opt/tomcat/webapps/*
docker-compose up -d
'
done
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Initialize Test & Prod Server succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Initialize Test & Prod Server failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
}
post {
success {
script {
sh '''
### Code to update the Story state
story_nbr=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2 | cut -d '-' -f1`
my_merge_branch=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2`
curl -v -X PUT -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{"current_state":"finished"}' "${MY_PIVOTAL_STORIES}/${story_nbr}"
### Code to add the comments to the pivotal story
curl -v -X POST -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{ "text":"Squad-5 Pipeline started due to new code merge to master from '${story_nbr}' in the github.\\n **BRANCH**: '${my_merge_branch}' \\n **REPO**: '${MY_REPO}'\\n \\n \\n Story status is updated to **FINISHED** " }' "${MY_PIVOTAL_STORIES}/${story_nbr}/comments"
'''
}
}
}
}
stage('Configure Test & Prod Server') {
steps {
slackSend channel: 'notify', message: "Configure Test & Prod Server started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
testserver=`grep test-server /etc/ansible/hosts | awk '{print $2}' | cut -d '=' -f2`
prodserver=`grep prod-server /etc/ansible/hosts | awk '{print $2}' | cut -d '=' -f2`
echo "Testserver: $testserver and Prodcution: $prodserver"
sed -i "s/squadtestserver/$testserver/g" $(find . -type f)
sed -i "s/squadprodserver/$prodserver/g" $(find . -type f)
grep URL functionaltest/src/test/java/functionaltest/ftat.java
grep URL Acceptancetest/src/test/java/acceptancetest/acat.java
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Configure Test & Prod Server succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Configure Test & Prod Server failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Package the project') {
steps {
slackSend channel: 'notify', message: "Package the project started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
mvn package -Dmaven.test.skip=true
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Package the project succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Package the project failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Store Artifacts') {
steps {
slackSend channel: 'notify', message: "Upload artifacts started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
server = Artifactory.server 'artifactory'
sh """ mvn package -Dmaven.test.skip=true """
def uploadSpec = """{
"files": [
{
"pattern": "target/*.war",
"target": "squad5-libs-release-local"
},
{
"pattern": "target/*.war",
"target": "squad5-libs-snapshot-local"
}
]
}"""
server.upload spec: uploadSpec
}
}
post {
success {
slackSend channel: 'notify', message: "Upload artifacts succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Upload artifacts failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Deploy on Test Server') {
steps {
slackSend channel: 'notify', message: "Deployment of War on Test Server started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
sudo ansible-playbook -e 'deployservers="test-server" lcp="QA"' dwnldArtifact.yml
sleep 20s
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Deployment of War on Test Server succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Deployment of War on Test Server failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('UI Testing on Test Server') {
steps {
slackSend channel: 'notify', message: "UI Testing started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
mvn -B -f functionaltest/pom.xml test
'''
}
// publish html
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '\\functionaltest\\target\\surefire-reports', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: ''])
}
post {
always {
jiraSendDeploymentInfo site: 'devopsbctcs03.atlassian.net', environmentId: 'test-${env.BUILD_NUMBER}', environmentName: 'testserver', environmentType: 'testing', issueKeys: ['DP-2']
}
success {
slackSend channel: 'notify', message: "UI Testing succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "UI Testing failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Performance test'){
steps {
slackSend channel: 'notify', message: "Performance Testing started for build : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
// blazeMeterTest credentialsId: 'Blazemeter', testId: '9137429.taurus', workspaceId: '775624'
//blazeMeterTest credentialsId: 'bcb98eda-1130-4c9e-97e3-2262ffd3a6b7', testId: '9204220.taurus', workspaceId: '799387'
}
post {
success {
slackSend channel: 'notify', message: "Performance Testing succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
### Code to update the Story state
story_nbr=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2 | cut -d '-' -f1`
my_merge_branch=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2`
curl -v -X PUT -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{"current_state":"delivered"}' "${MY_PIVOTAL_STORIES}/${story_nbr}"
### Code to add the comments to the pivotal story
curl -v -X POST -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{ "text":"Squad-5 Pipeline deployed the new code to AWS Test-Server from artifactory using docker compose and ran the UI and Performance testing. \\n **Story ID**: '${story_nbr}' \\n **BRANCH**: '${my_merge_branch}' \\n **REPO**: '${MY_REPO}'\\n \\n \\n Story status is updated to **DELIVERED** " }' "${MY_PIVOTAL_STORIES}/${story_nbr}/comments"
'''
}
}
failure {
slackSend channel: 'notify', message: "Performance Testing failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Deploy on Prod Server') {
steps {
slackSend channel: 'notify', message: "Deployment of War on Prod Server started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
sudo ansible-playbook -e 'deployservers="prod-server" lcp="Prod"' dwnldArtifact.yml
sleep 20s
'''
}
}
post {
success {
slackSend channel: 'notify', message: "Deployment of War on Prod Server succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Deployment of War on Prod Server failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
stage('Sanity Tests') {
steps {
slackSend channel: 'notify', message: "Sanity Testing on Prod Server started for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
script {
sh '''
mvn -B -f Acceptancetest/pom.xml test
'''
}
// publish html
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '\\Acceptancetest\\target\\surefire-reports', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: ''])
}
post {
always {
jiraSendDeploymentInfo site: 'devopsbctcs03.atlassian.net', environmentId: 'prod-${env.BUILD_NUMBER}', environmentName: 'prodserver', environmentType: 'production', issueKeys: ['DP-2']
}
success {
slackSend channel: 'notify', message: "Sanity Testing on Prod Server succesfully completed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
failure {
slackSend channel: 'notify', message: "Sanity Testing on Prod Server failed for : ${env.JOB_NAME} ${env.BUILD_NUMBER}"
}
}
}
}
post {
success {
script {
sh '''
### Code to update the Story CICD Pipeline status
export story_nbr=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2 | cut -d '-' -f1`
export NEW_LATEST_GIT_SHA=$(git rev-parse HEAD)
my_merge_branch=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2`
curl -v -X POST -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Content-Type: application/json" -d '{"status":"passed", "url":"'$BUILD_URL'", "uuid":"9c1a65985558b645d869c2adf0f162fc", "story_ids":['${story_nbr}'], "latest_git_sha":"'$NEW_LATEST_GIT_SHA'", "version":1}' "${MY_PIVOTAL_CICD}"
### Code to add the comments to the pivotal story
curl -v -X POST -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{ "text":"Squad-5 Pipelinedeployed the new code to AWS Prod-Server from artifactory using docker compose and completed the Sanity testing. Code is live to end-user \\n **Story ID**: '{story_nbr}' \\n **BRANCH**: '${my_merge_branch}' \\n **REPO**: '${MY_REPO}'\\n \\n \\n Story status is updated to **ACCEPTED** " }' "${MY_PIVOTAL_STORIES}/{story_nbr}/comments"
curl -v -X PUT -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Accept: application/json" -H "Content-Type: application/json" --data '{"current_state":"accepted"}' "${MY_PIVOTAL_STORIES}/${story_nbr}"
'''
}
}
failure {
script {
sh '''
### Code to update the Story CICD Pipeline status
export story_nbr=`git show ':/^Merge' --oneline| xargs| awk '{print $7}' | cut -d '/' -f2 | cut -d '-' -f1`
export NEW_LATEST_GIT_SHA=$(git rev-parse HEAD)
curl -v -X POST -H "X-TrackerToken: $TRACKER_API_TOKEN" -H "Content-Type: application/json" -d '{"status":"failed", "url":"'$BUILD_URL'", "uuid":"9c1a65985558b645d869c2adf0f162fc", "story_ids":['${story_nbr}'], "latest_git_sha":"'$NEW_LATEST_GIT_SHA'", "version":1}' "${MY_PIVOTAL_CICD}"
'''
}
}
}
tools {
maven 'Maven3.6.3'
jdk 'JDK'
}
environment {
SONAR_AUTH = credentials('sonar-login')
TRACKER_API_TOKEN = credentials('tracker-api-token')
MY_REPO="https://github.com/devopsbcsquad5/DevOps-Demo-WebApp"
MY_PIVOTAL_CICD="https://www.pivotaltracker.com/services/v5/projects/2490801/cicd"
MY_PIVOTAL_STORIES="https://www.pivotaltracker.com/services/v5/projects/2490801/stories"
}
}