Curl request from command line and via Groovy script

♀尐吖头ヾ 提交于 2019-11-28 14:37:49

groovy String.execute() returns Process that could be still running (depends on system load and weather))

if you want to wait until process ended do like this:

def txt = "cmd /c dir c:\\".execute().with{
    def output = new StringWriter()
    def error = new StringWriter()
    //wait for process ended and catch stderr and stdout 
    it.waitForProcessOutput(output, error)
    //check there is no error
    assert error.toString().size()==0: "$error"
    //println it.exitValue() //we can do check with error code
    //return stdout from closure 
    return output.toString()
}

for jenkins pipeline to avoid error java.io.NotSerializableException use the following code:

node {
    def res = runAndWait("cmd /c dir c:\\")
    echo res
}

@NonCPS
String runAndWait(Object cmd){
    def proc = cmd.execute()
    def output = new StringWriter()
    def error = new StringWriter()
    //wait for process ended and catch stderr and stdout 
    proc.waitForProcessOutput(output, error)
    //check there is no error
    assert error.toString().trim().size()==0: "$error"
    //assert proc.exitValue()==0 //we can do check with error code
    //return stdout from closure 
    return output.toString()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!