java.io.IOException:无法运行程序"curl":错误=2,没有这样的文件或目录



我正在使用 jenkins 的 docker 镜像并将其部署在 kubernetes 集群上。 我已经编写了一个时髦的脚本来在 jenkins 上动态创建的从服务器上运行 curl 命令,并且还配置了 slave 来运行 curl 命令,但在我的 jenkins 控制台中收到上述错误。我还使用where curl检查了我的从节点上是否安装了 curl,它给出的响应为/usr/bin/curl

我尝试在我的从属节点上仅运行 curl 命令,它可以工作。但是当我使用 Jenkins 调用时髦的脚本文件时,它会给出错误java.io.IOException: Cannot run program "curl": error=2, No such file or directory.

我猜 groovy 找不到 curl,请尝试使用完整路径调用 curl,如下所示:

def process = ['/usr/bin/curl', 'https://someurl'].execute()
process.consumeProcessOutput(System.out, System.err)
process.waitFor()

作为替代方案,如果您只需要对某个 URL 执行 HTTP get 请求,则可以在普通的 groovy 中执行此操作,而无需通过以下方式依赖 curl:

def response = 'https://someurl'.toURL().text

<评论后编辑>

你也可以使用纯 groovy 和类似的东西(未经测试)来做一个 post 请求:

def url  = 'http://api.duckduckgo.com'.toURL()
def body = 'some data'
url.openConnection().with {
doOutput      = true
requestMethod = 'POST'
// send post body 
outputStream.withWriter { writer ->
writer << body
}
// set header 
setRequestProperty "Content-Type", "application/x-www-form-urlencoded"
// print response
println content.text
}

相关内容

最新更新