What is the purpose of loading libraries in a Jenkinsfile?
Loading libraries in a Jenkinsfile allows you to reuse shared code and functions across multiple pipelines, making your Jenkinsfiles cleaner and easier to maintain.
Click to reveal answer
beginner
How do you load a shared library in a Jenkinsfile?
You use the @Library annotation followed by the library name, for example: @Library('my-shared-lib') _ at the top of your Jenkinsfile.
Click to reveal answer
intermediate
What does the underscore (_) mean after the @Library('lib-name') annotation?
The underscore tells Jenkins to import the library and make its classes and functions available in the Jenkinsfile.
Click to reveal answer
intermediate
Where do you configure shared libraries in Jenkins?
Shared libraries are configured in Jenkins under Manage Jenkins > Configure System > Global Pipeline Libraries. You define the library name, source code repository, and default version there.
Click to reveal answer
advanced
What is the difference between a global and a local library in Jenkins?
Global libraries are configured once in Jenkins and available to all pipelines. Local libraries are loaded from the same repository as the Jenkinsfile and are only available to that pipeline.
Click to reveal answer
How do you import a shared library named 'utils' in a Jenkinsfile?
A@Library('utils') _
Bimport 'utils'
CloadLibrary('utils')
DuseLibrary('utils')
✗ Incorrect
The correct syntax to import a shared library is using the @Library annotation followed by an underscore.
Where do you set up the repository URL for a shared library in Jenkins?
AManage Jenkins > Configure System > Global Pipeline Libraries
BManage Jenkins > Manage Plugins
CPipeline script in Jenkinsfile
DManage Jenkins > System Information
✗ Incorrect
Shared libraries are configured globally in Jenkins under Global Pipeline Libraries.
What does the underscore (_) do after the @Library annotation?
AIt comments out the line
BIt imports the library into the Jenkinsfile
CIt defines a variable
DIt runs the library code immediately
✗ Incorrect
The underscore tells Jenkins to import the library and make its contents available.
Which of the following is NOT true about shared libraries in Jenkins?
AThey are configured globally in Jenkins
BThey help reuse code across pipelines
CThey can be versioned
DThey must be written in Groovy
✗ Incorrect
While global shared libraries are configured in Jenkins, local shared libraries are loaded from the pipeline repository itself and are not configured globally.
What is a local library in Jenkins?
AA library that runs outside Jenkins
BA library configured globally in Jenkins
CA library loaded from the same repository as the Jenkinsfile
DA library downloaded from the internet
✗ Incorrect
Local libraries are stored in the same repository as the Jenkinsfile and only available to that pipeline.
Explain how to load and use a shared library in a Jenkinsfile.
Think about the syntax and where you configure the library.
You got /4 concepts.
Describe the difference between global and local libraries in Jenkins and when you might use each.
Consider scope and reuse.
You got /4 concepts.
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