19

I am currently starting to convert our builds into a Jenkins build pipeline. At a certain point it is necessary for us to wait for the startup of a web application within a docker container.

My idea was to use something like this:

timeout(120) {
    waitUntil {
        sh 'wget -q http://server:8080/app/welcome.jsf -O /dev/null'
    }
}

Unfortunately this makes the pipeline build fail:

ERROR: script returned exit code 4

Is there any simple way to make this work?

Edit:

I managed to make it work using the following code, but the stage is still marked as failed (although the build continues and is marked green in the end).

timeout(120) {
    waitUntil {
        try {
            sh 'wget -q http://server:8080/app/welcome.jsf -O /dev/null'
            return true
        } catch (exception) {
            return false
        }
    }
}
StephenKing
  • 36,187
  • 11
  • 83
  • 112
Nitek
  • 2,515
  • 2
  • 23
  • 39

3 Answers3

44

They just released a new version of the Pipeline Nodes and Processes Plugin which adds support for returning the exit status. This seems to do the job now:

timeout(5) {
    waitUntil {
       script {
         def r = sh script: 'wget -q http://remoterhoste/welcome.jsf -O /dev/null', returnStdout: true
         return (r == 0);
       }
    }
}
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
Nitek
  • 2,515
  • 2
  • 23
  • 39
6

You can use wget options to achieve that:

waitUntil {
    sh 'wget --retry-connrefused --tries=120 --waitretry=1 -q http://server:8080/app/welcome.jsf -O /dev/null'
}

120 tries, plus wait 1s between retries, retry even in case of connection refused, this might be slightly more seconds. So to make sure it is only 120s, then you can use timeout from shell:

waitUntil {
    sh 'timeout 120 wget --retry-connrefused --tries=120 --waitretry=1 -q http://server:8080/app/welcome.jsf -O /dev/null'
}
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • Interesting approach! There is no need for the waitUntil anymore, is it? – Nitek Jul 27 '16 at 05:51
  • 2
    I just realized that his doesn't work in my case, because once the application server is started it will return a 404 until the application is started as well. If wget trys to connect during this time it will exit with error code 8 – Nitek Jul 27 '16 at 06:49
2

If you don't have wget on the jenkins node (e.g. the default docker image) you can also install and use the HTTPRequest Plugin like this.

timeout(5) {
    waitUntil {
        script {
            try {
                def response = httpRequest 'http://server:8080/app/welcome.jsf'
                return (response.status == 200)
            }
            catch (exception) {
                 return false
            }
        }
    }
}
zaphod
  • 126
  • 4