Sobes.tech
Junior

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

sobes.tech AI

Answer from AI

To set up build status notifications in Jenkins, you can use several approaches: email plugins, integration with messengers (Slack, Telegram, etc.), or webhooks.

1. Email notifications (Mailer plugin)

  • Install the "Mailer" plugin in Jenkins via "Manage Jenkins" -> "Manage Plugins".
  • Configure SMTP server in "Manage Jenkins" -> "Configure System" under "Email Notification".
  • In the specific job settings, select "Post-build Actions" -> "Editable Email Notification" (if the plugin is installed) or "Email Notification".
  • Specify recipients and set sending conditions (success, failure, unstable build).
// Example of email notification setup in Pipeline
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                // Your build code
            }
        }
    }

    post {
        always {
            // Send notification after build
            mail bcc: '', body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}", cc: '', from: '', replyTo: '', subject: "${env.JOB_NAME} Build Status", to: 'recipient@example.com'
        }
    }
}

2. Integration with messengers (e.g., Slack)

  • Install the plugin for the desired messenger (e.g., "Slack Notification").
  • Configure the integration in the messenger (obtain token or webhook URL).
  • In "Manage Jenkins" -> "Configure System" configure the plugin, specifying the URL and other parameters.
  • In the job settings, under "Post-build Actions," add an action to send notifications via the messenger.
// Example of Slack notifications in Pipeline
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                // Your build code
            }
        }
    }

    post {
        failure {
            // Send notification on failure
            slackSend color: 'danger', message: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
        }
        success {
            // Send notification on success
            slackSend color: 'good', message: "Build ${env.JOB_NAME} #${env.BUILD_NUMBER} successful."
        }
    }
}

3. Webhooks

  • Use the "Generic Webhook Trigger" plugin or Jenkins' built-in capabilities to send webhooks.
  • Configure an external service or script to receive and process webhooks.
  • In the job settings, under "Post-build Actions," add an action to trigger the webhook, specifying the URL and optional parameters.
// Example of default data sent by webhook
{
  "name": "Job Name",
  "url": "job/Job Name/",
  "build": {
    "number": 123,
    "phase": "FINALIZED",
    "status": "SUCCESS",
    "url": "job/Job Name/123/",
    "full_url": "http://jenkins.example.com/job/Job Name/123/",
    "parameters": {
      "PARAM1": "value1",
      "PARAM2": "value2"
    }
  }
}

For each method, it is important to configure the conditions under which notifications are sent (e.g., only on failure, always, on status change). This is done in the plugin settings or in the Pipeline code.