jenkins - 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
。
解决方案
我猜 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
}
推荐阅读
- java - 使用 Java 解析 .csv 文件时面临的问题
- javascript - 防止字符串成为javascript中的数字
- java - 如何在没有中间模型类的情况下将列表绑定到视图?
- typescript - 折叠一个有区别的联合 - 从联合中派生一个包含所有可能键值组合的伞类型
- python - 如何解决写入文件的问题?
- python - 为什么 q_net 有这么多的输入层?
- javascript - 我正在尝试制作一个使用 CSS 和 Javascript 打印的游戏原型,但是当打印选项卡打开时,图标会失去颜色
- c++ - 我应该在哪里存储项目常量定义/声明
- can-bus - 为什么接收中存在 CAN 硬件验收滤波器?
- reactjs - JSX 元素没有构造或调用签名