0
0
Jenkinsdevops~10 mins

Disk space management in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Disk space management
Start Jenkins Job
Check Disk Space
Is Disk Space < Threshold?
NoContinue Job
Yes
Clean Workspace and Old Builds
Re-check Disk Space
Enough Space?
NoFail Job or Alert Admin
Yes
Continue Job Execution
This flow shows how Jenkins checks disk space before running jobs, cleans up if needed, and decides whether to continue or stop.
Execution Sample
Jenkins
node {
  def freeSpaceStr = sh(script: "df --output=avail -B1 ${WORKSPACE} | tail -1", returnStdout: true).trim()
  def freeSpace = freeSpaceStr.toLong()
  if (freeSpace < 5000000000L) {
    cleanWs()
    freeSpaceStr = sh(script: "df --output=avail -B1 ${WORKSPACE} | tail -1", returnStdout: true).trim()
    freeSpace = freeSpaceStr.toLong()
  }
  echo "Free space: ${freeSpace}"
}
This Jenkins pipeline script checks free disk space, cleans workspace if below 5GB, then prints free space.
Process Table
StepActionDisk Space (bytes)ConditionResult
1Start job and check disk space40000000004000000000 < 5000000000?True - proceed to cleanup
2Run cleanWs() to clean workspace7000000000N/AWorkspace cleaned, disk space increased
3Print free space after cleanup70000000007000000000 < 5000000000?False - enough space, continue job
4Job continues execution7000000000N/AJob runs successfully
💡 Disk space after cleanup is sufficient, job continues normally.
Status Tracker
VariableStartAfter Step 1After Step 2Final
freeSpaceN/A400000000070000000007000000000
Key Moments - 3 Insights
Why do we check disk space before cleaning the workspace?
Because if disk space is already enough (see Step 1 in execution_table), cleaning is unnecessary and saves time.
How does cleanWs() affect disk space?
cleanWs() deletes old files and builds, increasing free disk space as shown from 4GB to 7GB in Step 2.
What happens if disk space is still low after cleanup?
The job should fail or alert admin to prevent running with insufficient space, but in this example, space is enough after cleanup.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the disk space value after running cleanWs()?
A7000000000
B5000000000
C4000000000
D3000000000
💡 Hint
Check Step 2 in the execution_table under 'Disk Space (bytes)' column.
At which step does the condition 'freeSpace < 5000000000' become false?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' column in execution_table for each step.
If the initial disk space was 6000000000 bytes, what would happen at Step 1?
ACleanup would run
BJob would continue without cleanup
CJob would fail immediately
DDisk space would decrease
💡 Hint
Refer to the condition check in Step 1 of execution_table.
Concept Snapshot
Jenkins Disk Space Management:
- Check disk space before job runs
- If below threshold, run cleanWs() to free space
- Re-check space; if enough, continue job
- If still low, fail job or alert
- Helps prevent job failures due to disk full
Full Transcript
This visual execution shows how Jenkins manages disk space during job runs. First, it checks the available disk space. If the space is less than 5GB, it cleans the workspace using cleanWs() to free up space. After cleanup, it checks disk space again. If the space is sufficient, the job continues normally. If not, the job should fail or alert an administrator. This process helps avoid job failures caused by insufficient disk space.