I am trying to develop shared libraries and has the following directory structure
- src/com/mycomapny
- MyTest.groovy
- vars
- test.groovy
- Jenkinsfile
my Jenkinsfile calls only method available in the test.groovy with required input. which imports MyTest and creates object and calls constructor and followed by the actual method that executes the functionality available in the MyTest.groovy file
Here the constructor class never called from global vars/test.groovy
I tried calling class and method from groovy it is working fine but when i am calling it from jenkinsfile it is not working. I don't know why i am not able to call constructor and what i am missing.
I have placed @NonCPS on above test.groovy method but still it is not working.
//MyTest.groovy
package com.mycompany
class MyTest implements Serializable {
public _notification
MyTest(Closure content) {
notification = [:]
content.resolveStrategy = Closure.DELEGATE_FIRST
content.delegate = notification
content()
println("Entered Constructor")
this._notification = notification
}
}
//test.groovy
import com.mycopany.MyTest
def notify(Closure content) {
println("This is line i am getting in the output but nothing after that")
MyTest tester = new MyTest(content)
println(tester._notification.colorcode)
}
//Jenkinsfile
@library("MysharedLibrary@master") _
pipeline{
stages {
stage {
steps {
test.notify {
buildno = 12
jobname = "Mytest"
colorcode = "Blue"
}
}
}
}
}
here i just want to call constructor from jenkins file and print the value of input value in my vars/test.groovy.
EDIT1: When I use @NonCPS above the method "def nofity" my build is getting successful but i am getting nothing except print statement in the first line in the notify method.
If I don't use @NonCPS I am getting following Exception
Error when executing always post condition:
com.cloudbees.groovy.cps.impl.CpsCallableInvocation
hudson.remoting.ProxyException:com.cloudbees.groovy.cps.impl.CpsCallableInvocation
Finished: FAILURE