Kevin Lannen
Kevin Lannen

Reputation: 89

Using a shared library class from a custom step with Jenkins pipeline shared libraries

I am setting up a shared library for Jenkins pipelines and am trying to figure out how to import a class in the shared library into a custom step that I am writing.

Here's what the directory structure looks like:

src
--jenny
----util
------Versioning.groovy
vars
--calculateVersion.groovy

The Versioning.groovy file defines some static helper methods that do some stuff.

package jenny.util
class Versioner implements Serializable {
    static bool checkForValidVersion(version) {
        return true
    }
}

I would like to call this method from the calculateVersion.groovy something like this:

def call(version) {
    return jenny.util.Versioner.checkForValidVersion(version)
}

So that my declarative pipeline can call: def valid = calculateVersion "1.0.0"

But I receive this error No such property: jenny for class: calculateReleaseVersions

Is it possible to reference the classes in the shared library from files in the vars to define custom steps and how is this done?

Upvotes: 1

Views: 3012

Answers (1)

Joerg S
Joerg S

Reputation: 5129

Yes it is possible. At least for us:

Just like in plain java (or groovy) we put an import statement into the groovy script in vars. In your case that would be something like:

import jenny.util.Versioner

def call(version) {
    return Versioner.checkForValidVersion(version)
}

Another thing I just found: It looks like the file name of the class Versioner doesn't match the class name: Versioning.groovy. Could that be the issue?

If that doesn't work you propably want to upgrade your pipeline plugin version(s).

Upvotes: 3

Related Questions