Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume shared/common groovy methods in context of Jenkins Job DSL Plugin

Tags:

jenkins

groovy

using GroovyConsole I have file main.groovy with:

new Helpers().test("test method called")

and in the same dir have file Helpers.groovy with content

def test(String str) {
    println "test method called with: " + str
}

Running results in results:

groovy> new Helpers().test("test method called") 

test method called with: test method called

However, in the context of Jenkins using DSL, I have similar code, in file generator.groovy:

new Helpers().test("test method called")

then in Helpers.groovy in same dir I have:

def test(String str) {
    println("test method called on: " + str)
}

However when I run I do not get any output (from the println) in the logs. If I have my def's in the same main.groovy file instead, it works fine.

Probably missing something fundamental. It's compiling / green in jenkins so not sure how to adapt this, so the runtime will do what I want.

like image 451
JohnZaj Avatar asked Nov 07 '22 18:11

JohnZaj


1 Answers

You need to import class when call method from other files

Make a directory at the same level as the DSL called utilities and create a file called MyUtilities.groovy in the utilities directory with the following contents:

package utilities

class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}

Then from the DSL, add something like this:

import utilities.MyUtilities
like image 58
user7294900 Avatar answered Nov 14 '22 08:11

user7294900