0
0
Jenkinsdevops~5 mins

Artifact retention policies in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Artifact retention policies
O(n)
Understanding Time Complexity

When Jenkins keeps build artifacts, it uses rules to decide how many to keep.

We want to know how the time to clean old artifacts grows as the number of builds increases.

Scenario Under Consideration

Analyze the time complexity of the following Jenkins pipeline snippet that deletes old artifacts.


    pipeline {
      agent any
      options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
      }
      stages {
        stage('Build') {
          steps {
            echo 'Building...'
          }
        }
      }
    }
    

This code keeps only the latest 10 builds' artifacts and deletes older ones automatically.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Jenkins scans all previous builds to find which artifacts to delete.
  • How many times: It checks each build once to decide if it is older than the limit.
How Execution Grows With Input

As the number of builds grows, Jenkins must check more builds to delete old artifacts.

Input Size (n builds)Approx. Operations (checks)
1010 checks
100100 checks
10001000 checks

Pattern observation: The number of checks grows directly with the number of builds.

Final Time Complexity

Time Complexity: O(n)

This means the time to clean old artifacts grows in a straight line as the number of builds increases.

Common Mistake

[X] Wrong: "Deleting old artifacts happens instantly no matter how many builds exist."

[OK] Correct: Jenkins must check each build to decide if its artifacts should be deleted, so more builds mean more work.

Interview Connect

Understanding how Jenkins handles artifact cleanup helps you explain how build systems manage storage efficiently as projects grow.

Self-Check

"What if the retention policy kept artifacts based on time (e.g., last 30 days) instead of number of builds? How would the time complexity change?"