如何通过詹金斯的另一个管道作业来构建和获取管道作业的构建日志 [英] How to build and get build log of pipeline job by another pipeline job in jenkins

查看:80
本文介绍了如何通过詹金斯的另一个管道作业来构建和获取管道作业的构建日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jenkins管道.我可以通过以下命令构建并获取作业的构建日志:

I'm using Jenkins pipeline. I can build and get build log of the job by command:

def itemA = hudson.model.Hudson.instance.getItem(FOLDER).getJob(JOB_NAME_A)
hudson.model.Hudson.instance.queue.schedule(itemA)
def buildObj = itemA.getLastBuild()
def log = buildObj.log

没关系.但是,如果JOB_NAME_A是管道作业,则会出现错误:

It's OK. But if JOB_NAME_A is a pipeline job, I get an error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
unclassified method hudson.model.Queue schedule
org.jenkinsci.plugins.workflow.job.WorkflowJob    at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:113)
  at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149)
  at
org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
  at
com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16)
  at WorkflowScript.run(WorkflowScript:9)     at
___cps.transform___(Native Method)    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
  at
com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
  at
com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
  at sun.reflect.GeneratedMethodAccessor424.invoke(Unknown Source)    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)     at
com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  at
com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
  at
com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
  at
com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
  at
com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
  at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)   at
com.cloudbees.groovy.cps.Next.step(Next.java:58)  at
com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)   at
org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
  at
org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
  at
org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
  at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
  at
org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
  at
org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
  at
org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324)
  at
org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78)
  at
org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
  at
org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224)
  at
org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)     at
hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
  at
jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
  at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)     at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:745) Finished: FAILURE

因此任何人都遇到过这种情况,请帮助我解决此错误.预先感谢.

So anyone had experienced in this case please help me fix this error. Thanks in advance.

推荐答案

尝试构建作业" 步骤,该语法在管道语法中可用. 就像这样:

Try the 'build job' step, available in pipeline syntax. It would be something like:

build job: 'JOB_NAME_A'. OR build 'JOB_NAME_A'

如果您有一些参数,也可以在此处传递它们,还可以选择传递错误或等待作业完成的选项. (通过单击脚本区域下的管道语法链接查看管道语法,以获取全部详细信息.)

If you have some parameters you can pass them here as well and options to propagate errors or wait till completion of job. (See the pipeline syntax by clicking the pipeline syntax link under script area for full details.)

要获取日志,您可以尝试以下操作:

For getting the log you can try something like:

some_var = build job: 'JOB_NAME_A'

log = Jenkins.getInstance().getItemByFullName(JOB_NAME_A).getBuildByNumber(some_var.getNumber()).logFile.text

some_var存储您开始的作业的信息. 这可能需要您的Jenkins管理员批准脚本,并且必须有一些更干净的方法才能实现,但是我不确定. 尚未尝试对文件夹内的作业进行尝试,因此您可能需要对其进行一些调整.

some_var stores information of the job you started. This might require script approval from your Jenkins admin and there must be some cleaner way to get this but I'm not sure about that. Haven't tried it with jobs inside folders so you might need to tweak it a bit.

这篇关于如何通过詹金斯的另一个管道作业来构建和获取管道作业的构建日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