Make use of a shared library to reuse code across multiple jobs.
Blog post: https://rodrigocprates.hashnode.dev/jenkins-shared-libraries
-
In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries, add a library with the following settings:
- Name:
jenkins-shared-library
- Default version:
main
(branch) - Retrieval method: Modern SCM
- Select the Git type
- Project repository:
<git repository url>
- Credentials: select an existing credential
- Name:
-
Then create a Jenkins job with the following pipeline:
@Library('jenkins-shared-library') _ stage('Demo') { common.sayHello("Rodrigo") }
Whenever a job is triggered, it will:
- clone this shared library repo (with most updated code)
- import the functions
- run whatever function you call in your pipeline