0
0
Jenkinsdevops~5 mins

Creating reusable pipeline steps in Jenkins - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
When building automation pipelines, repeating the same steps wastes time and causes errors. Reusable pipeline steps let you write a task once and use it many times, making your pipelines cleaner and easier to maintain.
When you have multiple pipelines that need to run the same tests or build commands.
When you want to share deployment steps across different projects without copying code.
When you want to update a common step in one place and have all pipelines use the new version.
When you want to simplify your Jenkinsfile by moving complex logic into reusable functions.
When you want to enforce consistent steps like code quality checks across all pipelines.
Config File - Jenkinsfile
Jenkinsfile
def call(String name) {
    echo "Hello, ${name}! This is a reusable step."
}

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    call('Developer')
                }
            }
        }
    }
}

This Jenkinsfile defines a reusable step as a function named call that takes a name and prints a greeting. The pipeline block uses this step inside the Example stage by calling call('Developer'). This shows how to write and use a simple reusable step within the same Jenkinsfile.

Commands
This command uploads or updates the Jenkinsfile in the Jenkins server to apply the reusable step pipeline.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command triggers the Jenkins pipeline named 'my-pipeline' to run the Jenkinsfile with the reusable step.
Terminal
jenkins build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Example) [Pipeline] script [Pipeline] echo Hello, Developer! This is a reusable step. [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: SUCCESS
This command shows the console output of the last run of 'my-pipeline' to verify the reusable step executed correctly.
Terminal
jenkins console my-pipeline
Expected OutputExpected
Started by user admin Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Example) [Pipeline] script [Pipeline] echo Hello, Developer! This is a reusable step. [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: reusable pipeline steps let you write a task once and call it many times to keep your pipelines simple and consistent.

Common Mistakes
Defining reusable steps outside of a function or without parameters.
This causes Jenkins to not recognize the step as reusable or flexible, limiting reuse.
Always define reusable steps as functions with parameters to customize behavior.
Copy-pasting the same code block in multiple pipeline stages instead of calling a reusable step.
This leads to duplicated code that is hard to maintain and update.
Extract common code into a reusable step function and call it where needed.
Not testing the reusable step independently before using it in pipelines.
Errors in the reusable step can cause multiple pipelines to fail.
Test reusable steps in a simple pipeline first to ensure they work correctly.
Summary
Define reusable pipeline steps as functions with parameters inside your Jenkinsfile.
Call these functions in pipeline stages to avoid repeating code.
Run and verify your pipeline to ensure the reusable steps execute as expected.