Adding shared libraries to Jenkins
Shared code can be added to Jenkins by importing a github repository that matches the following directory structures identified here: https://jenkins.io/doc/book/pipeline/shared-libraries/. In the /vars directory you can add a groovy file of the name foo.groovy that can be used as a method in the Jenkinsfile. The groovy file must implement the call method. In order to make use of the method you need to go to Manage Jenkins > Configure System and under the Global Pipeline Libraries section, you can add a library that references your github repo.
- The name of the library is what you use to load the library.
- Default version specifies which branch or version to load. (This can be overridden)
Select the GitHub radio button under Source Code Management and fill in the owner and repository information.
To load the library into the Jenkinsfile add this to the top of the file:
@Library('[Name]') _
The underscore is necessary if the following line is not an import statement.