How to conditionally use wrapping blocks in groovy? How to conditionally use wrapping blocks in groovy? jenkins jenkins

How to conditionally use wrapping blocks in groovy?


There is no generic solution that will work for arbitrary wrappers. The if blocks you mention will be required in some form to achieve what you want.

However, you can improve matters somewhat by making the conditionals into methods.

def use_timestamps = true // or falsedef use_ansiColor = false // or truetimestampsOptional(use_timestamps) {    ansiColorOptional(use_ansiColor, 'xterm') {        sh cmd    }}def timestampsOptional(active, Closure action) {    if (active) {        timestamps action    } else {        action()    }    }def ansiColorOptional(active, encoding, Closure action) {    if (active) {        ansiColor(encoding) action    } else {        action()    }    }

The methods could then be moved into a shared library,keeping the ugliness out of your Jenkinsfile and allowing them to be reused by other Pipelines.