forked from javahometech/dockeransiblejenkins
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUAT_ApprovalMail_26July22
More file actions
374 lines (337 loc) · 13.1 KB
/
UAT_ApprovalMail_26July22
File metadata and controls
374 lines (337 loc) · 13.1 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/** Function to send Email*/
def sendEmail(emailSubject, emailBody, recipients, hasAttachment) {
print '...........................Sending Email..................................'
if(hasAttachment){
emailext (subject: emailSubject, attachmentsPattern: '**/*.txt', mimeType: 'text/html', body: emailBody, to: recipients);
} else {
emailext (subject: emailSubject, mimeType: 'text/html', body: emailBody, to: recipients);
}
}
def Docker_TAG
pipeline{
agent any
tools {
maven 'maven 3.8.5'
}
environment {
stage_status = "Failure"
test_status = "Failure"
uat_status = "Failure"
}
stages {
stage("checkout") {
steps{
script {
git url: 'https://github.com/nrBaskar/dockeransiblejenkins.git', branch: 'master'
Docker_TAG = getversion()
}
}
}
stage("last-changes") {
steps{
script{
env.GIT_COMMIT_AUTHOR = sh(returnStdout: true, script: 'git log -3 --format="%ae"').trim()
env.GIT_COMMIT_ID = sh(returnStdout: true, script: 'git log -3 --format="%h"').trim()
env.GIT_COMMIT_DATE = sh(returnStdout: true, script: 'git log -3 --graph --pretty=format:"%cd"').trim()
def diff = sh(returnStdout: true, script: 'git show -n 3')
sh(returnStdout: true, script: 'cd /var/lib/jenkins/workspace/')
env.SourcePATH = sh(returnStdout: true, script: 'pwd')
diff += "SOURCE PATH: "+env.SourcePATH
writeFile file: 'build.txt', text: diff
}
}
}
stage('Maven Build'){
steps{
sh "mvn clean package"
}
}
stage('Docker Build'){
steps{
dockerstatus()
sh "docker build . -t nrbaskar/hariapp:${Docker_TAG}"
}
}
stage('Dockerhub Push'){
steps{
withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {
sh "docker login -u nrbaskar -p ${dockerHubPwd}"
}
sh "docker push nrbaskar/hariapp:${Docker_TAG}"
}
}
stage('Staging server'){
steps{
script{
ansiblePlaybook(
becomeUser: null,
colorized: false,
installation: 'ansible',
credentialsId: 'dev-server',
disableHostKeyChecking: true,
forks: 50,
hostKeyChecking: false,
inventory: 'dev.inv',
playbook: 'deploy-docker.yml',
sudoUser: null,
extras: "-e DOCKER_TAG=${Docker_TAG}"
) //ansiblePlaybook
env.staging_logs = getOutputLogs()
}
}
}
stage('Stage Deploy'){
steps{
script{
//Stage Server Logs
// Match (ok=) word in logs using Regex pattern
def stage_matches = env.staging_logs =~ /(?=[ ]*:[ ]*ok=([1-9]|[1-9][0-9]))/
def stage_res = stage_matches[0]
if(stage_res[1] > 0){
stage_status = "Success"
}
}
}
}
stage('Testing server'){
steps{
script{
ansiblePlaybook(
becomeUser: null,
colorized: false,
installation: 'ansible',
credentialsId: 'testing-server',
disableHostKeyChecking: true,
forks: 50,
hostKeyChecking: false,
inventory: 'test.inv',
playbook: 'deploy_test.yml',
sudoUser: null,
extras: "-e DOCKER_TAG=${Docker_TAG}"
) //ansiblePlaybook
env.testing_log = getOutputLogs()
}
}
}
stage('Test Deploy'){
steps{
script{
//Test Server Logs
// Match (ok=) word in logs using Regex pattern
def test_matches = env.testing_log =~ /(?=[ ]*:[ ]*ok=([1-9]|[1-9][0-9]))/
def test_res = test_matches[0]
if(test_res[1] > 0){
test_status = "Success"
}
}
}
}
stage('UAT Approval'){
steps{
script{
def emailSubject = "Jenkins Notification: Build Approval for UAT Deployment Build ID #${BUILD_NUMBER}";
def emailBody = """
<p>Hi UAT Team,</p>
<strong>"UAT Deployment for Build ID #${BUILD_NUMBER} is waiting for your Approval"</strong>
<p>A build has been initiated in the UAT. We will let you know once the deployment is successful.</p>
<p>To browse Jenkins URL: http://18.212.49.103:8080</p>
<p>Staging Deployment- <b>${stage_status}</b></p>
<p>Staging URL - http://54.227.16.102:8083/webapp/index.jsp </b></p>
<p>${env.staging_logs}</p><br>
<p>Testing Deployment- <b>${test_status}</b></p>
<p>Testing URL - http://34.229.0.210:8085/webapp/index.jsp </b></p>
<p>${env.testing_log}</p><br>
<p>Source Path: <b>${env.SourcePATH}</b></p>
<p>Author: ${env.GIT_COMMIT_AUTHOR}</p>
<p>Date: ${env.GIT_COMMIT_DATE}</p>
<p>With Regards,</p>
<p>Sify Jenkins Admin</p>"""
def recipients = APPROVER
sendEmail(emailSubject, emailBody, recipients, true);
input(message: "Do you want to approve this build?")
}
}
}
stage('Uat server'){
steps{
script{
ansiblePlaybook(
becomeUser: null,
colorized: false,
installation: 'ansible',
credentialsId: 'uat1-server',
disableHostKeyChecking: true,
forks: 50,
hostKeyChecking: false,
inventory: 'uat.inv',
playbook: 'deploy_uat.yml',
sudoUser: null,
extras: "-e DOCKER_TAG=${Docker_TAG}"
) //ansiblePlaybook
env.uat_log = getOutputLogs()
}
}
}
stage('Uat Deploy'){
steps{
script{
//uat Server Logs
// Match (ok=) word in logs using Regex pattern
def uat_matches = env.uat_log =~ /(?=[ ]*:[ ]*ok=([1-9]|[1-9][0-9]))/
def uat_res = uat_matches[0]
if(uat_res[1] > 0){
uat_status = "Success"
}
}
}
}
}
post {
success{
script{
DeployNotification()
}
}
failure {
script {
if(stage_status != "Success"){
env.staging_logs = getErrorLogs()
}
if(test_status != "Success"){
env.testing_log = getErrorLogs()
}
if(uat_status != "Success"){
env.uat_log = getErrorLogs()
}
DeployNotification()
}
} //failure
} //post
}
def getversion(){
def commithash = sh(returnStdout: true, script: 'git rev-parse --short HEAD')
echo commithash
return commithash
}
def dockerstatus() {
sh "sudo service docker start"
sh "sudo chmod 777 /var/run/docker.sock"
}
def DeployNotification(){
if(stage_status != 'Success'){
def emailSubject = "Jenkins Notification: Build Failure for Staging Server Build ID #${BUILD_NUMBER}";
def emailBody = """
<p>Hi Staging Dev Team,</p>
<p>"Staging Deployment for Build ID #${BUILD_NUMBER} is successful"
<p>The Staging development environment has been deployed successfully, with latest version.</p>
<p>To browse use: http://54.227.16.102:8083/webapp/index.jsp</p>
<p>Please find the last commit details below:</p>
<p>See attached diff of <b>${env.JOB_NAME} #${BUILD_NUMBER}</b>.</p>
<p>Commit Id: <b>${env.GIT_COMMIT_ID}</b></p>
<p>Source Path: <b>${env.SourcePATH}</b></p>
<p>Author: ${env.GIT_COMMIT_AUTHOR}</p>
<p>Date: ${env.GIT_COMMIT_DATE}</p>
<p>Staging Deployment- <b>${stage_status}</b></p>
<p>${env.staging_logs}</p><br>
<p>With Regards,</p>
<p>Sify Jenkins Admin</p>"""
def recipients = TESTERS
sendEmail(emailSubject, emailBody, recipients, true);
}else if(test_status != 'Success'){
def emailSubject = "Jenkins Notification: Build Failure for Testing Server Build ID #${BUILD_NUMBER}";
def emailBody = """
<p>Hi Devops Team,</p>
<p>Jenkins File Deployment Process for mail_notification is Failure :
<p>Please find the last commit details below:</p>
<p>See attached diff of <b>${env.JOB_NAME} #${BUILD_NUMBER}</b>.</p>
<p>Commit Id: <b>${env.GIT_COMMIT_ID}</b></p>
<p>Source Path: <b>${env.SourcePATH}</b></p>
<p>Author: ${env.GIT_COMMIT_AUTHOR}</p>
<p>Date: ${env.GIT_COMMIT_DATE}</p>
<p>Staging URL - http://54.227.16.102:8083/webapp/index.jsp </b></p>
<p>${env.staging_logs}</p><br>
<p>Testing Deployment- <b>${test_status}</b></p>
<p>${env.testing_log}</p><br>
<p>With Regards,</p>
<p>Sify Jenkins Admin</p>"""
def recipients = TESTERS
sendEmail(emailSubject, emailBody, recipients, true);
}else if(uat_status != 'Success'){
def emailSubject = "Jenkins Notification: Build Failure for UAT Server Build ID #${BUILD_NUMBER}";
def emailBody = """
<p>Hi Devops Team,</p>
<p>Jenkins File Deployment Process for mail_notification is Failure :
<p>Please find the last commit details below:</p>
<p>See attached diff of <b>${env.JOB_NAME} #${BUILD_NUMBER}</b>.</p>
<p>Commit Id: <b>${env.GIT_COMMIT_ID}</b></p>
<p>Source Path: <b>${env.SourcePATH}</b></p>
<p>Author: ${env.GIT_COMMIT_AUTHOR}</p>
<p>Date: ${env.GIT_COMMIT_DATE}</p>
<p>Staging Deployment- <b>${stage_status}</b></p>
<p>Staging URL - http://54.227.16.102:8083/webapp/index.jsp </b></p>
<p>${env.staging_logs}</p><br>
<p>Testing Deployment- <b>${test_status}</b></p>
<p>Testing URL - http://34.229.0.210:8085/webapp/index.jsp </b></p>
<p>${env.testing_log}</p><br>
<p>UAT Deployment- <b>${uat_status}</b></p>
<p>${env.uat_log}</p><br>
<p>With Regards,</p>
<p>Sify Jenkins Admin</p>"""
def recipients = TESTERS
sendEmail(emailSubject, emailBody, recipients, true);
}else{
def emailSubject = "Jenkins Notification: Build Success for Staging,Testing and UAT Server Build ID #${BUILD_NUMBER}";
def emailBody = """
<p>Hi Devops Team,</p>
<p>Jenkins File Deployment Process for mail_notification is Success :
<p>Please find the last commit details below:</p>
<p>See attached diff of <b>${env.JOB_NAME} #${BUILD_NUMBER}</b>.</p>
<p>Commit Id: <b>${env.GIT_COMMIT_ID}</b></p>
<p>Source Path: <b>${env.SourcePATH}</b></p>
<p>Author: ${env.GIT_COMMIT_AUTHOR}</p>
<p>Date: ${env.GIT_COMMIT_DATE}</p>
<p>Staging Deployment- <b>${stage_status}</b></p>
<p>Staging URL - http://54.227.16.102:8083/webapp/index.jsp </b></p>
<p>${env.staging_logs}</p><br>
<p>Testing Deployment- <b>${test_status}</b></p>
<p>Testing URL - http://34.229.0.210:8085/webapp/index.jsp </b></p>
<p>${env.testing_log}</p><br>
<p>UAT Deployment- <b>${uat_status}</b></p>
<p>UAT URL - http://54.226.10.145:8087/webapp/index.jsp </b></p>
<p>${env.uat_log}</p><br>
<p>With Regards,</p>
<p>Sify Jenkins Admin</p>"""
def recipients = TESTERS
sendEmail(emailSubject, emailBody, recipients, true);
}
}
def getErrorLogs(){
// Get the last 100 lines of the log
def log = currentBuild.rawBuild.getLog(50)
// Get (PLAY RECAP) String in logs using REGEX pattern
def matches = log =~ /([0-9a-zA-Z\.\-]+)(?=[ ]*:[ ](.*)PLAY RECAP(.*))/
def errolog = null
if (matches) {
for (int i = 0; i < matches.size(); i++) {
if (errolog?.trim()) {
errolog = errolog + " " + matches[i]
} else {
errolog = matches[i][0]
}
} // for
}
return errolog
}
def getOutputLogs() {
// Get the last 100 lines of the log
def log = currentBuild.rawBuild.getLog(100)
//Match Output logs using Regex pattern
def matches = log =~ /([0-9a-zA-Z\.\-]+)(?=[ ]*:[ ](.*)PLAY RECAP(.*))/
def response = null
if (matches) {
for (int i = 0; i < matches.size(); i++) {
response = matches[i]
}
}
return response
}