0
0
Jenkinsdevops~3 mins

Creating reusable pipeline steps in Jenkins - Why You Should Know This

Choose your learning style9 modes available
The Big Idea

What if you could fix a bug in all your pipelines by changing just one line of code?

The Scenario

Imagine you have many projects, each with its own build process. You write the same steps over and over in each project's pipeline script.

Every time you want to change a step, you must update all scripts manually.

The Problem

This manual way is slow and boring. It's easy to forget one script and cause errors.

It wastes time and makes your work stressful because you repeat the same code everywhere.

The Solution

Creating reusable pipeline steps means writing a step once and using it everywhere.

This saves time, reduces mistakes, and makes your pipelines cleaner and easier to update.

Before vs After
Before
stage('Build') {
  steps {
    sh 'npm install'
    sh 'npm test'
  }
}

stage('Build') {
  steps {
    sh 'npm install'
    sh 'npm test'
  }
}
After
def buildStep() {
  sh 'npm install'
  sh 'npm test'
}

stage('Build') {
  steps {
    script {
      buildStep()
    }
  }
}

stage('Test') {
  steps {
    script {
      buildStep()
    }
  }
}
What It Enables

You can update one step and instantly improve all pipelines that use it.

Real Life Example

A company has 10 microservices, each with its own Jenkins pipeline. By creating reusable steps, they fix a bug in the build process once, and all services benefit immediately.

Key Takeaways

Manual repetition wastes time and causes errors.

Reusable steps let you write once, use everywhere.

Updating one step updates all pipelines instantly.