How to import Groovy class into Jenkin file?

How to import Groovy class into Jenkins file? I tried several approaches but no one worked.

This is the class I want to import:

Thing.groovy

class Thing { void doStuff() { ... } } 

These are the things that do not work:

Jenkinsfile-1

 node { load "./Thing.groovy" def thing = new Thing() } 

Jenkinsfile-2

 import Thing node { def thing = new Thing() } 

Jenkinsfile-3

 node { evaluate(new File("./Thing.groovy")) def thing = new Thing() } 
+5
source share
1 answer

You can return a new instance of the class using the load command and use the object to call "doStuff"

So, you would get it in "Thing.groovy"

 class Thing { def doStuff() { return "HI" } } return new Thing(); 

And you would get this in your dsl script:

 node { def thing = load 'Thing.groovy' echo thing.doStuff() } 

To do this, type β€œHI” at the console output.

Does this fit your requirements?

+2
source

All Articles