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

10
  • 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 9, 2020 at 10:40
  • I clarify the terms. what do you mean by "these limits are per branch, not total"?
    – arielma
    Jan 9, 2020 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 9, 2020 at 10:46
  • I clarify the question. hope it's clear now
    – arielma
    Jan 9, 2020 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 9, 2020 at 11:46

2 Answers 2

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.

1
  • 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. Jan 15, 2022 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') {
        }
   
}
1
  • Correct. But I think it is better to use "properties" outside of the node {}. Dec 2, 2022 at 14:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.