What if you could fix a bug once and have it update everywhere instantly?
Why Library directory structure in Jenkins? - Purpose & Use Cases
Imagine you have many Jenkins pipeline scripts scattered everywhere without any order. You want to reuse some common code, but you have to copy-paste it into each pipeline manually.
This manual copying is slow and risky. If you fix a bug in one place, you have to remember to fix it everywhere else. It's easy to make mistakes and hard to keep track of all copies.
Using a well-organized library directory structure in Jenkins lets you store shared code in one place. Pipelines can call this shared code easily, so you write it once and use it many times.
def greet() { echo 'Hello from pipeline!' } // Copy this in every pipeline
@Library('my-shared-lib') _ greet() // Call shared function from library
You can maintain and update shared pipeline code in one place, making your Jenkins jobs cleaner and more reliable.
A company has dozens of Jenkins pipelines for different projects. By using a library directory structure, they keep common steps like notifications and deployments centralized, saving time and reducing errors.
Manual code duplication is slow and error-prone.
Library directory structure centralizes shared pipeline code.
It makes Jenkins pipelines easier to maintain and reuse.
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
