你不能只是把管道化的bash命令扔到一个String中,然后抱着最好的希望,它就是不起作用(主要是因为管道等是由shell执行的,你不会使用它)
你可以这样做:
// A curl process
def curl = ['curl', '--silent', '--request', 'POST', '--header', 'content-type:application/json', 'www.google.co.in'].execute()
// A grep process
def grep = ['grep', 'DOCTYPE'].execute()
// Pipe the output of curl into grep and get the text
(curl | grep).text
要在纯Groovy中做到这一点,需要:
def response = new URL('https://www.google.co.in').with {
openConnection().with {
setRequestProperty('Content-type', 'application/json')
requestMethod = 'POST'
doOutput = true
if (responseCode > 400) {
errorStream.text
} else {
inputStream.text
}
}
}
println response.readLines().findAll { it.contains('DOCTYPE') }
但由于你在詹金斯,我不确定这是否可行