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.

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.