首页 > 解决方案 > java.io.IOException:无法运行程序“curl”:错误=2,没有这样的文件或目录

问题描述

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

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

标签: jenkinscurlgroovyjenkins-groovy

解决方案


我猜 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 和类似以下内容(未经测试)进行发布请求:

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
}

推荐阅读