using classes in jenkins job dsl

3.8k views Asked by At

Hi all I have a problem and I can't seem to figure it out.

So I'm creating some helper classes for my dsl to use, but it just does not seem to execute any method within these classes.

I have created a job with the following dsl in it:

class TestIt {
  def static helloStatic() {
    println "[STATIC] - Hello"
  }

  def hello() {
    println "[NORMAL] - Hello"
  }
}

def runIt() {
  println "Starting test"
  println "-------------"
  TestIt _test = new TestIt()
  _test.hello();
  TestIt.helloStatic();
  println "-------------"
  println "Done"
}

runIt();
TestIt.helloStatic();

When I run this job with jenkins it does not display/execute either the static or normal method. Do I need to somehow inject the class in the current running context or do something else?

Also note that if I run this exact same script from the command line, using a github version of the job dsl plugin and gradle, then the script does call the methods.

1

There are 1 answers

2
daspilker On BEST ANSWER

When using println in scripts (in your example, in the runIt function), Groovy sends the call to a out variable defined in the script's binding or to System.out.println if the variable is not set. The Job DSL plugin sets this variable so that the output goes to the build log.

When using println in classes (in your example, the TestIt class), System.out.println will be called. So the output is send to stdout. And depending on how you started Jenkins, stdout is e.g. logged the console or /var/log/jenkins/jenkins.log.

To send the output from classes to the build log, you need to pass the out variable to your class:

class TestIt {
  def out

  def static helloStatic(def out) {
    out.println "[STATIC] - Hello"
  }

  def hello() {
    out.println "[NORMAL] - Hello"
  }
}

def runIt() {
  println "Starting test"
  println "-------------"
  TestIt _test = new TestIt(out: out)
  _test.hello();
  TestIt.helloStatic(out);
  println "-------------"
  println "Done"
}

runIt();
TestIt.helloStatic(out);