Artifact retention policies in Jenkins - Time & Space 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.
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 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.
As the number of builds grows, Jenkins must check more builds to delete old artifacts.
| Input Size (n builds) | Approx. Operations (checks) |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of builds.
Time Complexity: O(n)
This means the time to clean old artifacts grows in a straight line as the number of builds increases.
[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.
Understanding how Jenkins handles artifact cleanup helps you explain how build systems manage storage efficiently as projects grow.
"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?"