问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/51353077/jenkins-pipeline-view-logs-of-child-build-job