0
0
Jenkinsdevops~5 mins

currentBuild variables in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: currentBuild variables
O(n)
Understanding Time Complexity

We want to understand how using currentBuild variables affects the time it takes for a Jenkins pipeline to run.

Specifically, how does accessing or updating these variables change the work Jenkins does as the pipeline grows?

Scenario Under Consideration

Analyze the time complexity of the following Jenkins pipeline snippet.


pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          for (int i = 0; i < n; i++) {
            currentBuild.description = "Build step " + i
          }
        }
      }
    }
  }
}
    

This code updates the currentBuild.description variable inside a loop that runs n times.

Identify Repeating Operations

Look for repeated actions in the code.

  • Primary operation: Updating currentBuild.description inside the loop.
  • How many times: The loop runs n times, so the update happens n times.
How Execution Grows With Input

As n grows, the number of updates grows the same way.

Input Size (n)Approx. Operations
1010 updates to currentBuild.description
100100 updates to currentBuild.description
10001000 updates to currentBuild.description

Pattern observation: The work grows directly with the number of loop iterations.

Final Time Complexity

Time Complexity: O(n)

This means the time to update currentBuild.description grows linearly as the number of updates increases.

Common Mistake

[X] Wrong: "Updating currentBuild variables inside a loop is instant and does not add time."

[OK] Correct: Each update triggers Jenkins to process and save the change, so doing it many times adds up and takes longer.

Interview Connect

Understanding how repeated updates to build variables affect pipeline time helps you write efficient Jenkins pipelines and shows you think about scaling your automation.

Self-Check

What if we updated currentBuild.description only once after the loop instead of inside it? How would the time complexity change?