How to test sh script return status with jenkins declarative pipeline new syntax

flowe picture flowe · Oct 27, 2017 · Viewed 9.8k times · Source

Using new jenkins declarative pipeline syntax, I'd like to test the return status of a sh script execution. Is it possible without using script step?

Script pipeline (working) :

...
stage ('Check url') {
   node {
    timeout(15) {
      waitUntil {
        sleep 20
        def r = sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true
        return (r == 0);
      }
    }
  }
}

Declarative pipeline (try) :

...
      stage('Check url'){
        steps {
            timeout(15) {
                waitUntil {
                    sleep 20
                    sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true == 0
                }
            }
        }
    }

log : java.lang.ClassCastException: body return value null is not boolean

Answer

flowe picture flowe · Oct 30, 2017

Since it's not possible without script block, we get something like :

...
stage('Check url'){
        steps {
            script {
                timeout(15) {
                    waitUntil {
                        sleep 20
                        def r = sh script: "wget -q ${CHECK_URL} -O /dev/null", returnStatus: true
                        return r == 0
                    }
                }
            }
        }
    }