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)