Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Pipeline to discard old builds?

The groovy syntax generator is NOT working for sample step properties: Set Job Properties. I've selected Discard old builds and then entered 10 in the Max # of builds to keep field and then Generate Groovy and nothing shows up.

Jenkins version: 2.7

like image 864
tarabyte Avatar asked Sep 17 '16 03:09

tarabyte


People also ask

How do I remove old builds in Jenkins pipeline?

Delete a Jenkins build via GUI. Go into the build you want to delete, and click the Delete this build button in the upper right corner. If you need to clean the Jenkins build history, and reset the build number back to 1, you can run a simple script in Jenkins Script Console.

How do you change the number of builds kept by Jenkins?

This is a feature that is given to you by Jenkins so you can change the number of builds you want to keep. If you are using a Declarative pipeline you can just add this to your code: pipeline { options { buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30')) } ... }

How do I delete multiple builds in Jenkins?

There's a lot you can break using the script console. Delete the builds' folders from disk (by default in $JENKINS_HOME/jobs/JobName/builds/ , using the start time stamp as folder name) and restart Jenkins, or Reload Configuration From Disk.


1 Answers

As for declarative syntax, you can use the options block:

pipeline {   options {     buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30'))   }   ... } 

Parameters for logRotator (from the source code):

  • daysToKeepStr: history is only kept up to this days.
  • numToKeepStr: only this number of build logs are kept.
  • artifactDaysToKeepStr: artifacts are only kept up to this days.
  • artifactNumToKeepStr: only this number of builds have their artifacts kept.

More information can be found in Cloudbees knowledge base and in the docs for options block.

like image 175
Vadim Kotov Avatar answered Sep 24 '22 17:09

Vadim Kotov