0
0
Jenkinsdevops~10 mins

Creating reusable pipeline steps in Jenkins - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - Creating reusable pipeline steps
Define reusable step as function
Call reusable step in pipeline
Step executes with given parameters
Pipeline continues or ends
First, define a reusable step as a function. Then call it in the pipeline with parameters. The step runs and the pipeline continues.
Execution Sample
Jenkins
def greet(name) {
  echo "Hello, ${name}!"
}

pipeline {
  agent any
  stages {
    stage('Greet') {
      steps {
        script {
          greet('Alice')
        }
      }
    }
  }
}
This pipeline defines a reusable step 'greet' and calls it with 'Alice' to print a greeting.
Process Table
StepActionEvaluationResult
1Define function greet(name)Function storedgreet available
2Start pipeline executionAgent allocatedPipeline running
3Enter stage 'Greet'Stage startedReady to run steps
4Call greet('Alice')Substitute name='Alice'echo "Hello, Alice!"
5Execute echoPrint messageConsole shows: Hello, Alice!
6Stage endsStage completePipeline continues or ends
💡 Pipeline finishes after all stages complete
Status Tracker
VariableStartAfter Step 4After Step 5Final
nameundefined'Alice''Alice''Alice'
Key Moments - 3 Insights
Why do we define the reusable step outside the pipeline block?
Defining the function outside the pipeline block makes it available to all stages and steps, as shown in Step 1 of the execution_table.
What happens if we call the reusable step with a different name?
The function uses the passed parameter each time, so the greeting changes accordingly, as seen in Step 4 where 'Alice' is substituted.
Can we reuse the step multiple times in the pipeline?
Yes, you can call the function multiple times with different parameters, each call executing the echo with the new value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4. What is the value of 'name' when greet is called?
Aundefined
B'Alice'
C'greet'
D'Hello, Alice!'
💡 Hint
Check the 'Evaluation' column in Step 4 where name='Alice' is substituted.
At which step does the message 'Hello, Alice!' appear in the console?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Result' column in Step 5 where the echo prints the message.
If we define the function inside the pipeline block, what likely happens?
APipeline runs faster
BFunction is available globally
CFunction is not recognized when called
DPipeline skips the function
💡 Hint
Refer to key_moments about function scope and Step 1 in execution_table.
Concept Snapshot
Creating reusable pipeline steps in Jenkins:
- Define a function outside the pipeline block
- Call the function inside pipeline steps
- Pass parameters to customize behavior
- Function runs and outputs results
- Reuse functions to avoid repeating code
Full Transcript
In Jenkins pipelines, you create reusable steps by defining functions outside the pipeline block. These functions take parameters and can be called inside pipeline stages. When the pipeline runs, it calls the function with given arguments, executes the code inside, and continues. This helps avoid repeating code and makes pipelines cleaner and easier to maintain.