Search code examples
jenkinsjenkins-pipelineslackjenkins-groovy

Jenkins pipeline getting error log in slack notification


I’m new to jenkins and slack.
I’m trying to integrate the error part of the build logs into my slack build notifications.
I didn't find documentation on how to get logs in the notification, i'm not looking for uploadfile.


Solution

  • There are different ways to do this. As you mentioned you can either read the Jenkins log and try to determine what the error was. A better approach would be to wrap your logic with a try-catch and send the notification in the catch block.

    pipeline {
      agent any
      stages {
        stage('Example') {
          steps {
            script {
               try {
                    sh 'letsGenerateAError'
                }
                catch (error) {
                    echo "Error occurred while Running. Message : ${error.getMessage()}"
                    slackSend(channel: channel, color: colorCode, message: "Error occurred while Running. Message : ${error.getMessage()}")
                    throw error
                }
            }
          }
        }
      }
    }