jenkins管道脚本close()被调用而不执行闭包

时间:2019-04-16 18:39:05

标签: jenkins jenkins-pipeline jenkins-groovy

以下Jenkins管道脚本不执行Closure中的代码。

class MyClass implements Closeable, Serializable {
    private final script

    MyClass(final script) {
        this.script = script
        this.script.echo "In MyClass()"
    }

    @Override
    public void close() throws Exception {
        this.script.echo "In MyClass.close()"
    }
}

def sdk = new MyClass(this).withCloseable {
    println "In Closure ..."
}

println "Done"
This is the output:
[Pipeline] Start of Pipeline
[Pipeline] echo
In MyClass()
[Pipeline] echo
In MyClass.close()
[Pipeline] echo
Done
[Pipeline] End of Pipeline
Finished: SUCCESS

请注意,没有“ InClosure ...”字符串。 我必须批准签名:方法groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object才能运行上述脚本。

1 个答案:

答案 0 :(得分:0)

简短版本:

我相信您的Jenkins安装随附的groovy版本不支持.withCloseable。具体来说,请注意,许多Jenkins安装仅具有groovy版本1.8.9。我认为Groovy直到版本2才添加自动withCloseable东西。

更长:

您不应该批准签名groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object。去批准它,因为它对您没有任何好处,可能会隐藏其他错误/使他们更难追踪,并且如果您有非管理员开发人员向您的Jenkins实例提交作业,这是一个安全漏洞。

groovy与Jenkins的交互有一个令人烦恼的UI失败:如果您尝试在不具有该方法的对象上调用某个方法,并且该对象的类是用Groovy编写的,则将得到一个“ Scripts not允许使用方法”错误,引用groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object。不是NoSuchMethodException,不是有关错误的方法名称的有用信息,而是安全错误。

如果收到错误消息具体指groovy.lang.GroovyObject invokeMethod ,通常表示“您已调用了不存在的方法”。在这种情况下,我认为这是因为在您的Jenkins实例上安装的groovy太旧了,并且不会自动将withCloseable添加到实现Closeable的类中。