7

How do I access $BUILD_LOG in a Jenkins pipeline, or is there a better way of getting the log output?

Going off of this answer, I've been trying to access the $BUILD_LOG environment variable, but when I try

echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"

the build errors out:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: unexpected token: BUILD_LOG @ line 11, column 29.
                       echo "${BUILD_LOG, maxLines=50, escapeHtml=false}"

And if I try

echo "$BUILD_LOG"

I get this error:

groovy.lang.MissingPropertyException: No such property: BUILD_LOG for class: groovy.lang.Binding

What am I doing wrong? Or is this the wrong way to access the printed output?

jpyams
  • 4,030
  • 9
  • 41
  • 66

3 Answers3

6

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.

anttikoo
  • 785
  • 8
  • 20
  • Escaping the $ worked for me. I don't understand why it works though? – Chris Maggiulli Aug 09 '21 at 12:45
  • 1
    @ChrisMaggiulli there are 2 different postprocess steps in play here (Jenkins itself, and emailext plugin), and unfortunately both use `$` to indicate a macro. `$BUILD_LOG` belongs to emailext, and if the `$`-sign is not escaped, Jenkins tries to replace it with a value. But Jenkins doesn't know this macro, so it replaces it with empty string (or something). When it is escaped with \-character, Jenkins just strips the \ from it, leaving the `$BUILD_LOG` macro to be handled by emailext plugin. So escaping delays the processing of the macro until correct stage. – anttikoo Aug 09 '21 at 14:42
3

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: 'myemail@somedomain.com'

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

Vladimir
  • 429
  • 7
  • 21
2

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.

jpyams
  • 4,030
  • 9
  • 41
  • 66
CarlMc
  • 210
  • 2
  • 11
  • 2
    That was it. I was trying to use `${BUILD_LOG}` outside of the `emailext` step. Apparently this variable only works there. – jpyams Jan 03 '18 at 17:09
  • 1
    I am trying to use the $BUILD_LOG inside the emailext plugin but it still doesnt work. Here is the gist -https://gist.github.com/vijayakumar-psg587/4e37efe4daf0cd8dbaf153da94e01fea – vijayakumarpsg587 Nov 10 '18 at 12:04
  • I end up with the following error - groovy.lang.MissingPropertyException: No such property: BUILD_LOG for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) – vijayakumarpsg587 Nov 10 '18 at 12:07
  • @Joey587, have you found any solution for this? I found just a workaround I've posted below in separate answer. – Vladimir Feb 26 '19 at 22:41
  • No @Vladimir. I will take a look at it. Thank you – vijayakumarpsg587 Feb 27 '19 at 07:55