Jenkins中Groovy脚本执行curl GET命令报‘无此文件或目录’错误求助
解决Jenkins Groovy脚本中GET请求的"无此文件或目录"错误
我看你遇到的问题很典型,咱们一步步拆解根源和解决办法:
核心问题1:Bash参数使用错误
你写的GET请求Groovy脚本里,把bash的关键参数写错了——应该用-c(这个选项是告诉bash执行后面跟着的命令字符串),但你误写成了-b。bash本身没有-b这个选项,所以它会把整个curl ...的长字符串当成要执行的文件名,自然就会抛出"No such file or directory"的错误。
核心问题2:URL中的&未做转义处理
另外,你的请求URL里包含&Appname=test,这个&在shell环境中会被解析成"将前半段命令后台运行"的分隔符,导致curl命令被意外截断,就算参数改对了也会执行异常。
修正后的Groovy脚本
把这两个问题修复后,脚本应该是这样的:
request = ['bash','-c', "curl -v -k -X GET -b cookies 'https://hostname/get-details?action=getDetails&Appname=test'"].execute() request.waitFor() println request.err.text println request.text
或者也可以用转义符处理&,效果是一样的:
request = ['bash','-c', "curl -v -k -X GET -b cookies https://hostname/get-details?action=getDetails\\&Appname=test"].execute() request.waitFor() println request.err.text println request.text
额外优化建议
其实在Groovy里,你完全可以不用调用bash,直接用Groovy自带的HTTP能力,这样能彻底避免shell解析带来的各种转义坑,在Jenkins环境里也更稳定。举个例子:
// 读取cookies文件内容 def cookieContent = new File("cookies").text // 构建请求 def url = new URL("https://hostname/get-details?action=getDetails&Appname=test") def connection = url.openConnection() connection.setRequestMethod("GET") connection.setRequestProperty("Cookie", cookieContent) // 获取响应 def responseCode = connection.getResponseCode() def responseText = connection.inputStream.text println "响应码: ${responseCode}" println "响应内容: ${responseText}"
内容的提问来源于stack exchange,提问作者Minnie
相关产品推荐
相关产品推荐

