element
element

Reputation: 111

jenkins shared library error com.cloudbees.groovy.cps.impl.CpsCallableInvocation

I run this code through jenkins pipeline(shared library).

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")
  }
}

It works locally, but in Jenkins pipeline, I get this error:

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

help me please

Upvotes: 8

Views: 9941

Answers (4)

Marv
Marv

Reputation: 3557

I had the same issue. In my case, it was caused by a method call for the parameter of a constructor for a field, where the method call was dependent on a field that was only initialized in the constructor. Something like this:

class A {
    final def b = new B(method())

    final def param

    A(param) {
        this.param = param
    }

    def method() {
        return param.foo()
    }
}

I moved the initialization into the constructor and inlined the method and the issue went away:

class A {
    final def b

    final def param

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

Upvotes: 0

Arcones
Arcones

Reputation: 4662

When I face this error, I annotate the method that is failing with @NonCPS from groovy-cps library by Cloudbees and solved!

Upvotes: 0

Cutton Eye
Cutton Eye

Reputation: 3559

As this is closely related and pops up in google on the top, I'm going to provide some additional infos to com.cloudbees.groovy.cps.impl.CpsCallableInvocation

I come over this when I used following constructor: (no errors locally in EclipseIDE, but jenkins complained with this useless error message not mentioning any code line)

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!
  }
}

Here this is the point where @wunt small but super usefull comment comes into play.

Upvotes: 0

wunt
wunt

Reputation: 139

Something is wrong with groovy interpreter in that Jenkins. I'm trying to write a library and have the same error.

I made an example of pipeline script. I wrote different classes to avoid induced errors:

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()

output on this script is:

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

The problem is in this string this.t1 = t2.getT2(). It turns out that t2.getT2() function could not be done inside constructor :(

And 2nd one - if you try return like this:

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

It will fail...

Upvotes: 5

Related Questions