Committed Software
Posted on • Originally published atcommitted.software on
Jenkins Slack Notifications
To add a slack notification from our continuous integration builds on Jenkins Blue Ocean we use theSlack Notification plugin. Adding a set of functions to the Jenkinsfile which is then called in thepost
section of the pipeline. We use the git command to extract the committers name and the hash of the commit so we can link to the commit in bitbucket and add the author’s name if the build has failed or is unstable so it will be brought to their attention.
pipeline{agent{...}stages{stage('env'){steps{notifyStarted()}}...}post{success{notifySuccess()}unstable{notifyUnstable()}failure{notifyFailed()}}}defnotifyBuild(StringbuildStatus='STARTED',StringcolorCode='#5492f7',Stringnotify=''){defproject='projectName'defchannel="${project}"defbase="https://bitbucket.org/committed/${project}/commits/"defcommit=sh(returnStdout:true,script:'git log -n 1 --format="%H"').trim()deflink="${base}${commit}"defshortCommit=commit.take(6)deftitle=sh(returnStdout:true,script:'git log -n 1 --format="%s"').trim()defsubject="<${link}|${shortCommit}> ${title}"defsummary="${buildStatus}: Job <${env.RUN_DISPLAY_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>\n${subject} ${notify}"slackSend(channel:"#${channel}",color:colorCode,message:summary)}defauthor(){returnsh(returnStdout:true,script:'git log -n 1 --format="%an" | awk \'{print tolower($1);}\'').trim()}defnotifyStarted(){notifyBuild()}defnotifySuccess(){notifyBuild('SUCCESS','good')}defnotifyUnstable(){notifyBuild('UNSTABLE','warning',"\nAuthor: @${author()} <${RUN_CHANGES_DISPLAY_URL}|Changelog>")}defnotifyFailed(){notifyBuild('FAILED','danger',"\nAuthor: @${author()} <${RUN_CHANGES_DISPLAY_URL}|Changelog>")}
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse