What is the purpose of the 'vars' directory in a Jenkins shared library?
The 'vars' directory contains Groovy scripts that define global variables or functions accessible in Jenkins pipelines. These scripts act like reusable steps or helpers.
Click to reveal answer
beginner
Where do you place reusable Groovy classes in a Jenkins shared library?
Reusable Groovy classes go inside the 'src' directory, organized by package structure. This keeps code modular and easy to maintain.
Click to reveal answer
intermediate
What is the role of the 'resources' directory in a Jenkins shared library?
The 'resources' directory holds non-Groovy files like configuration files, templates, or scripts that your library code can load and use.
Click to reveal answer
intermediate
How does Jenkins find and use the shared library code from the directory structure?
Jenkins loads the shared library from the root directory, then looks into 'vars' for global scripts, 'src' for classes, and 'resources' for supporting files. This structure helps Jenkins organize and access code efficiently.
Click to reveal answer
beginner
Why is it important to follow the standard directory structure in Jenkins shared libraries?
Following the standard structure ensures Jenkins can automatically find and load your code. It also makes your library easier to understand and maintain by others.
Click to reveal answer
Which directory in a Jenkins shared library contains global variables and functions?
Asrc
Bbin
Cresources
Dvars
✗ Incorrect
The 'vars' directory holds Groovy scripts that define global variables and functions accessible in pipelines.
Where should you place Groovy classes in a Jenkins shared library?
Aresources
Bsrc
Cvars
Dlib
✗ Incorrect
Groovy classes go inside the 'src' directory, organized by package.
What type of files does the 'resources' directory hold?
ANon-Groovy files like templates and configs
BGroovy classes
CGroovy scripts for global variables
DCompiled binaries
✗ Incorrect
The 'resources' directory contains files like templates, configuration files, or scripts used by the library.
Why is it important to follow the Jenkins shared library directory structure?
ATo increase build time
BTo confuse other developers
CTo make Jenkins load the library automatically
DTo avoid using Groovy
✗ Incorrect
Following the standard structure allows Jenkins to find and load your shared library code automatically.
If you want to add a new helper function accessible in all pipelines, where do you put it?
Avars
Bdocs
Cresources
Dsrc
✗ Incorrect
Helper functions accessible globally are placed as Groovy scripts inside the 'vars' directory.
Describe the main directories in a Jenkins shared library and their purposes.
Think about where Jenkins looks for different types of code and files.
You got /3 concepts.
Explain why maintaining the standard directory structure in Jenkins shared libraries is important.
Consider how Jenkins and your team benefit from a consistent layout.
You got /3 concepts.
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
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.
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.
Final Answer:
To store simple Groovy scripts accessible as global variables -> Option A
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
Step 1: Identify the purpose of src/ directory
The src/ directory is meant for Groovy or Java classes in a Jenkins shared library.
Step 2: Match the file type to directory
Since MyHelper.groovy is a class file, it belongs in src/, not vars/ or resources/.
Final Answer:
src/ -> Option A
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
Step 1: Identify the purpose of resources/ directory
The resources/ directory holds static files like configuration files (YAML, JSON) used by the library.
Step 2: Match the file type to usage
Since settings.yml is a config file, it should be loaded from resources/config/settings.yml.
Final Answer:
resources/config/settings.yml -> Option B
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
Step 1: Understand the role of resources/ directory
The resources/ directory is only for static files like configs, not executable Groovy scripts.
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.
Final Answer:
Jenkins will throw a missing method error because resources/ is for static files only -> Option C
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
Step 1: Assign global functions to vars/
Global functions like notifyBuild go into vars/ as Groovy scripts.
Step 2: Place helper classes in src/
Classes like BuildHelper belong in src/ directory.
Step 3: Store config files in resources/
Configuration files such as email.yml must be inside resources/.
Final Answer:
vars/notifyBuild.groovy, src/BuildHelper.groovy, resources/email.yml -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.