How to write Pipeline to discard old builds?

JenkinsJenkins Pipeline

Jenkins Problem Overview


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

Jenkins Solutions


Solution 1 - Jenkins

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.

Solution 2 - Jenkins

You can use the properties method which, nested within the BuildDiscarderProperty eventually has the key you want to set. I still don't have a solid way to look up the correct syntax of each key. After much guessing and checking:

properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '10']]]);

Note that this snippet is for scripted syntax.

Solution 3 - Jenkins

For Scripted Pipelines use:

properties([
    buildDiscarder(logRotator(daysToKeepStr: '3', numToKeepStr: '3')),
])

Solution 4 - Jenkins

Jenkins has built-in syntax generator pages.

Pipeline-Syntax: Snippet Generator
<your jenkins url>/pipeline-syntax/

Pipeline-Syntax: Directive Generator
<your jenkins url>/directive-generator/

Discard old builds example from Directive Generator discard old builds example

Solution 5 - Jenkins

  1. To Discard build after particular number of days:

     options {
         buildDiscarder(logRotator(daysToKeepStr: '7'))
     }
    
  2. To Discard build after particular number of builds:

     options {
         buildDiscarder(logRotator(numToKeepStr: '7'))
     }
    

Solution 6 - Jenkins

For declarative pipeline you can add this:

options {

    buildDiscarder(
        logRotator(
            // number of build logs to keep
            numToKeepStr:'5',
            // history to keep in days
            daysToKeepStr: '15',
            // artifacts are kept for days
            artifactDaysToKeepStr: '15',
            // number of builds have their artifacts kept
            artifactNumToKeepStr: '5'
        )
    )
}

Solution 7 - Jenkins

Vadim's answer did not work for me for some unknown reason. I simplified it down as follows and it works now:

options {
    buildDiscarder(logRotator(numToKeepStr: '3'))
}

Solution 8 - Jenkins

If you want to configure the build retention on the multibranch pipeline job level (vs in all the individual Jenkinsfiles) this is possible too: https://issues.jenkins-ci.org/browse/JENKINS-30519?focusedCommentId=325601&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-325601

In addition to the BuildRetentionBranchProperty you can configure any other of the *BranchPropertys in here: https://github.com/jenkinsci/branch-api-plugin/tree/master/src/main/java/jenkins/branch

They might not be shown in the GUI though, at least for me with Jenkins 2.73.2. But you can still use JobDSL or modify the config.xml directly (I didn't say that ;-))

Solution 9 - Jenkins

If you need a programmatic way (i.e. doing this from a function, rather than using options{} pipeline syntax):

def someFunction() {
  ...
  properties([
    buildDiscarder(logRotator(numToKeepStr: '5'))
  ])
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontarabyteView Question on Stackoverflow
Solution 1 - JenkinsVadim KotovView Answer on Stackoverflow
Solution 2 - JenkinstarabyteView Answer on Stackoverflow
Solution 3 - JenkinsStasKolodyukView Answer on Stackoverflow
Solution 4 - JenkinsChad GilmanView Answer on Stackoverflow
Solution 5 - JenkinsJerald Sabu MView Answer on Stackoverflow
Solution 6 - JenkinsbhordupurView Answer on Stackoverflow
Solution 7 - JenkinsFirdausView Answer on Stackoverflow
Solution 8 - JenkinsTorben KnerrView Answer on Stackoverflow
Solution 9 - JenkinsDavid LavenderView Answer on Stackoverflow