Technotes

Technotes for future me

Send messages via mail and Slack

Below a snippet to send messages in a scripted Jenkins pipeline

Git commit messages and error handling including messages via Slack

node {

  try {
    stage('Checkout SCM') {
      checkout scm
      sh "git rev-parse --short HEAD > commit-id"
      tag = readFile('commit-id').replace("\n", "").replace("\r", "")
      appname = "technotes:"
      registryHost = "blaataap.com:443/"
      env.imageName = "${registryHost}${appname}${tag}"
      env.BUILD_TAG=tag

      sh label: 'prep', script: '''git log --format=\"medium\" -1 | grep Author > git_last_commit_description'''
      sh label: 'prep', script: '''git log --format=\"medium\" -1 | grep Date >> git_last_commit_description'''
      sh label: 'prep', script: ''' git log --format=\"medium\" -1 | grep -v Date | grep -v Author | grep -v commit | grep . >> git_last_commit_description'''
      commitmessage = readFile('git_last_commit_description')
      env.GIT_LAST_COMMIT_DESCRIPTION=commitmessage

      slackSend channel: '#blaataap', color: '#439FE0', message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"
    }

  }
  catch (exc) {
      echo 'I failed'
      echo 'This will run only if the run was marked as unstable'
      mail to: 'blaataap@blaataap.com',
      subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
      body: "Something is wrong with ${env.BUILD_URL}"

      slackSend channel: '#blaataap', color: '#FF2D00', message: "Something is wrong with ${env.BUILD_URL}"
  }
  finally {
      def currentResult = currentBuild.result ?: 'SUCCESS'
      if (currentResult == 'UNSTABLE') {
          echo 'This will run only if the run was marked as unstable'
          mail to: 'blaataap@blaataap.com',
          subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
          body: "Something is wrong with ${env.BUILD_URL}"

          slackSend channel: '#blaataap', color: '#FF2D00', message: "Something is wrong with ${env.BUILD_URL}"
      }

      def previousResult = currentBuild.previousBuild?.result
      if (previousResult != null && previousResult != currentResult) {
          echo 'This will run only if the state of the Pipeline has changed'
          echo 'For example, if the Pipeline was previously failing but is now successful'

          slackSend channel: '#blaataap', color: '#D87D08', message: "Something about failing previous run, now good, build info: ${env.BUILD_URL}"
      }

      echo 'This will always run'
      mail to: 'blaataap@blaataap.com',
      subject: "New version of: ${currentBuild.fullDisplayName}",
      body: """
      We compiled a new website, build info: ${env.BUILD_URL}

      with tag: ${tag}
      """

      slackSend channel: '#blaataap', color: '#08D815', message:
      """
      We compiled a new website, build info: ${env.BUILD_URL}\ncommit info:\n${env.GIT_LAST_COMMIT_DESCRIPTION}\nwith tag: ${tag}
      """
  }

}
Last updated on 31 Jan 2021
Published on 10 Jan 2020
Edit on GitHub