@Library annotation in Jenkins - Time & Space Complexity
When using the @Library annotation in Jenkins pipelines, it is important to understand how loading shared libraries affects execution time.
We want to know how the time to load libraries grows as the number of libraries increases.
Analyze the time complexity of loading multiple shared libraries using @Library.
@Library(['libA', 'libB', 'libC']) _
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
libA.someFunction()
libB.someFunction()
libC.someFunction()
}
}
}
}
}
This code loads three shared libraries and calls a function from each.
Look for repeated actions that take time.
- Primary operation: Loading each shared library once.
- How many times: Once per library listed in
@Library.
Loading time grows as you add more libraries.
| Input Size (number of libraries) | Approx. Operations (load calls) |
|---|---|
| 1 | 1 |
| 5 | 5 |
| 10 | 10 |
Pattern observation: The time to load libraries increases directly with the number of libraries.
Time Complexity: O(n)
This means loading time grows in a straight line as you add more libraries.
[X] Wrong: "Loading multiple libraries happens all at once, so time stays the same no matter how many libraries."
[OK] Correct: Each library must be loaded separately, so more libraries mean more loading time.
Understanding how loading shared libraries scales helps you write efficient Jenkins pipelines and shows you can think about performance in real projects.
What if we cached the libraries after the first load? How would that change the time complexity?
Practice
@Library annotation in Jenkins pipelines?Solution
Step 1: Understand the role of
The@Library@Libraryannotation is used to load shared libraries in Jenkins pipelines.Step 2: Identify its main purpose
Loading shared libraries allows reuse of code and better organization of pipeline scripts.Final Answer:
To load shared code libraries for reuse in pipelines -> Option BQuick Check:
@Libraryloads shared libraries = C [OK]
@Library loads reusable code libraries [OK]- Confusing
@Librarywith environment variable setup - Thinking it triggers jobs instead of loading code
- Using it to specify pipeline agents
Solution
Step 1: Recall the correct syntax for
The correct syntax uses parentheses and quotes around the library name, followed by an underscore.@LibraryStep 2: Match the syntax to options
@Library('utils') _ matches@Library('utils') _, which is correct.Final Answer:
@Library('utils') _ -> Option AQuick Check:
Correct syntax uses parentheses and underscore = A [OK]
@Library [OK]- Omitting parentheses or quotes
- Using wrong annotation like
@LoadLibrary - Placing underscore before the annotation
@Library('mathLib') _
pipeline {
agent any
stages {
stage('Calculate') {
steps {
script {
echo mathLib.add(2, 3).toString()
}
}
}
}
}What will be the output if the shared library 'mathLib' has a method
add(a, b) that returns the sum of two numbers?Solution
Step 1: Understand the method call in the script block
The code callsmathLib.add(2, 3)which returns the sum of 2 and 3.Step 2: Convert the result to string and echo
The result 5 is converted to string and printed byecho.Final Answer:
5 -> Option AQuick Check:
Sum of 2 and 3 = 5 [OK]
- Expecting the method name printed instead of result
- Confusing concatenation with addition
- Assuming method is missing causing error
@Library('commonLib')
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
commonLib.doSomething()
}
}
}
}
}But Jenkins throws an error saying
Missing required underscore after @Library annotation. How do you fix it?Solution
Step 1: Identify the syntax error from the message
The error says the underscore is missing after@Library, which is required syntax.Step 2: Correct the syntax by adding underscore
Add an underscore immediately after the annotation:@Library('commonLib') _.Final Answer:
Add an underscore_immediately after the annotation like@Library('commonLib') _-> Option DQuick Check:
Underscore after@Libraryfixes syntax [OK]
@Library(...) [OK]- Omitting the underscore
- Placing underscore before annotation
- Changing annotation name incorrectly
@Library annotation?Solution
Step 1: Recall how to load multiple libraries
Multiple libraries can be loaded by passing a list of names inside@Libraryannotation.Step 2: Identify correct syntax for multiple libraries
The correct syntax is@Library(['utils', 'helpers']) _which loads both libraries at once.Final Answer:
@Library(['utils', 'helpers']) _ -> Option CQuick Check:
Use list inside@Libraryfor multiple libs [OK]
@Library to load many libs [OK]- Trying to use multiple
@Libraryannotations - Passing comma-separated string instead of list
- Combining names into one string without list
Ready to prove it?
Pass the test and this pattern moves unknown
