How to write Pipeline to discard old builds?

前端 未结 9 792
暖寄归人
暖寄归人 2020-12-04 20:24

The groovy syntax generator is NOT working for sample step properties: Set Job Properties. I\'ve selected Discard old builds and then entered

相关标签:
9条回答
  • 2020-12-04 21:08
    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'))
       }
      
    0 讨论(0)
  • 2020-12-04 21:09

    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

    0 讨论(0)
  • 2020-12-04 21:10

    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'))
    }
    
    0 讨论(0)
  • 2020-12-04 21:15

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

    0 讨论(0)
  • 2020-12-04 21:20

    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.

    0 讨论(0)
  • 2020-12-04 21:24

    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'))
      ])
    }
    
    0 讨论(0)
提交回复
热议问题