Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkinsfile + Slack: avisa solo cuando cambio el estado del build
#!groovy
stage 'build and tests'
def project
def slack
node {
def workspace = pwd()
slack = load "${workspace}@script/slack.groovy"
project = load "${workspace}@script/project.groovy"
try {
//get things done
} catch (e) {
currentBuild.result = "FAILED"
throw e
} finally {
slack.notifyToSlack(currentBuild.result)
}
}
def getLastBuildStatus() {
return currentBuild.rawBuild.getPreviousBuild()?.getResult().toString()
}
def notifyToSlack(String buildStatus = 'STARTED') {
buildStatus = buildStatus ?: 'SUCCESS'
if (mustNotify(buildStatus)) {
def color = getColorByStatus(buildStatus)
slackSend(message: "${env.JOB_NAME} - #${env.BUILD_NUMBER} - ${buildStatus}",
channel: "#your-channel", color: color, token: "yourToken")
return
}
echo "No debe notificar por Slack"
}
def getColorByStatus(String buildStatus = 'STARTED') {
if (buildStatus == 'STARTED') {
//amarillo
colorCode = '#FFFF00'
} else if (buildStatus == 'SUCCESS') {
//verde
colorCode = '#00FF00'
} else {
//rojo
colorCode = '#FF0000'
}
return colorCode;
}
def mustNotify(String buildStatus) {
def lastBuildStatus = getLastBuildStatus()
return !buildStatus.equals(lastBuildStatus)
}
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.