Can't access $BUILD_LOG in Jenkins pipeline

Still haven't found a solution to use BUILD_LOG parameter in a pipeline job with emailext plugin.

As a small solace, I found a workaround to access build log in another way:

currentBuild.rawBuild.getLog(15) 

Where 15 is a number of last log lines I want to show.

The example is:

  emailext attachLog: true,
                    body: "Build failed" +
                            "<br> See attached log or URL:<br>${env.BUILD_URL}" +
                            "<br><br> <b>The end of build log is:</b> <br>" +
                            currentBuild.rawBuild.getLog(15).join("<br>"),

                    mimeType: 'text/html',
                    subject: "Build failed",
                    to: '[email protected]'

Note that you have to approve a few script signatures at the In-Process Script Approval in order to allow usage of this function.


I had the same problem with declarative pipelines and a step like:

emailext(
  subject: "foo",
  to: "bar",
  body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
  <p>Console output (last 250 lines):<hr><pre>${BUILD_LOG}</pre></p>"""

I had email ext plugin installed.

The solution was to escape the $-sign of the the macro that should be expanded by the plugin, like so:

...
<p>Console output (last 250 lines):<hr><pre>\${BUILD_LOG}</pre></p>"""
...

And be sure to use double quotes.

This way groovy (?) will first expand all the environment variables, and leaves the escaped variables to be dealt with by email ext plugin.


From the answer you linked the BUILD_LOG variable is set by the email-extension plugin. Do you have this configured correctly as this may be your issue.