📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process. To keep your pipeline clean and reusable, you want to define a Groovy method inside the pipeline script that prints a welcome message with the project name.
🎯 Goal: Build a Jenkins pipeline script that defines a Groovy method called printWelcome which takes a project name as input and prints a welcome message. Then call this method in the pipeline to display the message.
📋 What You'll Learn
Create a Groovy method named printWelcome that accepts one parameter projectName
Inside the method, print the message: "Welcome to the project: <projectName>"
Call the printWelcome method inside the pipeline script with the project name "MyApp"
Use a pipeline block with agent any and a stage named "Greeting"
Print the output to the Jenkins console log
💡 Why This Matters
🌍 Real World
In real Jenkins pipelines, defining Groovy methods helps organize repeated tasks like notifications, validations, or formatting logs.
💼 Career
Knowing how to write and call Groovy methods in Jenkins pipelines is essential for DevOps engineers to create maintainable and scalable CI/CD workflows.
Progress0 / 4 steps
1
Create the Groovy method printWelcome
Define a Groovy method called printWelcome that takes one parameter named projectName. Inside the method, use echo to print the message "Welcome to the project: <projectName>".
Jenkins
Hint
Use def to define the method and echo to print inside Jenkins pipeline.
2
Set up the Jenkins pipeline skeleton
Create a pipeline block with agent any and a stage named "Greeting". Inside the stage, add a steps block.
Jenkins
Hint
Use the pipeline syntax with agent any and define a stage called Greeting.
3
Call the printWelcome method inside the pipeline
Inside the steps block of the Greeting stage, call the printWelcome method with the argument "MyApp".
Jenkins
Hint
Use a script block inside steps to call the Groovy method.
4
Run the pipeline and display the output
Run the pipeline script. The Jenkins console log should display the message Welcome to the project: MyApp. Add a print statement if needed to show the output.
Jenkins
Hint
Check the Jenkins console output after running the pipeline to see the welcome message.
Practice
(1/5)
1. What is the correct way to define a reusable method in a Jenkins pipeline using Groovy?
easy
A. Use def methodName() { } inside a script block
B. Use function methodName() { } outside the pipeline
C. Use method methodName() { } inside steps
D. Use def methodName[] { } inside environment
Solution
Step 1: Understand Groovy method syntax in Jenkins
Groovy methods are defined with def methodName() { } syntax inside script blocks in Jenkins pipelines.
Step 2: Identify correct placement
Methods must be inside script blocks to be recognized and used in declarative pipelines.
Final Answer:
Use def methodName() { } inside a script block -> Option A
A. Method compile is called without parentheses causing no execution
B. Method compile is not defined properly with def
C. echo cannot be used inside methods
D. script block cannot contain method definitions
Solution
Step 1: Check method call syntax
The method compile is defined correctly but called without parentheses, so it is treated as a reference, not executed.
Step 2: Understand Groovy method invocation
To execute the method, parentheses are required: compile().
Final Answer:
Method compile is called without parentheses causing no execution -> Option A
Quick Check:
Call methods with () to execute [OK]
Hint: Always add () when calling methods to run them [OK]
Common Mistakes:
Calling method without parentheses
Thinking def is missing for method
Assuming echo can't be inside methods
Believing script block forbids method definitions
5. You want to create a Groovy method in a Jenkins pipeline that takes a list of strings and returns a single string with all items joined by commas. Which method implementation is correct?
hard
A. def joinList(items) { return items + "," }
B. def joinList(items) { items.join }
C. def joinList(items) { return items.join(",") }
D. def joinList(items) { items.collect { it + "," } }
Solution
Step 1: Understand Groovy list joining
The join method on a list combines elements into a string separated by the given delimiter.
Step 2: Check method correctness
def joinList(items) { return items.join(",") } correctly returns items.join(",") which joins list items with commas.
Final Answer:
def joinList(items) { return items.join(",") } -> Option C
Quick Check:
Use list.join(",") to combine items [OK]
Hint: Use list.join(",") to join list elements into string [OK]
Common Mistakes:
Forgetting to return the joined string
Using + operator which concatenates incorrectly
Using collect which returns a list, not string
Omitting parentheses in join method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.