简体   繁体   中英

Jenkins Pipeline: view logs of child build job

I have a jenkins pipeline that is running a "job" as one of its stages. I am wondering, is there a way to see the logs of the build job in a pipeline, without clicking into the job and viewing the console output. It would just make it a lot easier to see the failures without some many clicks.

Yes there's a way how to do this, unfortuntaly it looks like it's not documented:

The build returns an object of type RunWrapper which you can use to access the Run object via getRawBuild() . Unfortunately the Run class is not serializable so you need to encapsulate the call in a @NonCPS method.

Important to mention is that the getRawBuild() will not work from within the sandbox . In order to use it you either have to disable the Groovy Sandbox or write a wrapper in some global shared library.

import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper

@NonCPS
String getLogFromRunWrapper(RunWrapper runWrapper, int logLines) {
    runWrapper.getRawBuild().getLog(logLines).join('\n')
}

RunWrapper buildInfo = build job: 'TestJob'
echo "Log of test job:"
echo getLogFromRunWrapper(buildInfo, 2000)

You need to adjust the number of log lines to retrieve to your needs. And of course it will only work properly if you decided to wait until the child job finished.

See also:

http://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html

http://javadoc.jenkins.io/hudson/model/Run.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM