Skip to content

Instantly share code, notes, and snippets.

@electron0zero
Forked from beatngu13/Jenkinsfile
Created February 27, 2018 20:59
Show Gist options
  • Select an option

  • Save electron0zero/2a8ad5d4b8950a7a4d75a6e48c5b213e to your computer and use it in GitHub Desktop.

Select an option

Save electron0zero/2a8ad5d4b8950a7a4d75a6e48c5b213e to your computer and use it in GitHub Desktop.
Fancy notifications for Slack and HipChat in a Jenkins Pipeline
// Based on https://jenkins.io/blog/2016/07/18/pipline-notifications/.
def notifyMessengers(String buildStatus = 'STARTED') {
// Build status of null means successful.
buildStatus = buildStatus ?: 'SUCCESS'
// Replace encoded slashes.
def decodedJobName = env.JOB_NAME.replaceAll("%2F", "/")
def colorSlack
def colorHipchat
if (buildStatus == 'STARTED') {
colorSlack = '#D4DADF'
colorHipchat = 'GRAY'
} else if (buildStatus == 'SUCCESS') {
colorSlack = '#BDFFC3'
colorHipchat = 'GREEN'
} else if (buildStatus == 'UNSTABLE') {
colorSlack = '#FFFE89'
colorHipchat = 'YELLOW'
} else {
colorSlack = '#FF9FA1'
colorHipchat = 'RED'
}
def msgSlack = "${buildStatus}: `${decodedJobName}` #${env.BUILD_NUMBER}: ${env.BUILD_URL}"
def msgHipchat = "${buildStatus}: <code>${decodedJobName}</code> #${env.BUILD_NUMBER}: <a href=\"${env.BUILD_URL}\">${env.BUILD_URL}</a>"
slackSend(color: colorSlack, message: msgSlack)
hipchatSend(color: colorHipchat, message: msgHipchat)
}
node {
try {
notifyMessengers()
// Existing build steps.
} catch (e) {
currentBuild.result = "FAILURE"
throw e
} finally {
notifyMessengers(currentBuild.result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment