发布到URL时,Jenkinsfile中的Groovy会挂起

时间:2016-07-06 20:25:22

标签: groovy http-post jenkins-pipeline jenkinsfile

我在Jenkinsfile中有以下方法用于从给定的URL检索数据(将json发布到它并读取输出)。 在Jenkins中调用它会导致构建挂起* 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                                                                                                                                                                               
}                                                                               

这种方法的召唤:

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

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

当我使用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/

1 个答案:

答案 0 :(得分:0)

问题不在于对HTTP响应的详细阅读,但是如果assert失败(已报告),则会挂起构建。

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

def asrt(value) {                                          
    if (!value) {                                          
        throw new IllegalStateException("failed assertion")
    }                                                      
}