如何编写管道来丢弃旧的构建?

Groovy 语法生成器对于示例步骤 properties: Set Job Properties不起作用。我已经选择了 Discard old builds,然后在 Max # of builds to keep字段中输入了 10,然后又输入了 Generate Groovy,结果什么也没有显示出来。

詹金斯版本: 2.7

90024 次浏览

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.

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.

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'))
}
  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'))
    }
    

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 ;-))

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

For Scripted Pipelines use:

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

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'))
])
}

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'
)
)
}

The following worked for me using Jenkins Configuration as Code (JCasC):

jobs: |
jobs:
- script: >
folder('Jobs')
- script: >
pipelineJob('Jobs/banana') {
logRotator(10,5,10,5)
definition {
cpsScmFlowDefinition {
scm {
gitSCM {
doGenerateSubmoduleConfigurations(false)
browser {}
gitTool(null)
userRemoteConfigs {
userRemoteConfig {
credentialsId("banana")
url('git@myrepo.git')
refspec(null)
name(null)
}
branches {
branchSpec {
name('remotes/origin/mybranch/update')
}
}
}
}
}
scriptPath('somefolder/Jenkinsfile')
lightweight(true)
}
}
}

If you're using the Jenkins Job DSL to create a job or pipelineJob, you can use any of the following formats to add the discard builds configuration to your job:

Option 1

This option modifies the XML structure of the job directly.

pipelineJob {
configure {
it / 'properties' / 'jenkins.model.BuildDiscarderProperty' {
strategy {
'daysToKeep'('7')
'numToKeep'('10')
'artifactDaysToKeep'('-1')
'artifactNumToKeep'('-1')
}
}
}
}

Option 2

pipelineJob {
logRotator(7, 10, -1, -1)
}

Option 3

pipelineJob {
logRotator {
numToKeep(10)
daysToKeep(7)
artifactNumToKeep(-1)
artifactDaysToKeep(-1)
}
}

See the following links for additional details: