7

I have multi-branch pipeline job in Jenkins:

http://illinXXXX:XXXX/job/OC/configure

and I checked the option for Discard old builds as below:

Discard old builds

I would have expected, that each new run after this change, it will delete old build from the server for each Repository inside that pipeline. however, I see all builds are still there, which causing me a File system issue. Jenkins link:

http://illinXXX:XXXX/job/OC/job/productconfigurator-ms/job/master/

From server:

jenkins@XXXXX:jenkins/jenkins-production/jobs/OC/jobs/productconfigurator-ms/branches/master/builds>

I see builds from 541 to 1039

Jenkins ver. 2.176.1

arielma
  • 1,308
  • 1
  • 11
  • 29
  • What's "MS" and "FS"? What exactly is "multi pipeline job"? If you mean Multibranch Pipeline, then I think these limits are per branch, not total. "Discard old builds" works perfectly here. – MaratC Jan 09 '20 at 10:40
  • I clarify the terms. what do you mean by "these limits are per branch, not total"? – arielma Jan 09 '20 at 10:43
  • In Multibranch Pipeline with the above configuration, there would be up to 100 jobs up to 100 days old for branch "X", up to 100 jobs up to 100 days old for branch "Y" etc. What's Microservice? If you somehow mean docker images/containers/the likes, then no, Jenkins doesn't know anything about these. – MaratC Jan 09 '20 at 10:46
  • I clarify the question. hope it's clear now – arielma Jan 09 '20 at 11:12
  • If you browse to `http://illinXXX:XXXX/job/OC/job/productconfigurator-ms/job/master/configure`, do you see a checkbox "Discard old builds" checked with the parameters in place? – MaratC Jan 09 '20 at 11:46
  • as it's multi-branch pipeline, under http://illinXXX:XXXX/job/OC/job/productconfigurator-ms/job/master/configure I have only the option for 'View Configuration', and there it's not checked... – arielma Jan 09 '20 at 12:08
  • Then your configuration hasn't come into effect. – MaratC Jan 09 '20 at 12:09
  • exactly, but why? I added PrtScn to the question, where you can clearly see its define – arielma Jan 09 '20 at 12:19
  • This is for the deleted branches, not for the active ones. There would be up to 100 jobs up to 100 days old for branch "X" once you delete that branch. – MaratC Jan 09 '20 at 12:24
  • so it means I need to delete old build manually from the server? – arielma Jan 09 '20 at 13:09

2 Answers2

17

The interface you pasted is for the Orphaned items. Orphaned items refer to deleted branches, where no Jenkinsfile is available.

For the Multibranch pipeline, the instructions to build each branch are inside that branch's Jenkinsfile. This is where you need to define these limits.

Use the following in your Jenkinsfile (from above, in master branch):

options {
        buildDiscarder(logRotator(numToKeepStr: "100"))
}

Make sure to use string (as in "100") and not number (as in 100).

Parameters:

  • daysToKeepStr: history is only kept up to this many days.
  • numToKeepStr: only this many build logs are kept.
  • artifactDaysToKeepStr: artifacts are only kept up to this many days.
  • artifactNumToKeepStr: only this many builds have their artifacts kept.

You may need to run your master pipeline manually once for it to work.

MaratC
  • 6,418
  • 2
  • 20
  • 27
  • This is the right answer, but there are two big caveats: 1. "options" must be specified in their entirety. E.g. If you want to prevent a build from resuming, you must include THAT option here along with all others. 2. It's actually the `properties` step when using a scripted pipeline. In a scripted pipeline you can change e.g. the numToKeepStr based on whether it's a master branch build or not. Not clear that works well in declarative syntax. Final caveat: what is available depends on what plugins are installed making it painful to try to boilerplate. – Steven the Easily Amused Jan 15 '22 at 17:07
5

This is the equivalent of scripted-pipeline:

node('some-label') {
        properties([
                buildDiscarder(
                        logRotator(
                                artifactDaysToKeepStr: "10",
                                artifactNumToKeepStr: "50",
                                daysToKeepStr: "10",
                                numToKeepStr: "50")
                )
        ])

        stage('Maven Compile') {
        }

        stage('Some other steps') {
        }
   
}
xbmono
  • 2,084
  • 2
  • 30
  • 50