I have following method in Jenkinsfile used to retrieve data from given URL (posting json to it and reading the output). Calling it in Jenkins causes the build to hang with *Proceeding.." text.
@NonCPS
def callService(server, method, params = '') {
final HttpURLConnection connection = "http://$server:8080/router/".toURL().openConnection()
connection.setRequestMethod("POST");
connection.setRequestProperty('Accept', 'application/json;charset=utf-8')
connection.setRequestProperty('Content-Type', 'application/json;charset=utf-8')
connection.setDoOutput(true)
connection.outputStream.withWriter { Writer writer ->
writer << """{"jsonrpc": "2.0", "method": "$method", "params": {$params}}"""
}
String text = connection.inputStream.withReader { Reader reader -> reader.text }
return text
}
And the call of this method:
servers = ["example1"]
for (int i = 0; i < servers.size(); i++) {
server = servers[i]
assert callService(server, 'VersionService:getVersionDetails').matches('.*build:[1-9][0-9]*.*')
}
Is the above code correct groovy one, or am I doing something wrong that causes the code to freeze?
When I do the same thing using curl, it works:
curl -v -H 'Accept: application/json;charset=utf-8' -H 'Content-Type: application/json;charset=utf-8' -d '{"jsonrpc": "2.0", "method":"VersionService:getVersionDetails", "params":{}}' http://example1:8080/router/
The problem was not with groovy reading of the HTTP response but with broken jenkins-pipeline, it hangs the build if an assert
fails (reported already https://issues.jenkins-ci.org/browse/JENKINS-34488).
So the solution for now is to write your own assert
, e.g.:
def asrt(value) {
if (!value) {
throw new IllegalStateException("failed assertion")
}
}