如何在jenkins

时间:2017-04-17 10:12:12

标签: jenkins jenkins-pipeline

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

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是管道作业,我会收到错误:

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

所以在这种情况下,任何人都有经验,请帮我修复此错误。提前谢谢。

1 个答案:

答案 0 :(得分:4)

尝试使用管道语法中的'构建作业' 步骤。 它会是这样的:

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

如果您有一些参数,您也可以在这里传递它们以及传播错误或等到工作完成的选项。 (有关详细信息,请单击脚本区域下的管道语法链接,以查看管道语法。)

要获取日志,您可以尝试以下内容:

some_var = build job: 'JOB_NAME_A'

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

some_var存储您开始的工作的信息。 这可能需要您的Jenkins管理员的脚本批准,并且必须有一些更清晰的方法来实现这一点,但我不确定。 Haven没有尝试使用文件夹中的作业,因此您可能需要稍微调整一下。