Stephen Nichols Stephen Nichols - 1 year ago 149
Groovy Question

Jenkins groovy pipeline - Need sout of command from executing jar file

I am using Jenkins v:1.647 and the Pipeline plugin v: 1.14. My pipeline job pulls a groovy script which runs my orchestration. My issue is I have a executable jar that will perform some Scalr API operations and return a new servers hostname, passed in standard out. I have a working snippet that works outside of Jenkins.

def serverHostName = "java -jar scalr-api.jar testing654 n1-standard-8".execute().text

This code works fine outside of Jenkins but my issue is when running my pipeline I receive the ever annoying RejectedAccessException.But unlike the other script security exceptions there is not option for me to approve the method in question.

Im looking any solution that can get past the script security and allow me to run that jar and get the standard out to use later in my script


Jenkins error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method java.lang.UNIXProcess getText
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(
at org.kohsuke.groovy.sandbox.impl.Checker$
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(
at sun.reflect.GeneratedMethodAccessor291.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(
at com.cloudbees.groovy.cps.Next.step(
at com.cloudbees.groovy.cps.Continuable.run0(
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$
at hudson.remoting.SingleLaneExecutorService$
at jenkins.util.ContextResettingExecutorService$
at java.util.concurrent.Executors$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$

Finished: FAILURE

Answer Source

You can execute a shell command from the pipeline script using sh step. The trick is to redirect the executed command's output to a file and then read it with readFile in the next step.

This should do what you want on linux slave:

sh "java -jar scalr-api.jar testing654 n1-standard-8 > scalr.out"
def out = readFile 'scalr.out'

On windows slave:

bat "java -jar scalr-api.jar testing654 n1-standard-8 > scalr.out"
def out = readFile 'scalr.out'