Shared Libraries
General Information
Jenkins shared libraries are re-usable custom steps, variables, functions, and classes.
Checklist
- Jenkins server setup/configured
- Jenkins pipeline job(s) configured
Shared Library Git Project
Create a new git project that will contain the Jenkins shared library.
Directory structure:
- vars/ → Custom steps in <name>.groovy format
- src/ → Other common code (classes, metadata)
Example: https://gitlab.com/whowe/jenkins-shared-library
Jenkins Server: Configure Shared Library
Edit the Jenkins server settings to be able to use the shared library
- Manage Jenkins
- Configure System
- Under “Global Pipeline Libraries”, click “Add”
- Library
- Name: <short-descriptive-name> (will be used in Jenkinsfiles to access shared library)
- The example pipeline below assumes the library name is: “library-example”
- Default version: <branch or tag to use as default> (example: master)
- Retrieval method
- Select “Modern SCM”
- Select “Git” or “GitHub”
- Project Repository: <paste in URL of your git shared library repo, same as clone URL>
- Credentials: <select git credentials>
- Click Save
Jenkins Pipeline: Import Shared Library
Configure a Jenkinsfile pipeline to use the shared library and then access the custom step via its filename (minus extension).
Example:
- Above pipeline accesses the custom step here: https://gitlab.com/whowe/jenkins-shared-library/blob/master/vars/hello.groovy