10
votes

Is it possible to combine multiple shared Jenkins libraries?

E.g. I have a common shared library: my-shared-library (git repository with a maven project) defined on a jenkins folder that contains some jobs. Each job that runs inside that folder can use that shared library in the Jenkinsfile with:

@Library("my-shared-library") _
import com.pipelines.Pipeline
new Pipeline().build()

Now I would like to create another shared library: my-specialized-shared-library that contains a few specialized pipelines (in another git repository also as a maven project). Pipelines (groovy classes, scripts etc) in my-specialized-shared-library should be able to use/import classes, pipelines etc from: my-shared-library is that possible and if so what are the recommended approach?

1
Could you please elaborate a bit more on that? Also, it would be helpful if you posted the full code. Thank you. - user9621927
The above is the full code. The Jenkinsfile only contains those 3 lines. The purpose is that I would like to "componentize" my shared libraries into separate git repositories/maven projects and then enable one to depend on the other. - u123
In what way and why though? - user9621927

1 Answers

6
votes

In Manage Jenkins > Configure System I defined 2 different Global Pipeline Libraries from different URLs.

  • GlobalLibrary-1
  • GlobalLibrary-2

The .jenkinsfile:

@Library('GlobalLibrary-1@some_branch') l1 //you can write here _ (underscore) or any other string.
@Library('GlobalLibrary-2@any_branch') l2
import com.lib1.Class1;  // import from GlobalLibrary-1 (Jenkins automatically finds com.lib.Class1 inside GlobalLibrary-1)
import com.prodcode.Class2;  // import from GlobalLibrary-2 (Jenkins automatically finds com.prodcode.Class2 inside GlobalLibrary-2)

node {
    new Class1();
    new Class2();
}

I can even use imports inside Class2.groovy to get classes from GlobalLibrary-1.