Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access $BUILD_LOG in Jenkins pipeline

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?

like image 934
jpyams Avatar asked Jan 03 '18 16:01

jpyams


People also ask

How do I access Jenkins pipeline?

On this page, click Build Now on the left to run the Pipeline. Under Build History on the left, click #1 to access the details for this particular Pipeline run. Click Console Output to see the full output from the Pipeline run. The following output shows a successful run of your Pipeline.

How do I add a time stamp in Jenkins pipeline?

In order to use the timestamp information from Jenkins, you need to install the Build Timestamp Plugin and set the timestamp format in the Jenkins configuration (for example, to "yyyyMMdd-HHmm"). In every place where we use the Docker image, we need to add the tag suffix: ${BUILD_TIMESTAMP} .

How do I enable pipeline view in Jenkins?

Step 4) Go to your Jenkins dashboard and create a view by clicking on the “+” button. Select the Build Pipeline View option and click OK. Step 5) Under Pipeline view configuration, locate Pipeline Flow. Under Pipeline flow, select the initial job to run.


3 Answers

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.

like image 74
anttikoo Avatar answered Sep 20 '22 08:09

anttikoo


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.

like image 35
Vladimir Avatar answered Sep 19 '22 08:09

Vladimir


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.

like image 38
CarlMc Avatar answered Sep 19 '22 08:09

CarlMc