0
0
Jenkinsdevops~10 mins

Loading libraries in Jenkinsfile - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Loading libraries in Jenkinsfile
Start Jenkinsfile Execution
Load Shared Library
Library Methods Available
Call Library Functions
Continue Pipeline Steps
End
The Jenkinsfile starts, loads a shared library, makes its functions available, calls them, then continues the pipeline.
Execution Sample
Jenkins
@Library('my-shared-lib') _

pipeline {
  agent any
  stages {
    stage('Use Lib') {
      steps {
        script {
          myFunction()
        }
      }
    }
  }
}
This Jenkinsfile loads a shared library named 'my-shared-lib' and calls a function 'myFunction' from it during the pipeline.
Process Table
StepActionEvaluationResult
1Start JenkinsfileN/AJenkinsfile execution begins
2Load library 'my-shared-lib'@Library('my-shared-lib') _Library loaded, functions available
3Enter pipeline stage 'Use Lib'stage('Use Lib')Stage starts
4Call function 'myFunction()' from librarymyFunction()Function executes successfully
5Complete stage 'Use Lib'stage completeStage ends
6Pipeline endspipeline completePipeline execution finishes
💡 Pipeline completes after all stages and library functions run
Status Tracker
VariableStartAfter Step 2After Step 4Final
libraryLoadedfalsetruetruetrue
myFunctionCalledfalsefalsetruetrue
Key Moments - 2 Insights
Why do we need to load the library before calling its functions?
Because the library functions are not available until Jenkins loads the library (see execution_table step 2). Calling functions before loading causes errors.
What happens if the library name is wrong?
Jenkins will fail at the library loading step (step 2) and stop the pipeline, because it cannot find the library.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the library loaded?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Check the 'Action' column for 'Load library' in execution_table row 2
According to variable_tracker, when does 'myFunctionCalled' become true?
AAfter Step 4
BAfter Step 2
CAt Start
DNever
💡 Hint
Look at 'myFunctionCalled' row in variable_tracker after Step 4
If the library was not loaded, what would happen when calling 'myFunction()'?
AFunction runs normally
BPipeline fails with error
CPipeline skips the function
DFunction runs but returns null
💡 Hint
Refer to key_moments about calling functions before loading library
Concept Snapshot
Loading libraries in Jenkinsfile:
- Use library 'name' to load shared libraries
- Load before calling any library functions
- Loaded functions become available in pipeline
- Calling functions before loading causes errors
- Libraries help reuse code across pipelines
Full Transcript
This visual execution shows how Jenkinsfile loads a shared library named 'my-shared-lib'. The pipeline starts, then Jenkins loads the library making its functions available. Next, the pipeline enters a stage where it calls a function 'myFunction' from the library. After the function runs, the stage and pipeline complete. Variables track that the library is loaded before the function call. Key moments explain why loading first is important and what happens if the library name is wrong. The quiz tests understanding of when loading happens and effects of missing library load.