0
0
Jenkinsdevops~10 mins

Pipeline utility functions in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Pipeline utility functions
Start Pipeline
Call Utility Function
Utility Function Executes
Return Result
Use Result in Pipeline
Continue Pipeline or End
The pipeline starts, calls a utility function, gets a result, and uses it to continue or finish.
Execution Sample
Jenkins
def greet(name) {
  return "Hello, ${name}!"
}

pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          def message = greet('World')
          echo message
        }
      }
    }
  }
}
Defines a utility function greet that returns a greeting message, then calls it in the pipeline and prints the message.
Process Table
StepActionEvaluationResult
1Pipeline startsN/APipeline initialized
2Call greet('World')Function greet called with 'World'Returns 'Hello, World!'
3Assign return to messagemessage = 'Hello, World!'message holds greeting string
4echo messagePrint message to consoleConsole shows: Hello, World!
5Pipeline continues or endsNo more stepsPipeline completes
💡 Pipeline ends after all stages complete
Status Tracker
VariableStartAfter Step 3Final
messageundefined'Hello, World!''Hello, World!'
Key Moments - 2 Insights
Why do we need to assign the function return to a variable before using it?
Because the function returns a value that must be stored to use later, as shown in step 3 where message gets the returned greeting.
What happens if the utility function is not defined before calling it?
The pipeline will fail with an error because it cannot find the function, unlike in step 2 where greet is defined and called successfully.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'message' after step 3?
Anull
Bundefined
C'Hello, World!'
D'greet'
💡 Hint
Check the variable_tracker row for 'message' after step 3
At which step does the pipeline print the greeting message?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the execution_table row where echo message is executed
If the greet function returned 'Hi, ${name}!' instead, what would be the output at step 4?
AHi, World!
BHello, World!
CHi, ${name}!
DError
💡 Hint
Consider the function return value and how it is assigned to message
Concept Snapshot
Pipeline utility functions are reusable blocks defined with def.
They return values used later in the pipeline.
Call them inside script blocks with parameters.
Assign their return to variables to use results.
Use echo to print outputs.
Functions must be defined before calling.
Full Transcript
This visual execution shows how Jenkins pipeline utility functions work. The pipeline starts and calls a utility function named greet with the argument 'World'. The function returns a greeting string 'Hello, World!'. This return value is assigned to the variable message. Then the pipeline prints the message to the console using echo. Finally, the pipeline completes after all stages run. Variables and steps are tracked to show how the function call affects pipeline state. Key moments clarify why storing function returns is necessary and the importance of defining functions before use. Quiz questions test understanding of variable values and output timing.