0
0
Jenkinsdevops~10 mins

Groovy methods in pipelines in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Groovy methods in pipelines
Define method
Call method in pipeline
Method executes code block
Return result or perform action
Pipeline continues with method output
This flow shows how a Groovy method is defined and then called inside a Jenkins pipeline, executing its code and returning results before the pipeline continues.
Execution Sample
Jenkins
def greet(name) {
  echo "Hello, ${name}!"
}

greet('Alice')
Defines a method greet that prints a hello message, then calls it with 'Alice'.
Process Table
StepActionEvaluationResult
1Define method greet(name)Store method greet with parameter nameMethod greet ready to use
2Call greet('Alice')Invoke greet with name='Alice'Method greet starts execution
3Execute echo "Hello, ${name}!"Substitute name with 'Alice'Prints: Hello, Alice!
4Method greet endsNo return valueReturns null implicitly
5Pipeline continuesNo errorsPipeline proceeds after method call
💡 Method greet completes and pipeline continues normally
Status Tracker
VariableStartDuring greet('Alice')After greet
nameundefined'Alice'undefined (local to method)
Key Moments - 3 Insights
Why does the variable 'name' only have a value during the method call?
Because 'name' is a parameter local to the method greet, it only exists during the method execution as shown in execution_table step 3 and variable_tracker.
What happens if the method does not explicitly return a value?
Groovy methods return null by default if no return statement is given, as shown in execution_table step 4.
Can the method be called multiple times with different arguments?
Yes, each call passes its own argument to 'name', running the method code independently each time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when greet('Alice') is called?
AHello, Alice!
BHello, name!
Cgreet method called
DNo output
💡 Hint
See execution_table step 3 where echo prints the message with 'Alice' substituted
At which step does the method greet finish execution?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check execution_table step 4 where method ends and returns
If you call greet('Bob') after greet('Alice'), what changes in variable_tracker?
A'name' keeps value 'Alice'
B'name' changes to 'Bob' during the second call
C'name' becomes global
DNo change in 'name'
💡 Hint
Variable 'name' is local and changes per call as shown in variable_tracker
Concept Snapshot
Groovy methods in Jenkins pipelines:
- Define with def methodName(params) { code }
- Call method by name with arguments
- Parameters are local variables
- Methods can print, return values, or perform actions
- No explicit return means null is returned
- Methods help reuse code inside pipelines
Full Transcript
This visual execution shows how Groovy methods work inside Jenkins pipelines. First, a method named greet is defined with one parameter called name. When the pipeline calls greet('Alice'), the method runs and prints 'Hello, Alice!'. The parameter 'name' holds 'Alice' only during the method execution and disappears after. If the method does not return a value explicitly, it returns null by default. The pipeline then continues normally after the method call. This helps organize and reuse code in Jenkins pipelines.