Jenkins Shared library

Subject: Need help loading specific functions from shared library in Jenkins pipeline

Dear @jenkins_community

I am currently working on a Jenkins pipeline and using a shared library that contains multiple functions. However, I only need to load and use a specific set of functions in my pipeline, not all of them.

Here is how I am currently loading the shared library in my Jenkinsfile:

library identifier: 'my-lib@param-sharedlibrary', retriever: modernSCM(
    [$class: 'GitSCMSource', remote: "URL", credentialsId: 'gitlab']),

Despite specifying the functions I need in the includes list, all functions from the shared library are still being loaded into my Jenkins pipeline, causing unnecessary clutter in the output.

Could someone please advise on the correct approach to loading only the specified functions from a shared library into a Jenkins pipeline? Any help or guidance would be greatly appreciated.

Thank you.

Sincerely,
Param Humbal

We use shared libraries and how things are loaded are indeed … unclear.

You are not sharing the structure of your library. One mistake we did in ours was to put all methods into a single vars/mylib.groovy file. In our pipelines we would then call mylib.myfunction(), etc…

Since then we have started work to slowly decentralize the methods and move them to their own groovy file.

We also moved more business logic inside the src/ folder but be aware that the code under src/ does not have access to global objects directly (for example the currentBuild object, or the env object, IIRC), but they can be passed around as arguments from the code under vars/.

An other thing we did is that we have different Jenkins instances for different purposes: CI vs CD, prod vs dev. Since some of these shared methods are only for CI we now have multiple libraries and we configured our jenkins instances to pull the libraries that make sense to them. This way CD pipelines do not pull the library for CI only and vice versa.

Finally, you might want to ensure that your vars/*groovy scripts do not have more business logic executed at import time than necessary. This means that your business logic should always be in methods with the def call(): method as the entry point, any other statement at the root level of the file will get executed when imported.

Not everybody reuses their groovy scripts. They are checked in with the source anyway. I don’t see the need to change it…

That being said this video seems to be what you are talking about: https://www.youtube.com/watch?v=Wj-weFEsTb0&t=1s

It does work BTW…