Jenkins - if statement comparing HOUR is not working

Viewed 958

I am trying to add a "validation" stage in Jenkinsfile based on the time. If it is later than 16, validation is required, otherwise not.

the if statement is not working

here I am declaring the variable

HOUR=sh(returnStdout: true, script: 'date +"%H"').trim().toInteger() 

and here is the stage

stage('validation') {
  steps {
    script {
      if ( HOUR > 16 ) {
        echo "Validation is required, time now is $HOUR"
      }
      else {
        echo "No validation required, time now is $HOUR"
      }
    }
  }
}

and here is the output

Validation is required, time now is 9

the value of the variable HOUR is correct, but the if statement doesnt work correctly

thanks in advance

1 Answers

Try first to refer to your variable with the ${xx} syntax:

if ( ${HOUR} > 16 ) {

Actually, that would be to be defined in an environment step to be working, as ${env.HOUR}, as illustrated here.


The OP Judy1989 confirms in the comment that you can use HOUR, but in two steps:

HOUR=sh(returnStdout: true, script: 'date +"%H"').trim() 
...
if ( HOUR.toInteger() > 16 )

You can see an example of such a deferred use in this question.

Related