Jenkins pipeline slack notification

Arif KIZILTEPE
3 min readOct 7, 2022

If you wanna get notification when Jenkins pipeline success/failure, follow below steps can integrate jenkins and slack easily.

Firstly install Slack Notification plugin.

If use freestyle job on Jenkins everything easy. Because all property exist in jenkins freestyle configure dashboard. But when use jenkins pipeline must configure message and message status in groovy script.

This definition code is jenkins failure and success message. This code set danger all non-successfull status.

Note : Slack notification plugins need jenkins integration in slack app. If don’t have any slack integration, can follow below documents.

/* Slack Notification Set */
def notifyProductionDeploy() {
if (currentBuild.currentResult == 'SUCCESS') {
def message = "@here Build <${env.BUILD_URL}|${currentBuild.displayName}> " +
"${currentBuild.currentResult} deployed to the production"
slackSend(message: message, channel: '#micro', color: 'good', token: 'token')
} else {
def message = "@here Build <${env.BUILD_URL}|${currentBuild.displayName}> " +
"${currentBuild.currentResult} deployed to the production"
slackSend(message: message, channel: '#micro', color: 'danger', token: 'token')
}
}

You can customize message easyl. See all jenkins env var JENKINSURL/pipeline-syntax/globals#env

Slack notification message was set. Now add new stage for example.

stage('notifyProductionDeploy') {
// do stuff
notifyProductionDeploy()
}

Full pipeline example

/* Slack Notification Set */
def notifyProductionDeploy() {
if (currentBuild.currentResult == 'SUCCESS') {
def message = "@here Build <${env.BUILD_URL}|${currentBuild.displayName}> " +
"${currentBuild.currentResult} deployed to the production"
slackSend(message: message, channel: '#micro', color: 'good', token: 'token')
} else {
def message = "@here Build <${env.BUILD_URL}|${currentBuild.displayName}> " +
"${currentBuild.currentResult} deployed to the production"
slackSend(message: message, channel: '#micro', color: 'danger', token: 'token')
}
}


node('DefaultJenkinsMachine') {
def app


stage('Clone repository') {
/* Let's make sure we have the repository cloned to our workspace */

checkout scm
load ".envvars"
echo " Version ${_VERSION_}"

}

stage('Build image') {
/* This builds the actual image; synonymous to
* docker build on the command line */

app = docker.build("nexus.xxx.com:8083/hellonode:${_VERSION_}")
}

stage('Test image') {
/* Ideally, we would run a test framework against our image.
* For this example, we're using a Volkswagen-type approach ;-) */
echo "NoN-Test"

}

stage('Push image') {
sh "docker push nexus.xxx.com:8083/hellonode:${_VERSION_}"

}

}
node('VTGENK8SMAS01') {
stage('Publish image on kubernetes') {
sh "hostname"
checkout scm
sh "sed -i 's/_APP_NAME_/${_APP_NAME_}/g' kubernetes.yml"
sh "sed -i 's/_APP_NAMESPACE_/${_APP_NAMESPACE_}/g' kubernetes.yml"
sh "sed -i 's/_APP_REPLICA_/${_APP_REPLICA_}/g' kubernetes.yml"
sh "sed -i 's/_DOCKER_REP_URL_/${_DOCKER_REP_URL_}/g' kubernetes.yml"
sh "sed -i 's/_IMAGE_NAME_/${_IMAGE_NAME_}/g' kubernetes.yml"
sh "sed -i 's/_VERSION_/${_VERSION_}/g' kubernetes.yml"
sh "sed -i 's/_APP_URL_/${_APP_URL_}/g' kubernetes.yml"
sh "/usr/bin/kubectl apply -f kubernetes.yml"
sh "/usr/bin/kubectl get pod --all-namespaces"

}
}
stage('notifyProductionDeploy') {
// do stuff
notifyProductionDeploy()
}

Everystep finish. Let’s try

--

--