0
0
Jenkinsdevops~30 mins

Disk space management in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Disk space management
📖 Scenario: You are a Jenkins administrator. Your Jenkins server stores many build artifacts and logs. Over time, disk space fills up, causing build failures. You want to manage disk space by cleaning old builds automatically.
🎯 Goal: Build a Jenkins pipeline script that defines a workspace cleanup strategy. You will create a list of build directories, set a threshold for maximum builds to keep, delete older builds beyond the threshold, and print the cleanup result.
📋 What You'll Learn
Create a list of build directories as strings
Define a maximum number of builds to keep as an integer
Write a loop to delete builds older than the threshold
Print the list of builds remaining after cleanup
💡 Why This Matters
🌍 Real World
Jenkins servers accumulate many build artifacts and logs. Managing disk space by cleaning old builds prevents failures and keeps the server healthy.
💼 Career
DevOps engineers and Jenkins administrators often write scripts to automate disk space management and maintain continuous integration environments.
Progress0 / 4 steps
1
Create the list of build directories
Create a variable called buildDirs as a list containing these exact strings: "build_1", "build_2", "build_3", "build_4", "build_5".
Jenkins
Need a hint?

Use Groovy list syntax with square brackets and double quotes for strings.

2
Define the maximum builds to keep
Create a variable called maxBuilds and set it to the integer 3.
Jenkins
Need a hint?

Use def maxBuilds = 3 to define the integer variable.

3
Delete older builds beyond the threshold
Write a while loop that runs while the size of buildDirs is greater than maxBuilds. Inside the loop, remove the first element from buildDirs using remove(0).
Jenkins
Need a hint?

Use while (buildDirs.size() > maxBuilds) and inside remove the oldest build with remove(0).

4
Print the remaining builds
Write a println statement to display the text "Remaining builds:" followed by the buildDirs list.
Jenkins
Need a hint?

Use println("Remaining builds: ${buildDirs}") to show the final list.