【问题标题】:jenkins shared library error com.cloudbees.groovy.cps.impl.CpsCallableInvocationjenkins 共享库错误 com.cloudbees.groovy.cps.impl.CpsCallableInvocation
【发布时间】:2018-07-14 15:29:52
【问题描述】:

我通过 jenkins 管道(共享库)运行此代码。

enum Components {
  service('name_api')

  Components(String componentName) {
    this.componentName = componentName
  }

  private String componentName

  String getComponentName() {
    return componentName
  }

  static boolean isValid(String name) {
    for (Components component : values()) {
      if (component.getComponentName().equalsIgnoreCase(name)) {
        return true
      }
    }
    println("The name of component is incorrect")
  }
}

它在本地工作,但在 Jenkins 管道中,我收到此错误:

hudson.remoting.ProxyException:         
com.cloudbees.groovy.cps.impl.CpsCallableInvocation

请帮帮我

【问题讨论】:

标签: jenkins groovy enums shared-libraries jenkins-pipeline


【解决方案1】:

Jenkins 中的 groovy 解释器出了点问题。我正在尝试编写一个库并遇到同样的错误。

我做了一个流水线脚本的例子。我写了不同的类来避免引发错误:

class Test1 {
    private t1
    private wfs

    Test1(Test2 t2, wfs) {
        this.wfs = wfs
        wfs.echo 'TEST1 constructor'
        this.t1 = t2.getT2() }

    def getT1() {
        wfs.echo 'getT1() function'
        def result = t1.toString()
        return result }
}

class Test2 {
    private t2
    private wfs

    Test2(wfs) {
        this.wfs = wfs
        wfs.echo 'TEST2 constructor'
        this.t2 = "hello" }

    def getT2() {
        wfs.echo 'getT2() function'
        def result = t2.toString()
        return result }
}

echo 'Creating Test2 object'
Test2 test2 = new Test2(this)
echo "Test2 object was created successfully. test2.t2="+test2.getT2()
echo 'Creating Test1 object'
Test1 test1 = new Test1(test2,this)
echo "Test1 object was created successfully. test1.t1="+test1.getT1()

这个脚本的输出是:

Started by user admin
[Pipeline] echo
Creating Test2 object
[Pipeline] echo
TEST2 constructor
[Pipeline] echo
getT2() function
[Pipeline] echo
Test2 object was created successfully. test2.t2=hello
[Pipeline] echo
Creating Test1 object
[Pipeline] echo
TEST1 constructor
[Pipeline] End of Pipeline
com.cloudbees.groovy.cps.impl.CpsCallableInvocation
Finished: FAILURE

问题在于这个字符串this.t1 = t2.getT2()。原来t2.getT2()函数不能在构造函数中完成:(

第二个 - 如果你尝试这样返回:

def getT1() {
    wfs.echo 'getT1()' 
    return t1.toString() 
}

它会失败...

【讨论】:

【解决方案2】:

由于这是密切相关并在谷歌顶部弹出,我将提供一些额外的信息给com.cloudbees.groovy.cps.impl.CpsCallableInvocation

当我使用以下构造函数时,我遇到了这个问题:(在 EclipseIDE 中本地没有错误,但 jenkins 抱怨这个无用的错误消息没有提及任何代码行)

class blubb{
  blubb(Name){      
      super(Name) // must be first in CONSTRUCTOR
      // no return from super! , nevertheless, last throws...
      println("This will never be printed inside of jenkins!") 
      someBaseClassFunction() // this line is not allowed but errors!
  }
}

这就是@wunt 小而超级有用的评论发挥作用的地方。

【讨论】:

    【解决方案3】:

    当我遇到这个错误时,我从groovy-cps library by Cloudbees@NonCPS 注释失败的方法并解决了!

    【讨论】:

      【解决方案4】:

      我有同样的问题。在我的情况下,它是由对字段的构造函数参数的方法调用引起的,其中方法调用依赖于仅在构造函数中初始化的字段。像这样的:

      class A {
          final def b = new B(method())
      
          final def param
      
          A(param) {
              this.param = param
          }
      
          def method() {
              return param.foo()
          }
      }
      

      我将初始化移到构造函数中并内联方法,问题就消失了:

      class A {
          final def b
      
          final def param
      
          A(param) {
              this.param = param
              this.b = new B(param.foo())
          }
      }
      

      【讨论】:

      • 在您的原始类中,您正在调用一个实例来初始化一个在构造函数有机会运行之前执行的字段,因此param.foo() 在这种情况下总是会导致 NPE。
      猜你喜欢
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多