Home > Back-end >  How to use conditional Statements in Jenkins 'Options' block
How to use conditional Statements in Jenkins 'Options' block

Time:06-25

I am trying to set a timeout on a Jenkins pipeline job based on a variable.

I have got something like this:

    pipeline {
        agent any
        options {
            timeout(time:6, unit:'HOURS')
    
        }
}

I will like this timeout to be set only when a variable is true. Something like this:

pipeline {
    agent any
    options {
        if (timerCause) {
            timeout(time:6, unit:'HOURS')
       }
    }
}

I can't do this with if or when statements in the options block. Any pointers would be great. Thanks :)

CodePudding user response:

How about you set a default value if the flag is not set.

def flag = false
def to = Integer.MAX_VALUE
if(flag) {
    to = 500
}

pipeline {
    agent any
    options {
         timeout(time:to, unit:'SECONDS')
    }

CodePudding user response:

Not knowing what a timerCause is, and not willing to guess, I'd just recommend the ternary operator:

pipeline {
    agent any
    options {
        timeout(time: timerCause ? 6 : 9999, unit:'HOURS')
    }
}
  • Related