0
0
Jenkinsdevops~10 mins

Why shared libraries reduce duplication in Jenkins - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why shared libraries reduce duplication
Write shared library code
Store in central repo
Multiple Jenkinsfiles
Reference shared library
Reuse common code
Less duplication, easier updates
Shared libraries hold common code in one place, so Jenkinsfiles can reuse it instead of copying code.
Execution Sample
Jenkins
@Library('my-shared-lib') _

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        mySharedStep()
      }
    }
  }
}
This Jenkinsfile loads a shared library and calls a common step from it.
Process Table
StepActionEvaluationResult
1Load shared library 'my-shared-lib'Library found in repoLibrary code available
2Start pipeline executionAgent allocatedPipeline running
3Enter 'Build' stageStage recognizedStage started
4Call 'mySharedStep()' from libraryFunction found in shared libShared code executed
5Complete 'Build' stageNo errorsStage finished
6Pipeline endsAll stages donePipeline success
💡 Pipeline completes after running shared library code once
Status Tracker
VariableStartAfter Step 1After Step 4Final
libraryLoadedfalsetruetruetrue
pipelineStatusnot startednot startedrunningsuccess
sharedStepExecutedfalsefalsetruetrue
Key Moments - 3 Insights
Why don't we see the shared library code inside the Jenkinsfile?
Because the Jenkinsfile references the shared library by name (see execution_table step 1), the actual code lives in the library repo and is loaded at runtime.
How does using a shared library reduce duplication?
Multiple Jenkinsfiles call the same shared library function (execution_table step 4), so common code is written once and reused, avoiding copy-pasting.
What happens if the shared library code changes?
All Jenkinsfiles using it get the updated code automatically next time they run, making maintenance easier (implied by execution_table steps 1 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'libraryLoaded' after step 1?
Afalse
Btrue
Cundefined
Derror
💡 Hint
Check variable_tracker column 'After Step 1' for 'libraryLoaded'
At which step does the shared library code actually run?
AStep 4
BStep 1
CStep 2
DStep 6
💡 Hint
Look at execution_table 'Action' column for when 'mySharedStep()' is called
If the shared library was not loaded, what would happen at step 4?
APipeline runs normally
BShared code runs anyway
CError: function not found
DPipeline skips the step
💡 Hint
Refer to execution_table step 4 where function is found in shared lib
Concept Snapshot
Shared libraries store common Jenkins pipeline code centrally.
Jenkinsfiles load libraries with 'library()' call.
They call shared functions instead of duplicating code.
This reduces errors and makes updates easy.
All pipelines using the library get improvements automatically.
Full Transcript
Shared libraries in Jenkins help reduce duplication by storing common pipeline code in one place. Jenkinsfiles load these libraries using the 'library()' function. When a Jenkinsfile calls a shared function, the code runs from the library, not copied inside the Jenkinsfile. This means many Jenkinsfiles can reuse the same code, avoiding repetition. If the shared library code changes, all Jenkinsfiles using it get the update automatically. This makes pipeline maintenance easier and less error-prone.