3

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

Judy1989
  • 327
  • 1
  • 6
  • 18

1 Answers1

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • somehow problem was solved calling ```.toInteger()```in the if statement, doing so ```HOUR=sh(returnStdout: true, script: 'date +"%H"').trim()``` and later ````if ( HOUR.toInteger() > 16 ) ```` – Judy1989 Jun 10 '19 at 10:10
  • @Judy1989 OK, I have included your comment in the answer for more visibility. – VonC Jun 10 '19 at 13:16