How can I get the last 100 lines of the console output and add it to the body of an email?

Jenkins setup:
Jenkins: 2.479.1
OS: Linux - 6.8.0-48-generic
Java: 17.0.13 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

I have a pipeline that sends an email when the pipeline fails. I would like to include the last 100 lines of the console output in the email message. How do I get that?

The part below that I am having trouble with is <pre>\${BUILD_LOG, maxLines=100, escapeHtml=false}</pre>

mail bcc: '', 
    body: """
<p>Pipeline Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}</p>
<p>Build URL: ${env.BUILD_URL}</p>
<p>Build Log (Last 100 Lines):</p>
<pre>\${BUILD_LOG, maxLines=100, escapeHtml=false}</pre>
""", 
    cc: '', 
    charset: 'UTF-8', 
    from: 'noreply@email.com', 
    mimeType: 'text/html', 
    replyTo: 'noreply@email.com', 
    subject: "Pipeline Failed: ${env.JOB_NAME} ${env.BUILD_NUMBER}", 
    to: "recipient@email.com";

Are you getting an error or something else? Can you describe what issue you are having with that line?

That line doesn’t work. It doesn’t get the build log.

Oh, I just noticed that you are using the mail step, that one doesn’t support tokens like BUILD_LOG. You would need to use the emailext step (Email Extension Plugin) which is part of the email-ext plugin (Email Extension).

1 Like

Thank you! I’ll give it a try!

Hi @dionmunk. Did you find the solutions? :grin:

Yes, switching from mail to emailext was the right way to go. Like @slide_o_mix said, it gave me access to the ${BUILD_LOG} token/variable, which let me include the last 100 lines of it in email notifications whenever I have a task that fails.

1 Like