0
0
Jenkinsdevops~5 mins

Why shared libraries reduce duplication in Jenkins - Why It Works

Choose your learning style9 modes available
Introduction
When you write Jenkins pipelines, you often repeat the same code in many places. Shared libraries let you write common code once and use it everywhere, saving time and avoiding mistakes.
When multiple Jenkins pipelines need to perform the same steps like building or testing code.
When you want to update a common process in one place instead of changing many pipeline files.
When you want to keep your pipeline scripts clean and easy to read by moving complex logic out.
When your team grows and many people write pipelines that share similar tasks.
When you want to enforce consistent steps across all your Jenkins jobs.
Config File - vars/commonBuild.groovy
vars/commonBuild.groovy
def call(String projectName) {
    echo "Building project: ${projectName}"
    // Add build steps here
    sh 'echo Compiling source code'
    sh 'echo Running tests'
    echo "Build complete for ${projectName}"
}

This file defines a shared library function named commonBuild. It takes a project name and runs common build steps like compiling and testing. Pipelines can call this function to reuse the same build logic.

Commands
Create the directory structure for the shared library inside Jenkins home.
Terminal
mkdir -p ~/.jenkins/shared-libraries/commonBuild/vars
Expected OutputExpected
No output (command runs silently)
Create the shared library Groovy script with common build steps.
Terminal
echo 'def call(String projectName) {\n    echo "Building project: ${projectName}"\n    sh "echo Compiling source code"\n    sh "echo Running tests"\n    echo "Build complete for ${projectName}"\n}' > ~/.jenkins/shared-libraries/commonBuild/vars/commonBuild.groovy
Expected OutputExpected
No output (command runs silently)
Install the Jenkins plugin that supports global shared libraries.
Terminal
jenkins-cli.jar -s http://localhost:8080 install-plugin workflow-cps-global-lib
Expected OutputExpected
Installing workflow-cps-global-lib plugin... Success
Configure Jenkins to use the shared library from a Git repository as a global library.
Terminal
jenkins-cli.jar -s http://localhost:8080 groovy = <<EOF
import jenkins.model.*
import org.jenkinsci.plugins.workflow.libs.*
import jenkins.plugins.git.GitSCMSource
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever

GlobalLibraries libraries = GlobalLibraries.get()
LibraryConfiguration libConfig = new LibraryConfiguration('commonBuild')
libConfig.setDefaultVersion('master')
libConfig.setImplicit(true)
libConfig.setRetriever(new SCMSourceRetriever(new GitSCMSource(null, 'https://github.com/example/commonBuild.git', '', '*', '', false)))
libraries.getLibraries().add(libConfig)
Jenkins.instance.save()
EOF
Expected OutputExpected
No output (command runs silently)
Create a Jenkins pipeline script that calls the shared library function to build the project.
Terminal
echo 'pipeline {\n  agent any\n  stages {\n    stage("Build") {\n      steps {\n        commonBuild("my-app")\n      }\n    }\n  }\n}' > Jenkinsfile
Expected OutputExpected
No output (command runs silently)
Trigger the Jenkins pipeline job that uses the shared library to run the build.
Terminal
jenkins-cli.jar -s http://localhost:8080 build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building project: my-app [Pipeline] sh + echo Compiling source code Compiling source code [Pipeline] sh + echo Running tests Running tests [Pipeline] echo Build complete for my-app [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

Shared libraries let you write common pipeline code once and reuse it everywhere, reducing repetition and mistakes.

Common Mistakes
Writing the same build steps in every Jenkinsfile instead of using a shared library.
This causes duplicated code that is hard to maintain and update.
Put common steps in a shared library function and call it from pipelines.
Not configuring the shared library correctly in Jenkins global settings.
Pipelines will fail because Jenkins cannot find the shared library code.
Set up the shared library in Jenkins configuration with the correct repository and name.
Forgetting to commit and push the shared library code to the Git repository.
Jenkins will use an empty or outdated library version, causing errors or missing steps.
Always commit and push changes to the shared library repo before running pipelines.
Summary
Create a shared library Groovy script with common pipeline steps.
Configure Jenkins to use the shared library globally from a Git repo.
Call the shared library function in Jenkins pipelines to reuse code.
Trigger pipelines to run the shared build steps without duplication.