Clean up Jenkins Workspaces

Problem :

When you use Jenkins for continuous integration, you can quickly have disk usage problems on slave nodes and sometimes with the master node too. Disk usage problems happen when you have many jobs . For example one of our jobs is taking up to 2GB. Therefore where Jenkins sits we might have disk usage problems.

Delete Workspace when build is done

For my projects I use often this option to delete workspace after build :https://wiki.jenkins-ci.org/display/JENKINS/Workspace+Cleanup+Plugin

Disadvantage :

After build, sometimes it is useful to keep the workspace  in order to understand failures. You can always decide to suppress this option temporarily if there is a problem.

Clean up Slaves Workspaces

Slaves workspaces are not deleted by this method. Therefore i use as well a script to delete slave workspaces :

https://gist.github.com/rb2k/8372402

To execute the script on the master node, go to “manage Jenkins” -> “ Console  Script”.

Comment the line “workspacePath.deleteRecursive()” if you want to verify which folders are going to be deleted  .

Write a clean up job for Jenkins :

It is a good practice to clean up Jenkins’s Workspaces.https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Best+Practices

Write jobs for your maintenance tasks, such as cleanup operations to avoid full disk problems.

upload_plugin_jenkins
Go to “Manage Jenkins”-> “Manage Plugin”.After reboot of Jenkins the plugin can be used.
  • Secondly set up and configure Groovy. Go to Manage Jenkins -> Configure System where you can configure Groovy. Then start to follow the steps indicated on the wiki of the Groovy  plugin(previous link).

Few tips how I installed Groovy when following the steps of the wiki

Use repo URL by extracting it from zip file :

http://stackoverflow.com/questions/29931091/upgrade-groovy-installation-on-slave-node-to-a-recent-version

Choose a label for the Groovy installation. For example it can be Groovy.

Download Url for binary archive :
https://bintray.com/artifact/download/groovy/maven/groovy-binary-2.3.11.zip

  • Then you can just create your Jenkins job to clean up daily workspaces. In configuration of the job you choose “Execute System Groovy script”.
groovy_jobv2y
Put the previous command from gist.github here
  • This Jenkins job saved me lot of time ! Since then i did not have disk problem ! I don’t need anymore to clean up the disk manually.

Clean up Master Workspace

It happens from time to time that the master node is full. It is necessary to clean up the workspace to launch jobs. In order to do so go to the console of the master node first.

Then choose the option ‘Script Console’ in the master node. Then i simply look for jobs which are taking the most of space.

In order to do so first find the directory of JENKINS_HOME  with this command for example :

println "env".execute().text

Find out which jobs are taking the most spaces with :

println "du -mh [JENKINS_HOME]/jobs".execute().text

Suppress the directory taking the most space(you need admin rights)

Example ::

println "rm -rf   [JENKINS_HOME]/jobs/[my_job_to_clean_up]/builds".execute().text

Link to the book “The Pragmatic Programmer” on Amazon

The Pragmatic Programmer: From Journeyman to Master

I advised this book because it helped me to understand the big picture of informatics : https://julienprog.wordpress.com/2015/03/14/review-book-pragmatic-programmer/

Other commands useful

Check where is located the path of the slaves directory  :

http://stackoverflow.com/questions/11387762/how-to-trigger-manual-clean-of-hudson-workspaces

def hi = hudson.model.Hudson.instance
hi.getItems(hudson.model.Job).each {
job ->
println(job.displayName)
println(job.isDisabled())
println(job.workspace)
}

Delete unused jobs :

https://gist.github.com/ceilfors/1400fd590632db1f51ca

How to create groovy script which clean up workspaces :

http://stackoverflow.com/questions/11387762/how-to-trigger-manual-clean-of-hudson-workspaces

List of groovy scripts for Jenkins

https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05

Wipe out workspaces for a specific job :

https://wiki.jenkins-ci.org/display/JENKINS/Wipe+workspaces+for+a+set+of+jobs+on+all+nodes

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s