Library directory structure in Jenkins - Time & Space Complexity
We want to understand how the time to load or access files in a Jenkins library grows as the number of files or folders increases.
How does the structure size affect the time Jenkins takes to find and use library code?
Analyze the time complexity of the following Jenkins pipeline snippet that loads a shared library and calls a function.
@Library('my-shared-lib') _
pipeline {
agent any
stages {
stage('Use Library') {
steps {
script {
myFunction()
}
}
}
}
}
This code loads a shared library named 'my-shared-lib' and calls a function from it during the pipeline run.
Look at what Jenkins does when loading the library:
- Primary operation: Searching through the library directory structure to find the requested function or script.
- How many times: Once per pipeline run, but internally Jenkins may scan multiple folders and files depending on the library size.
As the number of files and folders in the library grows, Jenkins spends more time searching for the requested code.
| Input Size (number of files/folders) | Approx. Operations (search steps) |
|---|---|
| 10 | About 10 search steps |
| 100 | About 100 search steps |
| 1000 | About 1000 search steps |
Pattern observation: The search time grows roughly in direct proportion to the number of files and folders.
Time Complexity: O(n)
This means the time Jenkins takes to find library code grows linearly with the number of files and folders in the library.
[X] Wrong: "Loading a library is instant no matter how big it is."
[OK] Correct: Jenkins must search through the directory structure, so bigger libraries take more time to load and find code.
Understanding how Jenkins handles library directories helps you explain build times and troubleshoot slow pipelines, a useful skill in real projects.
What if the library used caching to remember file locations? How would that change the time complexity?
Practice
vars/ directory in a Jenkins shared library?Solution
Step 1: Understand the role of
Thevars/directoryvars/directory is used to store simple Groovy scripts that Jenkins loads as global variables or functions.Step 2: Differentiate from other directories
Thesrc/directory is for classes, andresources/is for config files, sovars/is specifically for simple scripts.Final Answer:
To store simple Groovy scripts accessible as global variables -> Option AQuick Check:
vars/= simple scripts [OK]
- Confusing
vars/withsrc/directory - Thinking
vars/holds config files - Assuming
vars/stores compiled classes
MyHelper.groovy in a Jenkins shared library?Solution
Step 1: Identify the purpose of
Thesrc/directorysrc/directory is meant for Groovy or Java classes in a Jenkins shared library.Step 2: Match the file type to directory
SinceMyHelper.groovyis a class file, it belongs insrc/, notvars/orresources/.Final Answer:
src/-> Option AQuick Check:
Classes go tosrc/[OK]
src/ folder [OK]- Placing class files in
vars/ - Putting Groovy classes inside
resources/ - Using non-existent
jobs/directory
vars/notify.groovysrc/org/example/Helper.groovyresources/config/settings.ymlWhich file would you use to load configuration settings inside a pipeline?
Solution
Step 1: Identify the purpose of
Theresources/directoryresources/directory holds static files like configuration files (YAML, JSON) used by the library.Step 2: Match the file type to usage
Sincesettings.ymlis a config file, it should be loaded fromresources/config/settings.yml.Final Answer:
resources/config/settings.yml-> Option BQuick Check:
Config files =resources/[OK]
resources/ [OK]- Trying to load config from
vars/ - Confusing class files with config files
- Assuming config files go in
src/
build.groovy inside resources/ directory. What issue will you face when trying to call build() in your Jenkins pipeline?Solution
Step 1: Understand the role of
Theresources/directoryresources/directory is only for static files like configs, not executable Groovy scripts.Step 2: Consequence of placing script in
Sinceresources/build.groovyis not invars/orsrc/, Jenkins cannot load it as a callable method, causing a missing method error.Final Answer:
Jenkins will throw a missing method error becauseresources/is for static files only -> Option CQuick Check:
Scripts must be invars/orsrc/[OK]
resources/ cause missing method errors [OK]- Expecting Groovy scripts to run from
resources/ - Ignoring directory purpose and mixing files
- Assuming silent failure without error
- A global function
notifyBuild- A helper class
BuildHelper- A configuration file
email.ymlWhich directory structure correctly organizes these files?
Solution
Step 1: Assign global functions to
Global functions likevars/notifyBuildgo intovars/as Groovy scripts.Step 2: Place helper classes in
Classes likesrc/BuildHelperbelong insrc/directory.Step 3: Store config files in
Configuration files such asresources/email.ymlmust be insideresources/.Final Answer:
vars/notifyBuild.groovy,src/BuildHelper.groovy,resources/email.yml-> Option DQuick Check:
vars = functions, src = classes, resources = configs [OK]
- Swapping
vars/andsrc/for functions and classes - Placing config files outside
resources/ - Mixing file types in wrong directories
Ready to prove it?
Pass the test and this pattern moves unknown
