0
0
Jenkinsdevops~30 mins

Creating reusable pipeline steps in Jenkins - Try It Yourself

Choose your learning style9 modes available
Creating reusable pipeline steps
📖 Scenario: You are working on a Jenkins pipeline for a software project. You want to create reusable steps to avoid repeating the same code multiple times. This will make your pipeline cleaner and easier to maintain.
🎯 Goal: Build a Jenkins pipeline script that defines a reusable step called greet which prints a greeting message with a given name. Then use this step twice with different names.
📋 What You'll Learn
Define a reusable step called greet that takes a parameter name
The greet step should print 'Hello, <name>!'
Call the greet step twice with names 'Alice' and 'Bob'
Use declarative pipeline syntax
💡 Why This Matters
🌍 Real World
Reusable pipeline steps save time and reduce errors by avoiding repeated code in Jenkins pipelines.
💼 Career
DevOps engineers often create reusable pipeline steps to maintain clean, efficient CI/CD workflows.
Progress0 / 4 steps
1
Create a basic Jenkins pipeline
Write a Jenkins declarative pipeline with a pipeline block and an empty stages section.
Jenkins
Need a hint?

Start with pipeline { agent any stages { } structure.

2
Define a reusable step called greet
Inside the pipeline block but outside stages, define a def greet(name) function that prints 'Hello, <name>!'.
Jenkins
Need a hint?

Use def greet(name) { echo "Hello, ${name}!" } syntax.

3
Call the greet step twice in a stage
Add a stage named GreetUsers inside stages. Inside its steps, call greet('Alice') and greet('Bob').
Jenkins
Need a hint?

Use stage('GreetUsers') { steps { script { greet('Alice'); greet('Bob') } } }.

4
Print the pipeline output
Run the pipeline and print the output of the greet calls. The output should show greetings for Alice and Bob.
Jenkins
Need a hint?

Check the Jenkins console output for the greetings.