Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multiple jenkins shared libraries

Tags:

jenkins

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?

like image 709
u123 Avatar asked Sep 24 '18 11:09

u123


Video Answer


1 Answers

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.

like image 152
Mihai Avatar answered Oct 17 '22 14:10

Mihai