问题
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
回答1:
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.
回答2:
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.
回答3:
For Scripted Pipelines use:
properties([
buildDiscarder(logRotator(daysToKeepStr: '3', numToKeepStr: '3')),
])
回答4:
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
回答5:
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'))
}
回答6:
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')) }
回答7:
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 ;-))
回答8:
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'))
])
}
来源:https://stackoverflow.com/questions/39542485/how-to-write-pipeline-to-discard-old-builds