[JIRA] (JENKINS-47717) Allow different "Discard Old Build" options per branch on multibranch pipelines

6 views
Skip to first unread message

james@time4tea.net (JIRA)

unread,
Mar 23, 2018, 5:49:02 AM3/23/18
to jenkinsc...@googlegroups.com
James Richardson commented on Improvement JENKINS-47717
 
Re: Allow different "Discard Old Build" options per branch on multibranch pipelines

buildDiscarder(logRotator(numToKeepStr: env.BRANCH_NAME == 'master' ? '10' : '25'))

 

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

muntyanu.roman@gmail.com (JIRA)

unread,
Jul 25, 2019, 5:45:02 PM7/25/19
to jenkinsc...@googlegroups.com

A more sophisticated example of a per-branch clean-up policy. Multibranch Pipeline. Declarative build.jenkinsfile.

The policy:

| Branch name | # Builds | # Artifacts |
|-------------|----------|-------------|
| master      |      100 |           1 |
| test        |       10 |           1 |
| production  |       10 |           5 |
| feature/*   |        5 |           1 |
| bug/*       |        5 |           1 |
| <other>     |        1 |           0 |

The solution:

Unable to find source-code formatter for language: gradle. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
pipeline {

    options {
        buildDiscarder(logRotator(
            // number of builds to keep
            numToKeepStr:         env.BRANCH_NAME ==~ /master/ ? '100' :
                                  env.BRANCH_NAME ==~ /production|test/ ?  '10' :
                                  env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '5' : '1',
            // number of builds to keep the artifacts from
            artifactNumToKeepStr: env.BRANCH_NAME ==~ /master|test/ ? '1' :
                                  env.BRANCH_NAME ==~ /production/ ?  '5' :
                                  env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '1' : '0'
        ))
    }

.....
}
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

muntyanu.roman@gmail.com (JIRA)

unread,
Jul 25, 2019, 5:45:04 PM7/25/19
to jenkinsc...@googlegroups.com
Roman Muntyanu edited a comment on Improvement JENKINS-47717
A more sophisticated example of a per-branch clean-up policy.  Multibranch Pipeline. Declarative build.jenkinsfile.

The policy:
{code:none}

| Branch name | # Builds | # Artifacts |
|-------------|----------|-------------|
| master      |      100 |           1 |
| test        |       10 |           1 |
| production  |       10 |           5 |
| feature/*   |        5 |           1 |
| bug/*       |        5 |           1 |
| <other>     |        1 |           0 |
{code}

The solution:
{code:
gradle groovy }

pipeline {

    options {
        buildDiscarder(logRotator(
            // number of builds to keep
            numToKeepStr:         env.BRANCH_NAME ==~ /master/ ? '100' :
                     env.BRANCH_NAME ==~ /production|test/ ?  '10' :
                     env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '5' : '1',
            // number of builds to keep the artifacts from
            artifactNumToKeepStr: env.BRANCH_NAME ==~ /master|test/ ? '1' :
                     env.BRANCH_NAME ==~ /production/ ?  '5' :
                     env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '1' : '0'
        ))
    }

.....
}
{code}

muntyanu.roman@gmail.com (JIRA)

unread,
Jul 25, 2019, 5:46:05 PM7/25/19
to jenkinsc...@googlegroups.com
Roman Muntyanu edited a comment on Improvement JENKINS-47717
A more sophisticated example of a per-branch clean-up policy workaround .  Multibranch Pipeline. Declarative build.jenkinsfile.


The policy:
{code:none}
| Branch name | # Builds | # Artifacts |
|-------------|----------|-------------|
| master      |      100 |           1 |
| test        |       10 |           1 |
| production  |       10 |           5 |
| feature/*   |        5 |           1 |
| bug/*       |        5 |           1 |
| <other>     |        1 |           0 |
{code}

The solution:
{code:groovy}

pipeline {

    options {
        buildDiscarder(logRotator(
            // number of builds to keep
            numToKeepStr:         env.BRANCH_NAME ==~ /master/ ? '100' :
                     env.BRANCH_NAME ==~ /production|test/ ?  '10' :
                     env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '5' : '1',
            // number of builds to keep the artifacts from
            artifactNumToKeepStr: env.BRANCH_NAME ==~ /master|test/ ? '1' :
                     env.BRANCH_NAME ==~ /production/ ?  '5' :
                     env.BRANCH_NAME ==~ /feature\/.*|bug\/.*/ ? '1' : '0'
        ))
    }

.....
}
{code}

muntyanu.roman@gmail.com (JIRA)

unread,
Jul 25, 2019, 5:46:06 PM7/25/19
to jenkinsc...@googlegroups.com
Roman Muntyanu edited a comment on Improvement JENKINS-47717
A more sophisticated example of a per-branch clean-up policy workaround (thank you, [~richajam]!) .  Multibranch Pipeline. Declarative build.jenkinsfile.

roe.p@outlook.com (JIRA)

unread,
Oct 17, 2019, 10:03:03 AM10/17/19
to jenkinsc...@googlegroups.com

This solution/workaround doesn't seems to work.

Let's say I have configured the pipeline to use:

buildDiscarder(logRotator(numToKeepStr: env.BRANCH_NAME == 'master' ? '5' : '1'))

This will be as an example build (branch) history:

#1 master  #2 master  #3 master #4 master

#5 master #6 master #7 feature #8 feature

In this case according to the explanation of the policy when I build the master branch 5 times it will save them and will drop build#1 when I start the 6 build. If I build #7 as non master branch it will keep all the builds (#2-6) and on #8 it will remove #7 only.

 

However this is not the case, what really happens is that on build #7 it's evaluating the condition and now the value of the buildDiscarder is 1 so its removing all the builds and keeping #7 only regardless of the branch name (it doesn't evaluate each build with the corresponding branch name)

This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo

Kalle.Niemitalo@procomp.fi (JIRA)

unread,
Oct 17, 2019, 10:10:04 AM10/17/19
to jenkinsc...@googlegroups.com

roe_p pinhas, AFAIK, it works OK in a Multibranch Pipeline, where each branch becomes a nested project that discards its builds independently from the other branches.

Reply all
Reply to author
Forward
0 new messages