Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 227
flowe Avatar asked Oct 27 '17 15:10

flowe


People also ask

How do I get Jenkins pipeline syntax?

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

Which syntax does the declarative pipeline follow?

The basic statements and expressions which are valid in Declarative Pipeline follow the same rules as Groovy's syntax with the following exceptions: The top-level of the Pipeline must be a block, specifically: pipeline { } . No semicolons as statement separators. Each statement has to be on its own line.

What syntax does the scripted pipeline in Jenkins follows?

Creating Your Jenkins Pipeline Script. Pipelines have specific sentences or elements to define script sections, which follow the Groovy syntax.


1 Answers

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
                    }
                }
            }
        }
    }
like image 101
flowe Avatar answered Oct 05 '22 01:10

flowe