Loading...

Discard old builds in Jenkins

How to remove old builds as scripted pipeline in a Jenkinsfile? Not only I fail to see the sexiness in this new approach, but also I dislike the groovy syntax. In the end add I came up with several solutions to accomplish my goal: Keep only the last 10 builds. There are several ways and solutions. It was a huge try and error approach.

Set properties

Solution 1: Add before the node instruction the properties. This post help me a lot.

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

def isMaster = (env.BRANCH_NAME == 'master')
def version = null

node {
  // ..
}

Define Project Properties

Solution 2: You can also use the def declaration.

/* Only keep the 10 most recent builds. */
def projectProperties = [
    [$class: 'BuildDiscarderProperty',strategy: [$class: 'LogRotator', numToKeepStr: '10']],
]