{"id":430,"date":"2026-10-14T08:41:11","date_gmt":"2026-10-14T08:41:11","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2015\/09\/11\/how-to-attach-the-jenkins-build-log-as-part-of-the-email-body\/"},"modified":"2026-10-14T08:41:11","modified_gmt":"2026-10-14T08:41:11","slug":"how-to-attach-the-jenkins-build-log-as-part-of-the-email-body","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-attach-the-jenkins-build-log-as-part-of-the-email-body\/","title":{"rendered":"Jenkins Email-ext: Include Build Logs in Email Body (Full Log or Regex-Filtered)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Jenkins can insert build output directly into notification emails using the Email Extension plugin token macros. This is useful for CI\/CD engineers and on-call responders who need fast failure context without opening the Jenkins console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Originally published on 2015-09-11; fully revised and updated.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2015\/09\/jenkins-build-log-as-part-o.png\" alt=\"Jenkins build log in email body example\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Where to configure log tokens<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In classic Freestyle jobs, these tokens are typically placed in the <strong>Editable Email Notification<\/strong> body fields (for example, <strong>Default Content<\/strong>). The original post specifically notes Freestyle usage.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Install and configure the Jenkins Email Extension plugin (email-ext).<\/li><li>Enable editable email notifications in the job.<\/li><li>Add token macros to the email body content.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Way 1: Send the full build log in the email body<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use this token when you want broad context and do not need filtering:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-text\">${BUILD_LOG, maxLines=<span class=\"hljs-number\">9999<\/span>, escapeHtml=<span class=\"hljs-literal\">false<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This was the original recommendation for Freestyle projects. Increase or reduce <strong>maxLines<\/strong> based on email size limits and readability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Way 2: Send only selected lines with regex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use <strong>BUILD_LOG_REGEX<\/strong> to extract only relevant log lines (for example, failures or specific markers):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-text\">${BUILD_LOG_REGEX, regex=<span class=\"hljs-string\">\"\\\\b(Buildfile|BUILD)\\\\b\"<\/span>, linesAfter=<span class=\"hljs-number\">1<\/span>}\n${BUILD_LOG_REGEX, regex=<span class=\"hljs-string\">\"^.*?BUILD FAILED.*?$\"<\/span>, linesBefore=<span class=\"hljs-number\">0<\/span>, linesAfter=<span class=\"hljs-number\">10<\/span>, maxMatches=<span class=\"hljs-number\">5<\/span>, showTruncatedLines=<span class=\"hljs-literal\">false<\/span>, escapeHtml=<span class=\"hljs-literal\">true<\/span>}\n${BUILD_LOG_REGEX, regex=<span class=\"hljs-string\">\"\\\\b(FC0)\\\\b\"<\/span>, linesAfter=<span class=\"hljs-number\">1<\/span>}\n${BUILD_LOG_REGEX, regex=<span class=\"hljs-string\">\"^.*?FC0.*?$\"<\/span>, maxMatches=<span class=\"hljs-number\">95<\/span>, showTruncatedLines=<span class=\"hljs-literal\">false<\/span>, escapeHtml=<span class=\"hljs-literal\">false<\/span>, matchedLineHtmlStyle=<span class=\"hljs-literal\">true<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">These parameters control what appears around each match: <strong>linesBefore<\/strong>, <strong>linesAfter<\/strong>, <strong>maxMatches<\/strong>, and HTML handling flags such as <strong>escapeHtml<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">BUILD_LOG vs BUILD_LOG_REGEX<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Option<\/th><th>Best for<\/th><th>Main controls<\/th><th>Trade-off<\/th><\/tr><\/thead><tbody><tr><td>BUILD_LOG<\/td><td>Complete log context<\/td><td>maxLines, escapeHtml<\/td><td>Can create very large emails<\/td><\/tr><tr><td>BUILD_LOG_REGEX<\/td><td>Focused alerts (errors, markers)<\/td><td>regex, linesBefore\/After, maxMatches, escapeHtml<\/td><td>Needs careful regex tuning<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How Jenkins builds the email content<\/h2>\n\n\n\n<pre class=\"mermaid\">\nflowchart TD\n    A[Build completes] --> B[Email-ext prepares message]\n    B --> C{Token macro used}\n    C -->|BUILD_LOG| D[Insert log up to maxLines]\n    C -->|BUILD_LOG_REGEX| E[Scan log using regex]\n    E --> F[Collect matches and context lines]\n    D --> G[Send email]\n    F --> G[Send email]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Freestyle setup example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the email body field, many teams combine a focused excerpt and a broader fallback:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript language-text\">Failure summary:\n${BUILD_LOG_REGEX, regex=<span class=\"hljs-string\">\"^.*?BUILD FAILED.*?$\"<\/span>, linesBefore=<span class=\"hljs-number\">0<\/span>, linesAfter=<span class=\"hljs-number\">10<\/span>, maxMatches=<span class=\"hljs-number\">5<\/span>, showTruncatedLines=<span class=\"hljs-literal\">false<\/span>, escapeHtml=<span class=\"hljs-literal\">true<\/span>}\n\nExtended context:\n${BUILD_LOG, maxLines=<span class=\"hljs-number\">2000<\/span>, escapeHtml=<span class=\"hljs-literal\">true<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Start with regex-based excerpts to keep alerts short and actionable.<\/li><li>Prefer <strong>escapeHtml=true<\/strong> unless you explicitly need raw HTML rendering.<\/li><li>Validate regex patterns against real failed logs before broad rollout.<\/li><li>Avoid duplicate token lines in templates to reduce noisy emails.<\/li><li>Confirm SMTP\/email gateway size limits when using large <strong>maxLines<\/strong> values.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">References from the original post<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/wiki.hudson-ci.org\/display\/HUDSON\/Email-ext+plugin\">http:\/\/wiki.hudson-ci.org\/display\/HUDSON\/Email-ext+plugin<\/a><\/li><li><a href=\"http:\/\/stackoverflow.com\/questions\/9439450\/how-will-i-filter-out-only-errors-in-jenkins-email-ext-build-log-regex\">http:\/\/stackoverflow.com\/questions\/9439450\/how-will-i-filter-out-only-errors-in-jenkins-email-ext-build-log-regex<\/a><\/li><li><a href=\"http:\/\/champasoft.blogspot.nl\/2012\/11\/jenkins-pattern-for-e-mailing-log.html\">http:\/\/champasoft.blogspot.nl\/2012\/11\/jenkins-pattern-for-e-mailing-log.html<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":3363,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[42],"tags":[1449,91,473,1451,1122,545,1454,145,1453,1455,984,1450,593,732,104,1452],"class_list":["post-430","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jenkins","tag-attach","tag-build","tag-email","tag-email-body","tag-guide","tag-how","tag-how-to-attach-jenkins-build-log","tag-jenkins","tag-jenkins-build-log","tag-jenkins-build-log-in-email-body","tag-log","tag-part","tag-process","tag-steps","tag-tutorials","tag-ways"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=430"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":79304,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/430\/revisions\/79304"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3363"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}