📖 Scenario: You are managing a Jenkins pipeline that builds a software project. The project depends on a library, and you want to control which version of the library is used during the build.This helps ensure consistent builds and easy updates.
🎯 Goal: Create a Jenkins pipeline script that defines a library version variable, uses it in the build stage, and prints the library version used.
📋 What You'll Learn
Define a variable called libraryVersion with the value 1.2.3
Use the libraryVersion variable in the build stage to simulate using that library version
Print the library version in the pipeline output
💡 Why This Matters
🌍 Real World
Managing library versions in Jenkins pipelines ensures consistent builds and easier troubleshooting.
💼 Career
DevOps engineers often need to control dependency versions in CI/CD pipelines to maintain stable software delivery.
Progress0 / 4 steps
1
Define the library version variable
Create a variable called libraryVersion and set it to the string "1.2.3" at the top of the Jenkins pipeline script.
Jenkins
Hint
Use def libraryVersion = "1.2.3" to define the variable.
2
Add a build stage using the library version
Add a stage named Build that contains a steps block. Inside, add a echo command that prints "Building with library version: ${libraryVersion}".
Jenkins
Hint
Use pipeline { agent any stages { stage('Build') { steps { echo "Building with library version: ${libraryVersion}" } } } }.
3
Add a post section to print the library version after build
Add a post section inside the pipeline block with a always block. Inside always, add a echo command that prints "Library version used: ${libraryVersion}".
Jenkins
Hint
Add post { always { echo "Library version used: ${libraryVersion}" } } inside the pipeline.
4
Run the pipeline and display the output
Run the Jenkins pipeline script and ensure the output contains the lines Building with library version: 1.2.3 and Library version used: 1.2.3.
Jenkins
Hint
Run the pipeline and check the console output for the expected lines.
Practice
(1/5)
1. What does specifying a library version with @version in Jenkins pipelines help achieve?
easy
A. It disables the use of shared libraries in the pipeline.
B. It automatically updates the library to the latest version every time.
C. It ensures the pipeline uses a specific stable version of the shared library.
D. It deletes the library after the pipeline finishes.
Solution
Step 1: Understand library versioning purpose
Library versioning lets you pick a specific version of shared code to keep builds stable.
Step 2: Analyze the effect of specifying @version
Using @version locks the pipeline to that library version, avoiding unexpected changes.
Final Answer:
It ensures the pipeline uses a specific stable version of the shared library. -> Option C
Quick Check:
Library versioning = stable builds [OK]
Hint: Use '@version' to lock library version for stability [OK]
Common Mistakes:
Thinking '@version' updates to latest automatically
Believing it disables libraries
Assuming it deletes libraries after use
2. Which of the following is the correct syntax to load a shared library named utils at version v2.1 in a Jenkins pipeline?
easy
A. library('utils@v2.1')
B. library('utils:v2.1')
C. library('utils#v2.1')
D. library('utils-v2.1')
Solution
Step 1: Recall Jenkins library version syntax
Jenkins uses @ to specify the version or branch of a shared library.
Step 2: Match syntax to options
Only library('utils@v2.1') uses the correct @ symbol for versioning.
Final Answer:
library('utils@v2.1') -> Option A
Quick Check:
Version syntax uses '@' [OK]
Hint: Use '@' to specify library version in library() call [OK]
Hint: Versioned library methods return outputs from that version [OK]
Common Mistakes:
Assuming it prints latest version output
Expecting a failure without checking version
Thinking library is ignored without error
4. You wrote this line in your Jenkinsfile:
library('sharedLib@v1.2.3')
But the pipeline fails with an error: Library not found: sharedLib@v1.2.3. What is the most likely cause?
medium
A. The library name sharedLib is misspelled in the Jenkinsfile.
B. The version tag v1.2.3 does not exist in the library repository.
C. The Jenkins server is offline and cannot load any libraries.
D. The pipeline syntax requires double quotes instead of single quotes.
Solution
Step 1: Analyze error message
The error says the library with version v1.2.3 was not found.
Step 2: Identify common causes
This usually means the version tag v1.2.3 does not exist in the library's source repository.
Final Answer:
The version tag v1.2.3 does not exist in the library repository. -> Option B
Quick Check:
Missing version tag causes library not found error [OK]
Hint: Check if version tag exists in library repo when loading fails [OK]
Common Mistakes:
Assuming syntax quotes cause the error
Blaming library name spelling without checking version
Ignoring network or server issues without evidence
5. You want to update your Jenkins pipeline to use a new library version feature-x but keep the old version stable available for some jobs. How can you manage this using Jenkins library versioning?
hard
A. Delete the stable version from the repository to force use of feature-x.
B. Rename the library folder for each version and update the pipeline accordingly.
C. Use library('lib') without version to automatically switch between versions.
D. Specify library('lib@feature-x') in new jobs and library('lib@stable') in old jobs.
Solution
Step 1: Understand versioning usage
Jenkins library versioning lets you specify which version to use per pipeline.
Step 2: Apply versioning to different jobs
By specifying @feature-x or @stable, you control which version each job uses independently.
Final Answer:
Specify library('lib@feature-x') in new jobs and library('lib@stable') in old jobs. -> Option D
Quick Check:
Use '@version' per job to manage multiple library versions [OK]
Hint: Use '@version' per job to switch library versions easily [OK]
Common Mistakes:
Renaming folders instead of using version tags
Omitting version to auto-switch versions
Deleting stable version causing build failures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.