DEV Community


Jenkins Slack Notifications

Committed Software
Originally published at on ・1 min read


To add a slack notification from our continuous integration builds on Jenkins Blue Ocean we use the Slack Notification plugin. Adding a set of functions to the Jenkinsfile which is then called in the post 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 {
  post {
    success {
    unstable {
    failure {

def notifyBuild(String buildStatus = 'STARTED', String colorCode = '#5492f7', String notify = '') {

  def project = 'projectName'
  def channel = "${project}"
  def base = "${project}/commits/" 

  def commit = sh(returnStdout: true, script: 'git log -n 1 --format="%H"').trim()
  def link = "${base}${commit}" 
  def shortCommit = commit.take(6)
  def title = sh(returnStdout: true, script: 'git log -n 1 --format="%s"').trim()
  def subject = "<${link}|${shortCommit}> ${title}" 

  def summary = "${buildStatus}: Job <${env.RUN_DISPLAY_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>\n${subject} ${notify}"

  slackSend (channel: "#${channel}", color: colorCode, message: summary)


def author() {
  return sh(returnStdout: true, script: 'git log -n 1 --format="%an" | awk \'{print tolower($1);}\'').trim()

def notifyStarted() {

def notifySuccess() {
  notifyBuild('SUCCESS', 'good')

def notifyUnstable() {
  notifyBuild('UNSTABLE', 'warning', "\nAuthor: @${author()} <${RUN_CHANGES_DISPLAY_URL}|Changelog>")

def notifyFailed() {
  notifyBuild('FAILED', 'danger', "\nAuthor: @${author()} <${RUN_CHANGES_DISPLAY_URL}|Changelog>")

Discussion (0)

Forem Open with the Forem app