How do I set discard old builds for a Multi Branch Pipeline Job?

Article ID:115000237071
1 minute readKnowledge base

Issue

  • How do I set discard old builds for a Multi Branch Pipeline Job

Environment

  • Pipeline Multibranch Plugin

  • CloudBees Jenkins Enterprise

Resolution

In order to be able to configure the Discard old builds option within a Multibranch job, there are two main approaches that you might take, depending on whether you are using Scripted Pipeline Syntax or Declarative Pipeline Syntax.

Scripted Pipeline Syntax

For scripted syntax, you can take advantage of the usage of the BuildDiscarderProperty. As you can see in the pipeline syntax reference, by selecting the set job properties step as shown below.

As you can see in the image, when you click on the Generate Groovy button, you get the groovy step that you have to include in your Jenkinsfile.

properties([buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '10', numToKeepStr: '5')), [$class: 'ScannerJobProperty', doNotScan: false]])

Declarative Pipeline Syntax

In this case, you have available this setting through the options directive, which allow us to set different options such as buildDiscarder, as shown below.

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

You can find additional information on these options in the plugin documentation page