Skip to content
Advertisement

Jenkins BUILD_LOG_REGEX ( emailext plugin ) does not match across multiple lines ( dotall )

Background

I am trying to use the BUILD_LOG_REGEX environment variable, which is provided by the emailext plugin, to filter a build log. My regular expression is suppose to match everything between the “init:” and “Total time” strings. My regular expression is as follows init:(.*?)Totalstime

The Jenkins emailext code is as follows

emailext( 
    body: "${BUILD_LOG_REGEX, regex="init:(.*?)Total\stime", maxMatches=10000000, showTruncatedLines=false, escapeHtml=true}",
    subject: 'Test Subject',
    mimeType: 'text/html',
    to: 'noname@gmail.com'
)

The build log is as follows

Started by user Chris Maggiulli
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on EC2 (sis-account) - java-build-11 (sir-2viyb9qh) in /home/ec2-user/workspace/z_test/email-build-log
[Pipeline] {
[Pipeline] stage (checkout)
Using the ‘stage’ step without a block argument is deprecated
Entering stage checkout
Proceeding
[Pipeline] checkout
Using sole credentials awsjobs/****** (awsjobs-AD-User) in realm ‘<https://svn.int.excelsior.edu:443> Private’
Updating https://svn.int.excelsior.edu/svn/sis_dev2 at revision '2021-08-10T13:18:37.454 +0000' --quiet
[Pipeline] stage (build)
Using the ‘stage’ step without a block argument is deprecated
Entering stage build
Proceeding
At revision 27558

[Pipeline] dir
Running in /home/ec2-user/workspace/z_test/email-build-log/sis
[Pipeline] {
[Pipeline] sh
+ ant -Dsis.root=.. -Djava.home=/usr/lib/jvm/java-11-amazon-corretto.x86_64 -Djava.exec=/usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java make
Buildfile: /home/ec2-user/workspace/z_test/email-build-log/sis/build.xml

init:

compileSis:
     [copy] Copying 1 file to /home/ec2-user/workspace/z_test/email-build-log/sis/sis/WEB-INF/classes
     [copy] Copying 9 files to /home/ec2-user/workspace/z_test/email-build-log/sis/sis/WEB-INF/classes
    [javac] /home/ec2-user/workspace/z_test/email-build-log/sis/build.xml:108: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 3372 source files to /home/ec2-user/workspace/z_test/email-build-log/sis/sis/WEB-INF/classes
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 7
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 warning

createSisWar:
   [delete] Deleting: /home/ec2-user/workspace/z_test/email-build-log/deploy/tomcat/sis.war
      [war] Building war: /home/ec2-user/workspace/z_test/email-build-log/deploy/tomcat/sis.war

FullSisBuild:

make:
     [echo] build complete.

BUILD SUCCESSFUL
Total time: 25 seconds
[Pipeline] }
[Pipeline] // dir
[Pipeline] stage (email)
Using the ‘stage’ step without a block argument is deprecated
Entering stage email
Proceeding
[Pipeline] emailext
Sending email to: noname@gmail.com
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

While this works in almost all online regex testers it does not work in Jenkins. I believe this is related to how the BUILD_LOG_REGEX macro implements dotall but I can’t get it to match across multiple lines. I realize that BUILD_LOG_REGEX is implemented using java.util.regex.Pattern. Can someone assist me with this?

Attempts

I’ve also tried the following regex init:(?s:.*)Totalstime as well as attempting to use the BUILD_LOG_MULTILINE_REGEX ( with both regular expressions ) but both return no matches.

Advertisement

Answer

Solution BUILD_LOG_EXCERPT

I found a roundabout solution using the BUILD_LOG_EXCERPT token. The following code sample gives the results I am looking for using this token.

emailext(
   subject: "compile-check failed for $APP",
   body: "${BUILD_LOG_EXCERPT, start="init:.*", end="Total\stime.*"}",
   replyTo: '$DEFAULT_REPLYTO',
   mimeType: 'text/html',
   recipientProviders: [culprits()],
   to: 'noone@gmail.com'
)

The unescaped string is ${BUILD_LOG_EXCERPT, start="init:.*", end="Totalstime.*"}

I am still interested in a solution using BUILD_LOG_REGEX or BUILD_LOG_MULTILINE_REGEX

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement