Jenkins Pipeline:查看子代作业的日志 [英] 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:
build
返回类型为RunWrapper
的对象,您可以使用该对象通过getRawBuild()
访问Run
对象.不幸的是Run
类不能序列化,因此您需要将调用封装在@NonCPS
方法中.
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.
重要的是,getRawBuild()
在沙箱中无法使用.为了使用它,您必须禁用Groovy Sandbox或在某些全局共享库中编写包装.
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.
另请参阅:
http://javadoc.jenkins.io/hudson/model/Run.html
这篇关于Jenkins Pipeline:查看子代作业的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!