-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmailNotificationJenkinsFile
61 lines (50 loc) · 2.77 KB
/
EmailNotificationJenkinsFile
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
node {
// If the build is triggered by any upstream job
if (currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)) {
sendEmail()
}
}
@NonCPS
def sendEmail() {
// Name of the Jenkins jobs for email notification will be sent if status is FAILURE
def String[] jobsForStatusNotification = ["Kernel", "Authentication", "Pre-Registration", "Registration", "Registration-Processor", "modify-pipeline-poc", "master-branch-build-all-modules"]
def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
// Description for upstream job by which this pipeline has been triggered
def description = upstream?.shortDescription
// projectName for upstream job by which this pipeline has been triggered
def projectName = description.substring(description.indexOf("\"") + 1, description.indexOf("\"", description.indexOf("\"") + 1));
// projectStatus for upstream job by which this pipeline has been triggered
def projectStatus = Jenkins.instance.getItem(projectName).lastCompletedBuild.getResult().toString()
// projectBuildNumber for upstream job by which this pipeline has been triggered
def projectBuildNumber = Jenkins.instance.getItem(projectName).lastCompletedBuild.getNumber().toString()
// projectBuildURL for upstream job by which this pipeline has been triggered
def projectBuildURL = Jenkins.instance.getItem(projectName).lastCompletedBuild.absoluteUrl.toString()
echo "projectName: " + projectName
echo "projectStatus: " + projectStatus
echo "projectBuildNumber: "+projectBuildNumber
echo "projectBuildURL: "+projectBuildURL
def recipients
if (jobsForStatusNotification.contains(projectName) && projectStatus.equals("FAILURE")) {
if (projectName.equals("Kernel")) {
recipients = "$env.KERNEL_RECIPIENT_LIST"
} else if (projectName.equals("Pre-Registration")) {
recipients = "$env.PRE_REGISTRATION_RECIPIENT_LIST"
} else if (projectName.equals("Authentication")) {
recipients = "$env.IDA_RECIPIENT_LIST"
} else if (projectName.equals("Registration")) {
recipients = "$env.REGISTRATION_RECIPIENT_LIST"
} else if (projectName.equals("Registration-Processor")) {
recipients = "$env.REGISTRATION_PROCESSOR_RECIPIENT_LIST"
} else if (projectName.equals("master-branch-build-all-modules")) {
recipients = "$env.MASTER_BRANCH_BUILD_ALL_MODULES_RECIPIENT_LIST"
} else if (projectName.equals("modify-pipeline-poc")) {
recipients = "$env.TEST_RECIPIENT_LIST"
}
emailext (
subject: "MOSIP Jenkins Job '${projectName} with build no ${projectBuildNumber} failed'",
body: """<p>Check console output at <a href="${projectBuildURL}">${projectName}</a></p>""",
to: "$recipients",
from: '"Jenkins" <[email protected]>'
)
}
}