Friday, 1 May 2020

Send notifications to Slack from Jenkins pipeline


If you want to receive notifications using Jenkins declarative pipelines, this post is for you!

Suppose you have a channel named #mychannel, and want to receive:
- a notification when the pipeline starts
- a notification when the pipeline ends, both if failed or succeeded.

You can do as the following:


//Declarative pipeline
pipeline {
agent any
stages {
stage('Clone sources') {
steps {
slackSend (channel: "#mychannel", color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
git credentialsId: 'git_credentials', poll: false, branch: 'develop', url: 'https://mysource.com/mysimplenodejsexample.git'
}
}
stage('Install npm packages') {
steps {
nodejs(nodeJSInstallationName: 'node13') {
sh 'npm i'
}
}
}
stage('Build app') {
steps {
nodejs(nodeJSInstallationName: 'node13') {
sh 'npm run build:dev'
}
}
}
}
post {
success {
slackSend (channel: "#mychannel", color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (channel: "#mychannel", color: '#FF0000', message: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}


And this is the result:





No comments:

Post a Comment