Bird
Raised Fist0
Jenkinsdevops~10 mins

Library directory structure in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Library directory structure
Start: Jenkins Pipeline
↓
Load Shared Library
↓
Look for vars/ directory
↓
Look for src/ directory
↓
Look for resources/ directory
↓
Use library classes and vars in pipeline
↓
Pipeline executes with shared code
Jenkins pipeline loads the shared library, then looks for specific directories (vars, src, resources) to find reusable code and resources for the pipeline execution.
Execution Sample
Jenkins
library('my-shared-lib')

pipeline {
  agent any
  stages {
    stage('Example') {
      steps { script { myFunction() } }
    }
  }
}
This Jenkins pipeline loads a shared library and calls a function defined in the library's vars directory.
Process Table
StepActionDirectory CheckedResultPipeline Effect
1Load shared library 'my-shared-lib'N/ALibrary loadedLibrary code available
2Check for vars/ directoryvars/FoundFunctions and vars available for pipeline
3Check for src/ directorysrc/FoundClasses available for pipeline
4Check for resources/ directoryresources/FoundStatic files available for pipeline
5Call myFunction() from vars/vars/myFunction.groovyExecutedStep runs in pipeline
6Pipeline completesN/ASuccessPipeline finished
7ExitN/ANo more stepsPipeline ends
💡 Pipeline ends after all stages complete successfully
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
libraryLoadedfalsetruetruetruetruetruetrue
varsDirFoundfalsefalsetruetruetruetruetrue
srcDirFoundfalsefalsefalsetruetruetruetrue
resourcesDirFoundfalsefalsefalsefalsetruetruetrue
myFunctionCalledfalsefalsefalsefalsefalsetruetrue
pipelineStatusnot startedrunningrunningrunningrunningrunningsuccess
Key Moments - 2 Insights
Why does Jenkins check multiple directories like vars/, src/, and resources/ in the library?
Jenkins looks in these directories to find different types of reusable code and files: vars/ for simple functions, src/ for classes, and resources/ for static files. This is shown in steps 2, 3, and 4 of the execution table.
What happens if the vars/ directory is missing in the shared library?
If vars/ is missing, functions like myFunction() won't be found, causing pipeline errors when called. Step 2 in the execution table shows vars/ found; if missing, the pipeline would fail at step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the shared library first confirmed loaded?
AStep 2
BStep 5
CStep 1
DStep 7
💡 Hint
Check the 'Action' and 'Result' columns in the first row of the execution table.
According to the variable tracker, when does the variable 'myFunctionCalled' change to true?
AAfter Step 5
BAfter Step 4
CAfter Step 2
DAfter Step 7
💡 Hint
Look at the 'myFunctionCalled' row and see when it changes from false to true.
If the resources/ directory was missing, which step in the execution table would show a different result?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'Directory Checked' and 'Result' columns for step 4.
Concept Snapshot
Jenkins shared libraries have a standard directory structure:
- vars/: simple Groovy scripts as functions
- src/: Groovy classes in packages
- resources/: static files like templates
Pipeline loads the library, then looks in these directories to find reusable code.
Functions in vars/ can be called directly in pipelines.
Classes in src/ require import or usage via @Library.
Full Transcript
This visual execution shows how Jenkins loads a shared library and checks its directory structure. First, the pipeline loads the library, confirming it is available. Then Jenkins looks for the vars/ directory to find simple functions, the src/ directory for classes, and the resources/ directory for static files. Each directory found makes its contents available to the pipeline. The pipeline then calls a function from vars/, which executes successfully. Variables track the state of loading and function calls. Key moments clarify why Jenkins checks multiple directories and what happens if one is missing. The quiz tests understanding of when the library is loaded, when functions are called, and the effect of missing directories.

Practice

(1/5)
1. What is the primary purpose of the vars/ directory in a Jenkins shared library?
easy
A. To store simple Groovy scripts accessible as global variables
B. To store compiled Java classes
C. To hold configuration files like YAML or JSON
D. To keep pipeline job definitions

Solution

  1. Step 1: Understand the role of vars/ directory

    The vars/ directory is used to store simple Groovy scripts that Jenkins loads as global variables or functions.
  2. Step 2: Differentiate from other directories

    The src/ directory is for classes, and resources/ is for config files, so vars/ is specifically for simple scripts.
  3. Final Answer:

    To store simple Groovy scripts accessible as global variables -> Option A
  4. Quick Check:

    vars/ = simple scripts [OK]
