The groovy syntax generator is NOT working for sample step properties: Set Job Properties. I\'ve selected Discard old builds and then entered
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'))
}
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
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'))
}
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 ;-))
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.
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'))
])
}