Hoàng Nguyễn
Hoàng Nguyễn

Reputation: 71

How to build and get build log of pipeline job by another pipeline job in 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

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.

Upvotes: 3

Views: 8129

Answers (1)

zalimgandhera
zalimgandhera

Reputation: 757

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 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.

Upvotes: 7

Related Questions