MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Jenkins Email-ext: Add Full or Regex-Filtered Build Logs to Email Body

Originally published on 2015-10-03; fully revised and updated.

If your team depends on Jenkins email alerts, putting the right build-log content in the email body can speed up incident triage. This guide explains how to use Email Extension (email-ext) content tokens for full logs and regex-filtered snippets, plus practical examples you can copy into job configuration or Pipeline.

Jenkins build log as part of email body

Choose the right log-in-email strategy

There are two common approaches: include the whole log, or include only matched lines (with context). The second option keeps emails concise when builds produce large output.

Token or linkWhat it adds to the emailBest forTrade-off
${BUILD_LOG, maxLines=9999, escapeHtml=false}Full (or near-full) build console outputSmall/medium logs, immediate contextCan create very long and noisy emails
${BUILD_LOG_REGEX, ...}Only lines matching a regex, optionally with lines before/afterFailure summaries, error-first notificationsNeeds careful regex tuning
${BUILD_URL}/artifact/Link to build artifactsReports, logs, packaged outputsRequires Jenkins access and artifact retention
${PROJECT_URL}/ws/Link to workspaceQuick inspection in trusted/internal environmentsMay be inaccessible or restricted by security settings

How email-ext token rendering works in notifications

When a job trigger fires an email-ext notification, Jenkins resolves tokens in the subject/body and injects log content based on token parameters.

flowchart TD
    A[Build runs in Jenkins] --> B[Email trigger condition met]
    B --> C[email-ext composes message]
    C --> D{Token used in body}
    D --> E[BUILD_LOG inserts full log]
    D --> F[BUILD_LOG_REGEX filters by pattern]
    E --> G[Email sent to recipients]
    F --> G
    G --> H[Team triages result]

Way 1: Include the full build log in the email body

For freestyle-style configuration, place a BUILD_LOG token in the email-ext content field (for example, in Default Content or job-level editable email content).

  • Open the job configuration and locate Email Extension settings.
  • Add the build-log token to the email body template.
  • Save and run a build to verify output length and readability.
${BUILD_LOG, maxLines=9999, escapeHtml=false}Code language: JavaScript (javascript)

If your logs are large, reduce maxLines and consider escapeHtml=true to avoid accidental HTML rendering in messages.

Way 2: Include only specific lines with regex

Use BUILD_LOG_REGEX to send only important lines (for example failures, specific error codes, or a small context window around matched patterns).

${BUILD_LOG_REGEX, regex="\\b(Buildfile|BUILD)\\b", linesAfter=1}
${BUILD_LOG_REGEX, regex="^.*?BUILD FAILED.*?$", linesBefore=0, linesAfter=10, maxMatches=5, showTruncatedLines=false, escapeHtml=true}
${BUILD_LOG_REGEX, regex="\\b(FC0)\\b", linesAfter=1}
${BUILD_LOG_REGEX, regex="^.*?FC0.*?$", maxMatches=95, showTruncatedLines=false, escapeHtml=false, matchedLineHtmlStyle=true}Code language: JavaScript (javascript)

Regex parameter quick reference

ParameterPurposeTypical usage
regexPattern to match in console outputFind failure lines, warnings, IDs
linesBefore / linesAfterAdd context around each matchShow stack trace or command around error
maxMatchesLimit number of matched sectionsAvoid oversized messages
showTruncatedLinesControl truncation markersKeep output clean for readers
escapeHtmlEscape HTML-sensitive textSafer rendering for email clients

Useful tokens and formatting tips for email body templates

${PROJECT_URL}/ws/
${BUILD_URL}/artifact/
${BUILD_LOG, maxLines=1000}

String-argument escaping in token parameters follows the original guidance: escape quotes and backslashes inside arguments. For HTML emails, line breaks are typically added with <br>.

arg1="\"quoted\""; arg2="c:\\path"; arg3="one\\ntwo"Code language: JavaScript (javascript)

Pipeline example using emailext

If you send notifications from Jenkins Pipeline, you can keep tokenized content in the emailext body.

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'make test'
      }
    }
  }
  post {
    failure {
      emailext(
        subject: "Build failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
        to: 'team@example.com',
        body: '''Build URL: ${BUILD_URL}<br/>
Artifacts: ${BUILD_URL}/artifact/<br/><br/>
${BUILD_LOG_REGEX, regex="^.*?BUILD FAILED.*?$", linesBefore=0, linesAfter=15, maxMatches=5, escapeHtml=true}'''
      )
    }
  }
}Code language: PHP (php)

Original references

Common pitfalls

  • Sending too many log lines can cause unreadable or clipped emails.
  • Using escapeHtml=false may render raw HTML from logs in email clients.
  • Regex patterns often fail due to missing escaping; validate on a sample failed build.
  • Workspace and artifact links depend on Jenkins URL configuration, permissions, and retention.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Jenkins Email-ext: Include Build Logs in Email Body (Full Log or Regex-Filtered)

Learn how to configure Jenkins Email Extension tokens to send either the full build log or targeted regex matches directly in the email body. This guide covers practical token examples, workflow, and pitfalls to avoid noisy or oversized alerts.

Read More

Jenkins at Scale: Practical References and Admin Cheatsheet

This revised guide upgrades a short 2015 Jenkins link post into an operations-ready reference for teams running Jenkins at scale. It preserves the original links and adds a workflow diagram, comparison table, and practical API/CLI examples.

Read More

Foodcritic for Chef Cookbooks: Practical Linting and Jenkins CI for Legacy Pipelines

Foodcritic helps you statically lint Chef cookbooks for style, correctness, and common anti-patterns before convergence fails in production. This guide shows how to run it locally, wire it into Jenkins, and enforce fail policies that teams will actually use.

Read More

Jenkins Backup and Restore Runbook: ThinBackup, Filesystem Archives, and Recovery Steps

A practical Jenkins backup and restore guide for DevOps teams, covering what to back up, strategy trade-offs, and step-by-step command examples. Includes a workflow diagram and a recovery checklist you can operationalize.

Read More

Jenkins Troubleshooting Guide: Fast Triage, Log Analysis, and Common Failure Fixes

A practical Jenkins troubleshooting guide for DevOps teams: find the right logs first, separate controller vs. agent vs. job failures, and resolve common outage patterns quickly.

Read More

Jenkins Interview Questions, Answered: CI, APIs, Installation, Backups, and Essential Plugins

This revised guide turns common Jenkins interview prompts into practical answers you can explain with confidence. It covers CI fundamentals, API usage, setup on Ubuntu and RHEL-like systems, backup strategy, and key plugins.

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month ago

Including Jenkins build logs in email alerts can help teams spot failures without opening the Jenkins dashboard. However, sending full logs every time can make emails too long and hard to read. A useful approach is to include the key error details and link to the complete console output. Teams should also check that logs do not expose passwords, tokens, or other sensitive data. This keeps alerts clear, useful, and safer to share.

Anuj kumar
Anuj kumar
1 month ago

Including Jenkins build logs in email notifications is helpful, but sending too much output can make important failures difficult to spot. For larger pipelines, I would prefer highlighting the failed stage, relevant error messages, and a direct link to the complete console output rather than embedding thousands of lines in every email. It is also worth checking log size limits and masking credentials or sensitive environment variables before sending notifications. This approach keeps alerts readable and useful without exposing information that should remain inside the build environment.

2
0
Would love your thoughts, please comment.x
()
x