How to load external class in Groovy Postbuild action?

Article ID:218576338
1 minute readKnowledge base

Issue

I would like to load an external Groovy class (or classes) to use in my Groovy Postbuild action.

Environment

  • CloudBees Jenkins Enterprise

  • Groovy Postbuild Plugin

Resolution

Below is a simplified example of how to do this.

Create file /tmp/JenkinsPostBuild.groovy with contents:

public class JenkinsPostBuild {

  def manager

  public JenkinsPostBuild(manager){
    this.manager = manager
  }

  def run() {
    manager.listener.logger.println("I want to see this line in my job's output");
  }
}

In the job Post-build Actions - Groovy Postbuild:

Groovy Script::

import JenkinsPostBuild
def postBuild = new JenkinsPostBuild(manager)
postBuild.run()

Additional classpath:

JAR file path or URL: /tmp

That’s it!

Or if you wanted to call static methods:

Contents:

...
  static void runStatic(manager) {
    manager.listener.logger.println("I want to see this line in my job's output");
  }
...

Groovy Script:

import JenkinsPostBuild

JenkinsPostBuild.runStatic(manager)

See Groovy Postbuild Plugin for more.