Hint: Remember: vars = simple scripts, src = classes, resources = configs [OK]
Common Mistakes:
  • Confusing vars/ with src/ directory
  • Thinking vars/ holds config files
  • Assuming vars/ stores compiled classes
2. Which directory should you place a Groovy class file named MyHelper.groovy in a Jenkins shared library?
easy
A. src/
B. vars/
C. resources/
D. jobs/

Solution

  1. Step 1: Identify the purpose of src/ directory

    The src/ directory is meant for Groovy or Java classes in a Jenkins shared library.
  2. Step 2: Match the file type to directory

    Since MyHelper.groovy is a class file, it belongs in src/, not vars/ or resources/.
  3. Final Answer:

    src/ -> Option A
  4. Quick Check:

    Classes go to src/ [OK]
Hint: Classes always go inside src/ folder [OK]
Common Mistakes:
  • Placing class files in vars/
  • Putting Groovy classes inside resources/
  • Using non-existent jobs/ directory
3. Given this Jenkins shared library structure:
vars/notify.groovy
src/org/example/Helper.groovy
resources/config/settings.yml
Which file would you use to load configuration settings inside a pipeline?
medium
A. vars/notify.groovy
B. resources/config/settings.yml
C. vars/settings.yml
D. src/org/example/Helper.groovy

Solution

  1. Step 1: Identify the purpose of resources/ directory

    The resources/ directory holds static files like configuration files (YAML, JSON) used by the library.
  2. Step 2: Match the file type to usage

    Since settings.yml is a config file, it should be loaded from resources/config/settings.yml.
  3. Final Answer:

    resources/config/settings.yml -> Option B
  4. Quick Check:

    Config files = resources/ [OK]
Hint: Config files always go in resources/ [OK]
Common Mistakes:
  • Trying to load config from vars/
  • Confusing class files with config files
  • Assuming config files go in src/
4. You placed a Groovy script build.groovy inside resources/ directory. What issue will you face when trying to call build() in your Jenkins pipeline?
medium
A. The script will run normally without issues
B. The script will be compiled but not executed
C. Jenkins will throw a missing method error because resources/ is for static files only
D. The pipeline will ignore the script and continue silently

Solution

  1. Step 1: Understand the role of resources/ directory

    The resources/ directory is only for static files like configs, not executable Groovy scripts.
  2. Step 2: Consequence of placing script in resources/

    Since build.groovy is not in vars/ or src/, Jenkins cannot load it as a callable method, causing a missing method error.
  3. Final Answer:

    Jenkins will throw a missing method error because resources/ is for static files only -> Option C
  4. Quick Check:

    Scripts must be in vars/ or src/ [OK]
Hint: Scripts in resources/ cause missing method errors [OK]
Common Mistakes:
  • Expecting Groovy scripts to run from resources/
  • Ignoring directory purpose and mixing files
  • Assuming silent failure without error
5. You want to create a reusable Jenkins shared library that includes:
- A global function notifyBuild
- A helper class BuildHelper
- A configuration file email.yml
Which directory structure correctly organizes these files?
hard
A. src/notifyBuild.groovy, vars/BuildHelper.groovy, resources/email.yml
B. vars/notifyBuild.groovy, resources/BuildHelper.groovy, src/email.yml
C. resources/notifyBuild.groovy, src/BuildHelper.groovy, vars/email.yml
D. vars/notifyBuild.groovy, src/BuildHelper.groovy, resources/email.yml

Solution

  1. Step 1: Assign global functions to vars/

    Global functions like notifyBuild go into vars/ as Groovy scripts.
  2. Step 2: Place helper classes in src/

    Classes like BuildHelper belong in src/ directory.
  3. Step 3: Store config files in resources/

    Configuration files such as email.yml must be inside resources/.
  4. Final Answer:

    vars/notifyBuild.groovy, src/BuildHelper.groovy, resources/email.yml -> Option D
  5. Quick Check:

    vars = functions, src = classes, resources = configs [OK]
Hint: Match file type: vars=func, src=class, resources=config [OK]
Common Mistakes:
  • Swapping vars/ and src/ for functions and classes
  • Placing config files outside resources/
  • Mixing file types in wrong directories
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard