Discard old build in multi-branch pipeline job, doesn't really delete builds from server
Asked Answered
R

2

8

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

Rivalee answered 9/1, 2020 at 9:56 Comment(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.Agility
I clarify the terms. what do you mean by "these limits are per branch, not total"?Rivalee
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.Agility
I clarify the question. hope it's clear nowRivalee
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?Agility
as it's multi-branch pipeline, under illinXXX:XXXX/job/OC/job/productconfigurator-ms/job/master/… I have only the option for 'View Configuration', and there it's not checked...Rivalee
Then your configuration hasn't come into effect.Agility
exactly, but why? I added PrtScn to the question, where you can clearly see its defineRivalee
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.Agility
so it means I need to delete old build manually from the server?Rivalee
A
19

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.

Agility answered 9/1, 2020 at 16:44 Comment(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.Extravagancy
S
7

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') {
        }
   
}
Sitter answered 22/4, 2021 at 0:31 Comment(1)
Correct. But I think it is better to use "properties" outside of the node {}.Vizzone

© 2022 - 2024 — McMap. All rights reserved.