Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a Jenkins log but keep build?

Tags:

jenkins

We have builds that generate a lot of logs. It takes space thus we have to tell Jenkins to remove old builds. It is a problem because we therefore lose the long-term statistics.

I am thinking of removing or emptying the "log" file of each build older than a week and tell Jenkins not to delete old build.

Is there a clean way of achieving the same ?

like image 610
Barth Avatar asked Jan 13 '14 08:01

Barth


People also ask

How do I clear Jenkins logs?

Go to your Jenkins home page -> Manage Jenkins -> Script Console. Run this script to clean and reset. Copy and paste this script to your Console Script text area and change the "copy_folder" to the project name that you need to clean the history. Then click the Run button.

How do I remove all Jenkins build history?

Usage. Go to a Job (Any type of) or a Folder and click on the "Purge Build History" link on the left menu. Select none or all of the options. Reset build number to 1: Resets the next build number 1 if all the builds are deleted.

Where are Jenkins build logs stored?

Note: Jenkins logs may be stored in the Jenkins installation folder in Program Files on some Windows systems. The location of the Jenkins home folder is specified during the installation. The default location is C:\ProgramData\Jenkins\. jenkins.


2 Answers

I saw there is a Delete Log Plugin but I didn't personally use it.

https://wiki.jenkins-ci.org/display/JENKINS/Delete+log+plugin

However in my work I did similar thing by just create another job, which go to the Jenkins master server, sort the builds, pick up the ones that need to be cleaned up, and remove the log file. Should be faily easy to do with any scripting language.

like image 137
Andy Chen Avatar answered Sep 25 '22 00:09

Andy Chen


Haven't found a really clean way that exists yet, but using groovy from within Jenkins is arguably cleaner than deleting from the filesystem outside of Jenkins.

This example groovy script will delete all logs from builds of the "test_dummy" job that are older than 180 days.

import jenkins.model.*;
def days = 180
def jobName = "test_dummy"
def j = Jenkins.instance.getItemByFullName(jobName);
j.getBuilds().byTimestamp(1, System.currentTimeMillis() - (1000L * 60 * 60 * 24 * days)).each { it.getLogFile().delete(); it.getLogFile().createNewFile() }

So get the job, get all the builds for it in your timestamp range, then get and delete all the log files. I'm also going and creating an empty file in their place, though I'm sure if I dug deeper I could find a way just to truncate to 0.

Using Jenkins getJobNames(), you could get all the jobs in Jenkins and then loop through them if you wanted. Depending on how you do backups, you could tie it to run before you do backups to save space.

like image 36
Caleb Avatar answered Sep 25 '22 00:09

Caleb