Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Job DSL's readFileFromWorkspace from a helper class?

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.

like image 685
Dániel Szoboszlay Avatar asked Oct 27 '25 19:10

Dániel Szoboszlay


1 Answers

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.

like image 151
Surendra Deshpande Avatar answered Oct 30 '25 15:10

Surendra Deshpande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!