Jenkinsfile 中的 Groovy 在发布到 URL 时挂起

Groovy in Jenkinsfile hangs when posting to an URL

我在 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]*.*') 
}   

上面的代码是正确的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/

问题不在于 groovy 读取 HTTP 响应,而是损坏的 , it hangs the build if an assert fails (reported already https://issues.jenkins-ci.org/browse/JENKINS-34488)。

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

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