0
0
Jenkinsdevops~10 mins

@Library annotation in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - @Library annotation
Start Jenkinsfile
@Library annotation reads library name
Jenkins loads shared library code
Library functions/classes become available
Jenkinsfile calls library code
Pipeline executes with library support
End
The @Library annotation tells Jenkins to load shared library code before running the pipeline script.
Execution Sample
Jenkins
@Library('my-shared-lib') _
pipeline {
  agent any
  stages {
    stage('Example') {
      steps { myFunction() }
    }
  }
}
This Jenkinsfile uses @Library to load 'my-shared-lib' and calls a function from it in the pipeline.
Process Table
StepActionEvaluationResult
1Read Jenkinsfile startDetect @Library('my-shared-lib')Prepare to load 'my-shared-lib'
2Load shared libraryFetch 'my-shared-lib' from configured sourceLibrary code loaded and available
3Parse pipeline blockRecognize stages and stepsPipeline structure ready
4Execute stage 'Example'Call myFunction() from librarymyFunction() runs successfully
5Complete pipelineNo errorsPipeline finishes successfully
💡 Pipeline ends after all stages run with library functions available
Status Tracker
VariableStartAfter Step 2After Step 4Final
libraryLoadedfalsetruetruetrue
myFunctionAvailablefalsetruetruetrue
pipelineStatusnot startednot startedrunningcompleted
Key Moments - 2 Insights
Why do we need the underscore (_) after the @Library annotation?
The underscore tells Jenkins to load the library immediately and make its functions available in the script, as shown in execution_table step 2 and 4.
What happens if the library name is incorrect or missing?
Jenkins will fail to load the library, causing errors when calling library functions, stopping the pipeline before step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the shared library code loaded?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row 2
According to variable_tracker, when does 'myFunctionAvailable' become true?
AAfter Step 2
BStart
CAfter Step 4
DFinal
💡 Hint
Look at the 'myFunctionAvailable' row and its values after each step
If the @Library annotation is missing the underscore (_), what likely happens?
APipeline fails to start
BLibrary loads but functions are not available
CPipeline runs normally
DLibrary loads twice
💡 Hint
Refer to key_moments explanation about the underscore usage
Concept Snapshot
@Library annotation loads shared Jenkins libraries.
Syntax: @Library('lib-name') _
Loads code before pipeline runs.
Makes library functions/classes available.
Use underscore (_) to apply immediately.
Without it, library code is not imported.
Full Transcript
The @Library annotation in Jenkinsfiles tells Jenkins to load shared library code before running the pipeline. The underscore (_) after the annotation is important because it instructs Jenkins to import the library functions immediately, making them available in the pipeline script. The execution flow starts by reading the Jenkinsfile, detecting the @Library annotation, loading the shared library, then running the pipeline stages that can call library functions. Variables like 'libraryLoaded' and 'myFunctionAvailable' change from false to true once the library is loaded. If the library name is wrong or the underscore is missing, the pipeline will fail or the functions won't be available. This visual trace helps beginners see how the annotation affects pipeline execution step-by-step.