Using the @Library Annotation in Jenkins Pipelines
📖 Scenario: You are working on a Jenkins pipeline project where you want to reuse shared code stored in a separate Git repository. Jenkins provides a way to include shared libraries using the @Library annotation.This project will guide you step-by-step to create a simple Jenkins pipeline script that uses the @Library annotation to load a shared library and call a function from it.
🎯 Goal: Build a Jenkins pipeline script that uses the @Library annotation to load a shared library named my-shared-lib and calls a function sayHello() from that library.
📋 What You'll Learn
Create a Jenkins pipeline script with the @Library annotation
Declare the shared library name exactly as my-shared-lib
Call the sayHello() function from the shared library
Print the output of sayHello() in the pipeline
💡 Why This Matters
🌍 Real World
In real Jenkins projects, shared libraries help teams reuse common pipeline code, reducing duplication and errors.
💼 Career
Understanding how to use the @Library annotation is essential for Jenkins pipeline developers and DevOps engineers to write modular and maintainable pipelines.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline script
Create a Jenkins pipeline script with a pipeline block and a stages section containing one stage named Example. Inside the stage, add a steps block with a placeholder comment.
Jenkins
Hint
Start by writing the basic Jenkins pipeline structure with pipeline, agent any, and one stage named Example.
2
Add the @Library annotation to load the shared library
Add the @Library annotation at the top of the script to load the shared library named my-shared-lib. Use the syntax @Library('my-shared-lib') _ exactly.
Jenkins
Hint
Place the @Library('my-shared-lib') _ annotation at the very top of the Jenkinsfile to load the shared library.
3
Call the sayHello() function from the shared library
Inside the steps block of the Example stage, add a script block. Inside the script block, call the function sayHello() and assign its result to a variable named message.
Jenkins
Hint
Use a script block inside steps to write Groovy code. Call sayHello() and store the result in message.
4
Print the message returned by sayHello()
Still inside the script block, add a println statement to print the message variable.
Jenkins
Hint
Use println(message) to print the message returned by sayHello().
Practice
(1/5)
1. What is the main purpose of the @Library annotation in Jenkins pipelines?
easy
A. To specify the pipeline agent type
B. To load shared code libraries for reuse in pipelines
C. To define environment variables for the pipeline
D. To trigger downstream jobs automatically
Solution
Step 1: Understand the role of @Library
The @Library annotation is used to load shared libraries in Jenkins pipelines.
Step 2: Identify its main purpose
Loading shared libraries allows reuse of code and better organization of pipeline scripts.
Final Answer:
To load shared code libraries for reuse in pipelines -> Option B
But Jenkins throws an error saying Missing required underscore after @Library annotation. How do you fix it?
medium
A. Change @Library to @LoadLibrary
B. Remove the parentheses from @Library
C. Place the underscore before the annotation like _ @Library('commonLib')
D. Add an underscore _ immediately after the annotation like @Library('commonLib') _
Solution
Step 1: Identify the syntax error from the message
The error says the underscore is missing after @Library, which is required syntax.
Step 2: Correct the syntax by adding underscore
Add an underscore immediately after the annotation: @Library('commonLib') _.
Final Answer:
Add an underscore _ immediately after the annotation like @Library('commonLib') _ -> Option D
Quick Check:
Underscore after @Library fixes syntax [OK]
Hint: Always put underscore right after @Library(...) [OK]
Common Mistakes:
Omitting the underscore
Placing underscore before annotation
Changing annotation name incorrectly
5. You want to load two shared libraries named 'utils' and 'helpers' in your Jenkinsfile. Which is the correct way to do this using the @Library annotation?
hard
A. @Library('utils_helpers') _
B. @Library('utils') _ @Library('helpers') _
C. @Library(['utils', 'helpers']) _
D. @Library('utils,helpers') _
Solution
Step 1: Recall how to load multiple libraries
Multiple libraries can be loaded by passing a list of names inside @Library annotation.
Step 2: Identify correct syntax for multiple libraries
The correct syntax is @Library(['utils', 'helpers']) _ which loads both libraries at once.
Final Answer:
@Library(['utils', 'helpers']) _ -> Option C
Quick Check:
Use list inside @Library for multiple libs [OK]
Hint: Use list syntax inside @Library to load many libs [OK]
Common Mistakes:
Trying to use multiple @Library annotations
Passing comma-separated string instead of list
Combining names into one string without list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.