📖 Scenario: You are setting up a Jenkins pipeline to automate your software build process. To keep your Jenkinsfile clean and reusable, you want to load a shared library that contains common functions.
🎯 Goal: Learn how to load a shared library in a Jenkinsfile and use a function from that library in your pipeline.
📋 What You'll Learn
Create a Jenkinsfile with a pipeline block
Load a shared library named my-shared-lib
Call a function greet from the shared library
Print the greeting message in the pipeline
💡 Why This Matters
🌍 Real World
Shared libraries in Jenkins help teams reuse common code like utility functions, reducing duplication and errors.
💼 Career
Knowing how to load and use shared libraries is essential for Jenkins pipeline developers and DevOps engineers to build maintainable CI/CD pipelines.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline
Write a Jenkinsfile that defines a pipeline block with an empty stages section.
Jenkins
Hint
Start with the basic Jenkins pipeline syntax including pipeline, agent any, and an empty stages block.
2
Load the shared library
Inside the pipeline block but before stages, add a libraries block that loads the shared library named my-shared-lib using lib('my-shared-lib').
Jenkins
Hint
Use the libraries directive with lib('my-shared-lib') to load the shared library before the stages block.
3
Call the greet function from the library
Inside the stages block, add a stage named Greet with a steps section that calls the greet() function and stores its result in a variable called message.
Jenkins
Hint
Use a stage with steps and a script block to call greet() and assign it to message.
4
Print the greeting message
Inside the script block of the Greet stage, add a println statement to print the message variable.
Jenkins
Hint
Use println(message) to print the greeting message returned by greet().
Practice
(1/5)
1. What is the main purpose of loading libraries in a Jenkinsfile?
easy
A. To automatically update Jenkins plugins
B. To reuse common pipeline code across multiple Jenkinsfiles
C. To increase the build speed by caching dependencies
D. To create new Jenkins jobs dynamically
Solution
Step 1: Understand the role of libraries in Jenkinsfiles
Libraries contain reusable pipeline code that can be shared across multiple Jenkinsfiles.
Step 2: Identify the benefit of using libraries
Using libraries helps keep Jenkinsfiles clean and avoids code duplication by reusing common code.
Final Answer:
To reuse common pipeline code across multiple Jenkinsfiles -> Option B
Quick Check:
Libraries = Reuse code [OK]
Hint: Libraries help reuse code, not speed or plugins [OK]
Common Mistakes:
Confusing libraries with plugin management
Thinking libraries speed up builds directly
Assuming libraries create jobs automatically
2. Which of the following is the correct syntax to load a shared library named 'utils' at version '1.2' in a Jenkinsfile?
easy
A. loadLibrary('utils', '1.2')
B. useLibrary utils version 1.2
C. import 'utils:1.2'
D. library 'utils@1.2'
Solution
Step 1: Recall the Jenkinsfile syntax for loading libraries
The correct syntax uses the keyword library followed by the library name and version in quotes, separated by '@'.
Step 2: Match the syntax to the options
library 'utils@1.2' matches the correct syntax: library 'utils@1.2'.
Final Answer:
library 'utils@1.2' -> Option D
Quick Check:
library 'name@version' = correct syntax [OK]
Hint: Use library 'name@version' format to load libraries [OK]
Common Mistakes:
Using incorrect keywords like loadLibrary or import
Missing quotes around the library name and version
Using spaces instead of '@' between name and version
But the pipeline fails with an error: groovy.lang.MissingPropertyException: No such property: tools. What is the likely cause?
medium
A. The agent 'any' is not supported in this Jenkins version
B. The method build() does not exist in the tools library
C. The library 'tools' is not configured or not loaded properly
D. The Jenkinsfile syntax for stages is incorrect
Solution
Step 1: Understand the error message
The error says 'No such property: tools', meaning the variable or library named 'tools' is not recognized in the pipeline.
Step 2: Identify the cause related to library loading
This usually happens if the shared library 'tools' is not configured in Jenkins or the library failed to load, so the variable 'tools' is undefined.
Final Answer:
The library 'tools' is not configured or not loaded properly -> Option C
Quick Check:
MissingPropertyException means library not loaded [OK]
Hint: MissingPropertyException means library not loaded or configured [OK]
Common Mistakes:
Assuming method build() missing causes this error
Blaming pipeline syntax for this error
Thinking agent 'any' causes this error
5. You want to load two shared libraries in your Jenkinsfile: 'utils' at version '1.0' and 'deploy' at version '2.1'. Which Jenkinsfile snippet correctly loads both libraries and calls a method deployApp() from the 'deploy' library inside a stage?
Each library must be loaded with its own library 'name@version' statement; library 'utils@1.0'
library 'deploy@2.1'
pipeline {
agent any
stages {
stage('Deploy') {
steps {
script {
deploy.deployApp()
}
}
}
}
} correctly loads 'utils@1.0' and 'deploy@2.1' separately.
Step 2: Call method from the correct library namespace
To call deployApp() from 'deploy' library, use deploy.deployApp() inside the script block, as shown in library 'utils@1.0'
library 'deploy@2.1'
pipeline {
agent any
stages {
stage('Deploy') {
steps {
script {
deploy.deployApp()
}
}
}
}
}.
Final Answer:
Option A correctly loads both libraries and calls deploy.deployApp() -> Option A