I have a large number of Jenkins job definitions in Job DSL that all rely on some common functionality that I implemented in helper classes. This is the essence of the jobDsl step running these scripts:
jobDsl {
  additionalClasspath('jobdsl/src/main/groovy')
  targets('jobdsl/*.groovy')
  sandbox(true)
}
One of the helper classes in jobdsl/src/main/groovy needs to read a file from the workspace, but it cannot access the readFileFromWorkspace function.
So this one wouldn't work:
class MyHelper {
  static Closure processFile(String src) {
    ...
    def txt = readFileFromWorkspace(src)
    ...
  }
}
I have to take a closure parameter instead:
class MyHelper {
  static Closure processFile(String src, Closure rffw) {
    ...
    def txt = rffw(src)
    ...
  }
}
Which makes the code calling this helper bloated:
MyHelper.processFile('foo.txt', { readFileFromWorkspace(it) })
Is there a way to make my class see readFileFromWorkspace? Actually, I couldn't even figure out to which class does this function belong to. Or whether is it a real function at all or something "magicly" defined by the DSL.
HelperClass is present in other file which is out of Job-dsl context. So to make it visible, try doing as below.
class MyHelper {
   static Closure processFile(String src, def dslFactory) {
   ...
   def txt = dslFactory.readFileFromWorkspace(src)
   ...
   }
}
MyHelper.processFile('foo.txt', this)
The above code should work for you, else please revert to me if you encounter any problems.
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