Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline shared library vs plugin

I am working on Jenkins pipeline for two projects. I built some customized configuration alerts messages via slack and emails. We expect my code can be used for my projects and also several other projects. So I am thinking to make it a small lib so that others don't need to ask me every time they onboard a Jenkins pipeline jobs. I was thinking using shared library with @Library() for other to use, as described in the docs.

However, since my lib depends on the existence of slack and emails plugin, it will not be usable when these plugin are not installed.

My question is: is there are way to declare dependency in pipeline Shared Libraries or I have to make jenkins plugin to address this issue?

like image 610
weifeng zhang Avatar asked Feb 04 '23 18:02

weifeng zhang


1 Answers

As far as I know there is no way to declare dependencies to plugins right now (or version of Jenkins). Instead, what you can do is add a check for the plugin and give a proper error to the user of your library:

if (Jenkins.getInstance().getPluginManager().getPlugin("Slack+Plugin") == null) {
  error "This shared library function requires "Slack plugin!"
}

Put this at the start of your shared library script, before any uses of the plugin. Note though, this gets tricky if you need to import classes from a plugin (since imports goes first in the groovy file). What you do in that situation is to make two scripts, the first script has the check and is the one the user calls, the second contains all the logic and imports, and is called by the first script once the checks pass.

like image 54
Jon S Avatar answered Feb 07 '23 09:02

Jon S