Introduction:
Discover the efficiency of Jenkins Shared Libraries as we delve into creating a powerful shared library for seamless pipeline integration. Uncover the simplicity of organizing repetitive pipeline code, saving time, and ensuring consistency across multiple applications.
In the world of Jenkins, a pipeline is the smallest unit every Jenkins user interacts with.
There are two ways of writing a Jenkinsfile – One is called Scripted Pipeline, where syntax for a pipeline is strictly Groovy. Another is a relatively new way of writing Jenkinsfile called Declarative Pipeline.
Jenkins Shared Library
Most of the applications have the same steps to build, test & deploy the application. So instead of repeating those steps for every application, we can create a shared library, which can be used across applications.
Let’s say we have five different Spring Boot applications in our Microservice architecture. All of them need to be built using Maven, run Unit & Integration tests to ensure the integrity of code, packed as a JAR/WAR file & pushed to the satisfactory.
Typically, all five applications need their own Jenkinsfile, but the content of the Jenkinsfiles is going to be mostly the same. Jenkins Shared Library avoids this repetition of pipeline code by creating a shared library.
Steps to create & use the Jenkins shared library:
- Create a separate git repo for the Jenkins pipeline library & push the shared library code to that repo.
- Integrate the shared library repo in Jenkins under the Manage Jenkins section.
- Create Jenkinsfile in the project. In that Jenkinsfile, Import & use the shared library.
Creating Jenkins Shared Library
Jenkins Shared Libraries are usually pushed into a separate Git repository & checked out by Jenkins job during the job run.
Steps to create Jenkins shared library:
Step 1: Create a vars folder
- Create a separate project directory. Inside the project directory, create a directory called vars, which will host the shared library’s source code (usually in files with extension. groovy)
Step 2: Create a Groovy file.
- Create a file called onlineBookStore.groovy inside the vars folder.
- The filename onlineBookStore will be used later by Jenkinsfile to access this Jenkins pipeline library.
vars | --- onlineBookStore.groovy
Step 3: Create a call () function inside the Groovy file.
When a shared library is referred from the Jenkins job, Jenkins, by default, will invoke the call () function within our Groovy file.
def call(repoUrl,dockerImageName){ pipeline { agent any tools { maven 'Maven 3.9.0' } environment { DOCKERHUB_CREDENTIALS = credentials('dockerhubID') } stages { stage("git_checkout") { steps { git branch: 'J2EE', credentialsId: 'githubloginID', url:"${repoUrl}" echo "repo cloned successfully." } } stage("Build") { steps { sh 'mvn clean package' } post{ success{ echo "Archiving the artifacts" archiveArtifacts artifacts: '**/target/*.war' } } } stage("Build Docker Image") { steps { script { sh "docker build -t ${dockerImageName}:${BUILD_NUMBER} ." } } } stage("Push image to Dockerhub") { steps { script { sh "echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin" sh "docker push ${dockerImageName}:${BUILD_NUMBER}" } } } } } }
In the above example, the call is the name of the function, repoUrl, and dockerImageName are the arguments.
Note: Call () function can take ‘n’ number of arguments.
Now commit & push the contents of the vars folder to a remote git repository.
Configure Shared Library in Jenkins
Steps to configure Jenkins pipeline library:
Step 1: Open Jenkins in your browser. (if local)
http://localhost:8080/
Step 2: Click on manage Jenkins on the left side menu.
Step 3: Click on configure system. Scroll down until you find the Global Pipeline Libraries section.
Step 4: Under the Library section, configure values as below.
- Name (remember, we will refer to this shared library from Jenkinsfile using this name).
- Default version (branch name of our Shared Library git repo).
- Under the Retrieval method, choose Modern SCM.
- Under Source Code Management, choose Git.
- Enter your Pipeline Shared Libraries repo URL under Project Repository.
- Configure credentials if your repo requires credentials to checkout code.
- First, we need to import the shared library to use the functions defined in that library. we can do that using ‘@Library (‘ Library_Name ‘).
- The online bookstore is the name of the groovy file inside the vars folder of the shared library.
"https://github.com/bprasad701/onlinebookstore.git" and "bprasad701/cz-onlinebookstore"
Now we can run our Pipeline job.
Conclusion:
The Jenkins Shared Libraries are a huge help when it comes to streamlining your CI/CD processes. You may reduce redundancy, improve maintainability, and promote a standardized method for developing, testing and deploying applications by centralizing common pipeline phases. Create and set up your Jenkins Shared Library by following these steps, then add reusable Groovy methods to your Jenkinsfile. Embrace the potential of shared libraries for effective and reliable pipeline management to transform your Jenkins experience. Happy Jenkins Coding!