Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call functions in one Jenkins Shared Library from another

I have two separate libraries (Library A and Library B), I have defined them on the jenkins configuration so they can be both called from the pipeline.

From Library A I would like to call some functions/methods that are defined in Library B.

My logic tells me that I need to import and probably create an instance of Library B inside Library A before I can have access to any of Library B methods. But I have not been successful. I am no expert in Java, however any guidance is greatly appreciated.

like image 906
ximbal Avatar asked Jan 17 '18 19:01

ximbal


People also ask

How does Jenkins shared library work?

What is a Shared Library in Jenkins? A shared library is a collection of independent Groovy scripts which you pull into your Jenkinsfile at runtime. The best part is, the Library can be stored, like everything else, in a Git repository. This means you can version, tag, and do all the cool stuff you're used to with Git.

What is @NonCPS?

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).


1 Answers

This depends on where the Libraries are are stored.

If they are under ./vars/ then you do not need to import them, as the Jenkinsfile has already imported all scripts via the @Library('sharedLibs') _. You would just call it, def a = LibraryB.functionA()

If it is under a traditional ./src/com/something/whatevas, then you would do a more traditional import com.something.whateva.Library at the top of the Library A

like image 141
metalisticpain Avatar answered Sep 18 '22 20:09

metalisticpain