首页 文章

Jenkins CI Pipeline Scripts不允许使用方法groovy.lang.GroovyObject

提问于
浏览
65

我正在使用Jenkins 2来编译Java项目,我想从pom.xml中读取该版本,我遵循这个例子:

https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md

这个例子表明:

Full Jenkins Pipeline with problematic function circled

似乎访问文件系统存在一些安全问题,但我无法弄清楚它给出了什么(或为什么)这个问题:

我只是做了一个与示例有点不同的事情:

def version() {
    String path = pwd();
    def matcher = readFile("${path}/pom.xml") =~ '<version>(.+)</version>'
    return matcher ? matcher[0][1] : null
}

我在运行'version'方法时遇到的错误:

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)

我正在使用这些版本:Plugin Pipeline 2.1 Jenkins 2.2

3 回答

  • 5

    Quickfix

    我有类似的问题,我解决了它做以下事情

    • 导航到jenkins>管理jenkins>进程内脚本批准

    • 有一个挂起的命令,我必须批准 .

    In process approval link in Jenkins 2.61
    Alternative 1: Disable sandbox

    正如article深入解释的那样,默认情况下,groovy脚本以沙盒模式运行 . 这意味着允许在没有管理员批准的情况下运行groovy方法的子集 . 也可以不以沙盒模式运行脚本,这意味着整个脚本需要立即由管理员批准 . 这会阻止用户当时批准每一行 .

    在没有沙箱的情况下运行脚本可以通过在脚本下方的项目配置中取消选中此复选框来完成:
    enter image description here

    Alternative 2: Disable script security

    正如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>
    

    如果你实现了这一点,请确保你知道自己在做什么!

  • 8

    您必须在作业配置中禁用Groovy的沙箱 .

    目前,对于groovy脚本来自scm的multibranch项目,这是不可能的 . 有关更多信息,请参阅https://issues.jenkins-ci.org/browse/JENKINS-28178

  • 176

    为了解决SCM存储的Groovy脚本的沙盒,我建议将脚本运行为 Groovy Command (而不是 Groovy Script file ):

    import hudson.FilePath
    final GROOVY_SCRIPT = "workspace/relative/path/to/the/checked/out/groovy/script.groovy"
    
    evaluate(new FilePath(build.workspace, GROOVY_SCRIPT).read().text)
    

    在这种情况下,groovy脚本从工作区传输到Jenkins Master,在那里它可以作为 system Groovy Script 执行 . 只要未选中 Use Groovy Sandbox ,沙盒就会被抑制 .

相关问题