Sobes.tech
Junior

How to set up notifications in Jenkins for successful or failed builds?

sobes.tech AI

Answer from AI

To set up notifications in Jenkins, you can use the following methods:

  1. Built-in email notifications:

    • Go to "Manage Jenkins" -> "Configure System".
    • Find the "Email Notification" or "Extended E-mail Notification" section.
    • Set the SMTP server, credentials, and test email.
    • Save changes.
    • In each Job configuration, in the "Post-build Actions" section, add "Editable Email Notification" (if the plugin is installed) or "E-mail Notification".
    • Specify the recipient list, email subjects, and sending conditions (always, on success, on failure, on unstable build).
  2. Using plugins for integration with messengers/systems:

    • Install the appropriate plugin from "Manage Jenkins" -> "Manage Plugins" -> "Available plugins". Examples:
      • Slack Notification Plugin
      • Telegram Bot
      • Discord Notification Plugin
      • Microsoft Teams Notification Plugin
      • Jira Notification Plugin
    • Configure the installed plugin (usually in "Manage Jenkins" -> "Configure System" or through a new section in the specific Job settings) to connect to the external system (tokens, webhook URL, etc.).
    • In each Job configuration, in the "Post-build Actions" section, add the action provided by the plugin and configure it (channel/recipient, sending conditions).
  3. Groovy scripts in Pipeline:

    • When using Jenkins Pipeline (Declarative or Scripted), notifications can be integrated directly into the script.

    • Example in Declarative Pipeline using the mail step:

      // Declarative Pipeline
      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      // Your build code
                      echo 'Performing build...'
                  }
              }
          }
          post {
              success {
                  mail bcc: '', body: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} completed successfully.", cc: '', from: '', subject: "Success: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", to: 'recipient@example.com'
              }
              failure {
                  mail bcc: '', body: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} failed. Details: ${env.BUILD_URL}", cc: '', from: '', subject: "Failure: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", to: 'recipient@example.com'
              }
          }
      }
      
    • Example in Scripted Pipeline:

      // Scripted Pipeline
      node {
          try {
              stage('Build') {
                  // Your build code
                  echo 'Performing build...'
              }
              // Successful completion
              currentBuild.result = 'SUCCESS'
          } catch (err) {
              // Error handling
              currentBuild.result = 'FAILURE'
              throw err // Re-throw the exception to mark the build as failed
          } finally {
              // Send notifications regardless of the result
              if (currentBuild.result == 'SUCCESS') {
                  mail bcc: '', body: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} completed successfully.", cc: '', from: '', subject: "Success: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", to: 'recipient@example.com'
              } else {
                  mail bcc: '', body: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} failed. Details: ${env.BUILD_URL}", cc: '', from: '', subject: "Failure: ${env.JOB_NAME} - ${env.BUILD_NUMBER}", to: 'recipient@example.com'
              }
          }
      }
      
    • For integration with messengers, steps provided by the respective plugins (e.g., slackSend) are used in Pipeline.

  4. Using hooks:

    • Some external systems can send webhooks to Jenkins on certain events. Conversely, Jenkins can be configured to execute a script or send a request to an external system after build completion. Plugins or custom scripts in the "Post-build Actions" section ("Execute shell" or "Execute Windows batch command") are used for this.

The choice of method depends on your needs, the build system used (Freestyle Job or Pipeline), and the notification services integrated. The most flexible and modern approach is to use Pipeline with appropriate notification steps.