Created
October 21, 2016 19:44
-
-
Save jplagostena/9c53f1812d8c3cb0aa1a7a720550410f to your computer and use it in GitHub Desktop.
Jenkinsfile + Slack: avisa solo cuando cambio el estado del build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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