Attach build logs to jenkins mail

To attach build logs to Jenkins mail notifications, you can follow these steps:

Method 1: Using the Jenkins "Attach build log" feature

  1. Go to your Jenkins job configuration page.
  2. Scroll down to the "Post-build Actions" section.
  3. Click on the "Add post-build action" dropdown and select "Attach build log".
  4. Choose the log level you want to attach (e.g., "Full log", "Error log", etc.).
  5. Save the changes.

From now on, whenever a build is triggered, Jenkins will attach the selected log level to the email notification.

Method 2: Using a Jenkins plugin

There are several plugins available that can help you attach build logs to Jenkins email notifications. Here are a few popular ones:

  1. Email-ext plugin: This plugin allows you to customize the email content and attachments. You can use the ${BUILD_LOG} variable to include the build log in the email.
  2. Build Log Archiver plugin: This plugin allows you to archive and attach build logs to email notifications.
  3. Jenkins Email Extender plugin: This plugin provides additional features for customizing email notifications, including the ability to attach build logs.

To use one of these plugins, follow these steps:

  1. Install the plugin from the Jenkins Plugin Manager.
  2. Configure the plugin according to its documentation.
  3. In your Jenkins job configuration page, add the plugin as a post-build action.

Method 3: Using a script

If you prefer a more manual approach, you can use a script to attach the build log to the email notification. Here's an example using Groovy:

import hudson.model.AbstractBuild
import hudson.model.BuildLog

// Get the current build
def build = Thread.currentThread().executable

// Get the build log
def log = build.getLog()

// Attach the log to the email
def email = build.getCauses().find { it instanceof hudson.model.Cause.UserCause }.getRecipient()
email.addAttachment("build-log.txt", log.getText())

Add this script as a post-build action in your Jenkins job configuration page.

Remember to adjust the script according to your Jenkins version and email notification settings.