0
0
Jenkinsdevops~5 mins

Creating reusable pipeline steps in Jenkins - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: Creating reusable pipeline steps
O(n)
Understanding Time Complexity

When we create reusable steps in Jenkins pipelines, we want to know how the time to run these steps changes as we use them more or with bigger inputs.

We ask: How does the execution time grow when we call these reusable steps multiple times or with larger data?

Scenario Under Consideration

Analyze the time complexity of the following Jenkins pipeline snippet.

def reusableStep(param) {
  echo "Processing ${param}"
  for (int i = 0; i < param; i++) {
    echo "Step iteration: ${i}"
  }
}

pipeline {
  agent any
  stages {
    stage('Run Steps') {
      steps {
        script {
          reusableStep(5)
          reusableStep(10)
        }
      }
    }
  }
}

This code defines a reusable step that runs a loop based on the input parameter, then calls it twice with different sizes.

Identify Repeating Operations

Look for loops or repeated calls that affect execution time.

  • Primary operation: The for-loop inside reusableStep that runs param times.
  • How many times: The reusable step is called twice, once with 5 and once with 10, so loops run 5 and 10 times respectively.
How Execution Grows With Input

The time to run each reusable step grows directly with the input number because the loop runs that many times.

Input Size (param)Approx. Operations (loop iterations)
55
1010
100100

Pattern observation: If you double the input, the loop runs twice as many times, so execution time grows linearly.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the reusable step grows in a straight line with the input size.

Common Mistake

[X] Wrong: "Calling the reusable step twice means the time complexity is squared, like O(n²)."

[OK] Correct: Each call runs independently, so total time adds up, not multiplies. Two calls with inputs n and m take O(n + m), not O(n * m).

Interview Connect

Understanding how reusable steps scale helps you design pipelines that stay fast as projects grow. This skill shows you can think about efficiency, not just correctness.

Self-Check

"What if the reusable step called itself recursively instead of using a loop? How would the time complexity change?"