我正在使用 Jenkins 2 编译 Java 项目,我想从 pom.xml 中读取版本,我正在遵循以下示例:
https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md
示例建议:
访问文件系统似乎存在一些安全问题,但我无法弄清楚它给出了什么(或为什么)该问题:
我只是做了一些与示例不同的事情:
def version() {
String path = pwd();
def matcher = readFile("${path}/pom.xml") =~ '<version>(.+)</version>'
return matcher ? matcher[0][1] : null
}
运行“版本”方法时遇到的错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.codehaus.groovy.runtime.GStringImpl call org.codehaus.groovy.runtime.GStringImpl)
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:165)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:117)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:103)
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:71)
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.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:100)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source)
我正在使用这些版本: 插件管道2.1 Jenkins 2.2
最佳答案
快速修复解决方案:
我遇到了类似的问题,我通过以下方式解决了该问题
- 导航到 jenkins > 管理 jenkins > 进程内脚本批准
- 有一个待处理的命令,我必须批准该命令。
替代方案 1:禁用沙箱
如这个article深入解释一下,groovy 脚本默认在沙箱模式下运行。这意味着 Groovy 方法的子集无需管理员批准即可运行。也可以不在沙箱模式下运行脚本,这意味着整个脚本需要立即得到管理员的批准。这会阻止用户当时批准每一行。
在没有沙箱的情况下运行脚本可以通过取消选中脚本下方的项目配置中的此复选框来完成:
替代方案 2:禁用脚本安全性
如这个article解释了也可以完全禁用脚本安全性。首先安装permissive script security plugin然后更改您的 jenkins.xml 文件添加以下参数:
-Dpermissive-script-security.enabled=true
所以你的 jenkins.xml 看起来像这样:
<executable>..bin\java</executable>
<arguments>-Dpermissive-script-security.enabled=true -Xrs -Xmx4096m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=80 --webroot="%BASE%\war"</arguments>
如果您实现此操作,请确保您知道自己在做什么!
关于Jenkins CI 管道脚本不允许使用方法 groovy.lang.GroovyObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276341/