0

我在 Jenkinsfile 中有以下方法用于从给定的 URL 检索数据(将 json 发布到它并读取输出)。在 Jenkins 中调用它会导致构建以 *Proceeding.." 文本挂起。

@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                                                                                                                                                                               
}                                                                               

以及此方法的调用:

servers = ["example1"]
for (int i = 0; i < servers.size(); i++) {                                  
    server = servers[i]                                                     
    assert callService(server, 'VersionService:getVersionDetails').matches('.*build:[1-9][0-9]*.*') 
}   

上面的代码是正确的,还是我做错了什么导致代码冻结?

当我使用 curl 做同样的事情时,它可以工作:

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/
4

1 回答 1

0

问题不在于对 HTTP 响应的常规读取,而是由于损坏,如果assert失败,它会挂起构建(已报告https://issues.jenkins-ci.org/browse/JENKINS-34488)。

所以现在的解决方案是编写你自己的assert,例如:

def asrt(value) {                                          
    if (!value) {                                          
        throw new IllegalStateException("failed assertion")
    }                                                      
}
于 2016-07-07T19:04:48.377 回答