3

I would like to configure the buildDiscarder differently depending on a global variable. Currently I have

options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
}

but I'm looking for something like

// BROKEN
options {
    if ("${SOME_VAR}" == 'some_val') {
        buildDiscarder(logRotator(numToKeepStr: '5'))
    } else {
        buildDiscarder(logRotator(daysToKeepStr: '7'))
    }
}

Is there any way to achieve this kind of behaviour in a Jenkins declarative pipeline? I don't think I can use a script/when/expression block here, or at least it didn't work when I tried.

Sources/ideas that I've stumbled upon which didn't work: 1, 2, 3, 4, 5, 6

AdrienW
  • 3,092
  • 6
  • 29
  • 59

1 Answers1

4

Try this:

options {
    buildDiscarder(logRotator(numToKeepStr: ("${SOME_VAR}" == 'some_val') ? '5' : '7'))
}

If you want to use different arguments, just set the "unused" one to '-1':

options {
    buildDiscarder(logRotator(
            numToKeepStr: ("${SOME_VAR}" == 'some_val') ? '5' : '-1',
            daysToKeepStr: ("${SOME_VAR}" == 'some_val') ? '-1' : '7'
    ))
}
AdrienW
  • 3,092
  • 6
  • 29
  • 59
MaratC
  • 6,418
  • 2
  • 20
  • 27