Skip to content

Instantly share code, notes, and snippets.

@yermulnik
Last active June 9, 2024 13:34
Show Gist options
  • Save yermulnik/44e591251bea0a08d5124b80ac796ee7 to your computer and use it in GitHub Desktop.
Save yermulnik/44e591251bea0a08d5124b80ac796ee7 to your computer and use it in GitHub Desktop.
Jenkins `buildNotifier.groovy`
@NonCPS
/*
* slackChannel = '#channel,@user' - Slack channel for notifications, space- or comma-separated list (leave empty to use global Jenkins settings)
* silentNotifyFor = ['STARTED'] - Silent notifications for certain buildStatuses. E.g.: silentNotifyFor = ['STARTED','SUCCESS','FAILURE','ABORTED']
*/
def notifyBuild(String buildStatus = 'FAILURE', String customMessage = null) {
// buildStatus of null means FAILURE
buildStatus = buildStatus ?: 'FAILURE'
// Default values
def colorCode = '#000000'
def buildCause = []
def buildCauses = currentBuild.rawBuild.getCauses()
for (cause in buildCauses) {
if (cause != null && !buildCause.contains(cause.properties.shortDescription)) {
buildCause << cause.properties.shortDescription
}
}
buildCause = buildCause ?: "[${buildCauses}]"
def header = (buildStatus != 'STARTED') ? "${buildStatus} after ${currentBuild.durationString.replaceAll(/\s+and\s+counting$/, '')}" : buildStatus
def subject = "${header}: ${currentBuild.fullDisplayName} ${buildCause}"
def summary = "${subject} (<${env.BUILD_URL}|Open>)"
if (customMessage) {
summary += "\n${customMessage}"
}
// Override default values based on build status
// Colors: good, warning, danger, or any hex color code (eg. #439FE0)
// Statuses: http://javadoc.jenkins-ci.org/hudson/model/Result.html
switch(buildStatus) {
case 'FAILURE':
colorCode = 'danger'
break
case 'STARTED':
colorCode = '#00CCFF'
break
case 'SUCCESS':
colorCode = 'good'
break
case 'ABORTED':
colorCode = '#CCCCCC'
break
default:
colorCode = '#000000'
}
// Include Test Summary
if (buildStatus != 'STARTED' && binding.variables.get("notificationTestSummary")) {
summary += "\n" + notificationTestSummary
}
// Include SCM Changelog
if (buildStatus != 'STARTED' && binding.variables.get("notificationSCMChanges")) {
summary += "\n" + notificationSCMChanges
}
// Send notifications
if (!(binding.variables.get("silentNotifyFor")) || !(buildStatus in silentNotifyFor)) {
if (binding.variables.get("slackChannel")) {
// Send to custom Slack channel if defined
slackSend (color: colorCode, message: summary, channel: slackChannel)
} else {
// Send to globally defined Slack channel
slackSend (color: colorCode, message: summary)
}
}
}
// Send E-mail notifications
/*
* emailRecipients = '[email protected],[email protected]' - Comma-separated list of E-mail Notification Recipients
*/
def notifyEmail() {
def msgTitle = '[buildNotifier.notifyEmail]'
// Mark failed if recipients list is not passed
if (!(binding.variables.get("emailRecipients"))) {
currentBuild.result = 'FAILURE'
echo "${msgTitle} Missing required parameter: emailRecipients"
} else {
// Set default values
emailNotifyEveryUnstableBuild = binding.variables.get("emailNotifyEveryUnstableBuild") ? true : false
emailSendToIndividuals = binding.variables.get("emailSendToIndividuals") ? true : false
step([$class: 'Mailer',
recipients: emailRecipients,
notifyEveryUnstableBuild: emailNotifyEveryUnstableBuild,
sendToIndividuals: emailSendToIndividuals
])
}
}
return this;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment