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

时间:2019-06-11 07:20:22

标签: jenkins curl groovy jenkins-groovy

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

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

1 个答案:

答案 0 :(得分:0)

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

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

作为替代方案,如果您只需要对某些URL进行http get请求,则可以通过普通方式而不用依赖curl来做到这一点:

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

<评论后编辑>

您还可以使用纯常规方法和以下内容(未经测试)进行发布请求:

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
}
相关问题