How to write Pipeline to discard old builds?
JenkinsJenkins PipelineJenkins 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/
Solution 5 - Jenkins
-
To Discard build after particular number of days:
options { buildDiscarder(logRotator(daysToKeepStr: '7')) }
-
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 Jenkinsfile
s) 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 *BranchProperty
s 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'))
])
}