如何编写 Pipeline 来丢弃旧版本?

问题描述 投票:0回答:12

groovy 语法生成器不适用于示例步骤

properties: Set Job Properties
。 我选择了
Discard old builds
,然后在
10
字段中输入
Max # of builds to keep
,然后输入
Generate Groovy
,但什么也没有显示。

詹金斯版本:2.7

jenkins jenkins-pipeline
12个回答
253
投票

对于声明性语法,您可以使用

options
块:

pipeline {
  options {
    buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30'))
  }
  ...
}

logRotator
的参数(来自源代码):

  • daysToKeepStr
    :历史仅保留至今。
  • numToKeepStr
    :仅保留此数量的构建日志。
  • artifactDaysToKeepStr
    :文物仅保留至今。
  • artifactNumToKeepStr
    :只有这个数量的构建才保留其工件。

更多信息可以在Cloudbees知识库

options
块的文档中找到。


58
投票

您可以使用

properties
方法,嵌套在
BuildDiscarderProperty
中最终拥有您想要设置的密钥。 我仍然没有可靠的方法来查找每个键的正确语法。 经过多次猜测和检查:

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

请注意,此代码片段适用于脚本语法。


30
投票

对于脚本化管道使用:

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

23
投票

Jenkins 有内置语法生成器页面。

管道语法:代码片段生成器

<your jenkins url
>/管道语法/

管道语法:指令生成器

<your jenkins url
>/指令生成器/

指令生成器中的

Discard old builds
示例 discard old builds example


11
投票

对于声明式管道,您可以添加以下内容:

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

10
投票
  1. 在特定的天数后放弃构建:

     options {
         buildDiscarder(logRotator(daysToKeepStr: '7'))
     }
    
  2. 在特定数量的构建之后放弃构建:

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

6
投票

由于某种未知的原因,瓦迪姆的回答对我不起作用。我将其简化如下,现在可以使用了:

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

5
投票

如果您使用 Jenkins Job DSL 创建

job
pipelineJob
,您可以使用以下任何格式将丢弃构建配置添加到您的作业中:

选项1

此选项直接修改作业的 XML 结构。

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

选项2

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

选项3

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

请参阅以下链接了解更多详细信息:


2
投票

如果您想在多分支管道作业级别(相对于所有单独的

Jenkinsfile
)配置构建保留,这也是可能的: https://issues.jenkins-ci.org/browse/JENKINS-30519?focusedCommentId=325601&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-325601

除了

BuildRetentionBranchProperty
之外,您还可以在此处配置任何其他
*BranchProperty
https://github.com/jenkinsci/branch-api-plugin/tree/master/src/main/java/jenkins/branch

它们可能不会显示在 GUI 中,至少对于使用 Jenkins 2.73.2 的我来说是这样。但你仍然可以使用JobDSL或者直接修改

config.xml
(我没这么说;-))


2
投票

如果您需要编程方式(即从函数执行此操作,而不是使用

options{}
管道语法):

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

1
投票

使用 Jenkins 配置即代码 (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('[email protected]')
                          refspec(null)
                          name(null)
                        }
                        branches {
                          branchSpec {
                            name('remotes/origin/mybranch/update')
                          }
                        }
                     }
                  }
                }
                scriptPath('somefolder/Jenkinsfile')
                lightweight(true)
              }
            }
          }

0
投票

这适用于 FreeStyleJob

job('foo') {
    // ...
    properties {
        buildDiscarder {
            strategy {
                logRotator {
                    daysToKeepStr('')
                    numToKeepStr('10')
                    artifactDaysToKeepStr('')
                    artifactNumToKeepStr('10')
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.