Search code examples
jenkinsjenkins-pipelinejenkins-groovyjenkins-job-dslmultibranch-pipeline

Multibranch Pipeline job configuration with logrotator using DSL


I'm using jenkins job dsl to configure my multibranch pipeline jobs. Actually my all settings are working except logRotator. My aim is to delete the old builds and keep a particular number of build. I can use

options {

    buildDiscarder(logRotator(numToKeepStr: '10'))
  }

in freestyle job for this purpose. The multibranch pipeline job configure section not having discardold build section as an option in UI. Is there any way I can use the logRotator without adding this to my jenkins file.


Solution

  • I've added the following section in my code to implement the buildDiscarder functionality in multibranch pipeline jobs.

     multibranchPipelineJob("job") {
      branchSources {
      branchSource {
        source {
          bitbucket {
            credentialsId("myid")
            repoOwner("iam")
            repository("job")                       
     traits {
              headWildcardFilter {
                includes("branchestoinclude")
                excludes("toexclude")
              }
            }
          }
        }
        strategy {
          defaultBranchPropertyStrategy {
            props {
              buildRetentionBranchProperty {
                buildDiscarder {
                  logRotator {
                    daysToKeepStr("-1")
                    numToKeepStr("8")
                    artifactDaysToKeepStr("-1")
                    artifactNumToKeepStr("-1")
                  }
                }
              }
            }
          }
        }
      }
    }