Could anyone advice on how to load external groovy class into the Jenkinsfile ? In general, I would like to build instance by passing parameters via constructor. Sample code below.
Jenkinsfile
stage('Demo stage') {
//missing part
}
Tools.groovy
public class Demo {
String message;
Demo(String message) {
this.message=message;
}
public void print(def script) {
script.sh "echo " + message
}
}
It requires a helper function to do so.
public class Demo {
String message;
Demo(String message) {
this.message = message;
}
public void print(def script) {
script.sh "echo " + message
}
}
Demo createDemo(String message) {
new Demo(message)
}
return this
stage('Demo stage') {
steps {
script {
Object lib = load 'path/to/Tools.groovy'
Object demo = lib.createDemo('a demo')
demo.print(this)
}
}
}
mytools = load 'Tools.groovy'
public class Demo {
}
return new Demo() ;
In your Tools.groovy
you have to have a return statement... Since you are looking to call functions inside a class , you have to return new Demo() at the end ,this would return reference of the object to mytools.
In addition to this you can always use groovy class loader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With