需要执行jar文件的命令

需要执行jar文件的命令

本文介绍了Jenkins groovy pipeline - 需要执行jar文件的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jenkins v:1.647和Pipeline插件v:1.14。我的管道工作引入了一个运行我的编排的groovy脚本。我的问题是我有一个可执行的jar,将执行一些Scalr API操作并返回一个新的服务器主机名,通过标准输出。我有一个工作片段,在Jenkins之外工作。

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

这段代码在Jenkins之外很好运行,但是我的问题是运行我的管道时收到了令人讨厌的RejectedAccessException。但与其他脚本安全例外不同,我无法批准所述方法。



我正在寻找任何可以超越脚本安全性的解决方案,并允许我运行该jar并在稍后的脚本中使用该标准。



谢谢

Jenkins错误:

  org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:未分类的方法java.lang.UNIXProcess getText 
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:74)
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:15)
at WorkflowScript.run(WorkflowScript:97)
at ___ cps.transform ___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock $ ContinuationImpl.dispatchOrArg(Fu nctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock $ ContinuationImpl.fixName(FunctionCallBlock.java:74)
at sun.reflect.GeneratedMethodAccessor291.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.ConstantBlock.eval(ConstantBlock.java:21)
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:19)
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.scripts ecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:106)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
at org.jenkinsci。 plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:277)
at org.jenkinsci。 plugins.workflow.cps.CpsThreadGroup.access $ 000(CpsThreadGroup.java:77)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:186)
at org。 jenkinsci.plugins.workflow.cps.CpsThreadGroup $ 2.call(CpsThreadGroup.java:184)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService $ 2.call(CpsVmExecutorService.java:47)
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)
在java.util.concurren t.Executors $ RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1145)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)$ b $在java.lang.Thread.run(Thread.java:745)

完成:FAILURE

解决方案您可以使用 sh 步骤从管道脚本执行shell命令。诀窍是将执行的命令的输出重定向到一个文件,然后在下一步中使用 readFile 读取它。


$ b

  shjava -jar scalr-api.jar testing654 n1-standard- 8> scalr.out
def out = readFile'scalr.out'

开windows的slave:

  batjava -jar scalr-api.jar testing654 n1-standard-8> scalr.out
def out = readFile'scalr.out'


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

Thanks

Jenkins error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method java.lang.UNIXProcess getText
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:74)
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:15)
at WorkflowScript.run(WorkflowScript:97)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:74)
at sun.reflect.GeneratedMethodAccessor291.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.ConstantBlock.eval(ConstantBlock.java:21)
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:19)
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:106)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:277)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:77)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:184)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
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

解决方案

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'

这篇关于Jenkins groovy pipeline - 需要执行jar文件的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:12