我正在使用一个groovy脚本来计算我的构建持续时间并将度量标准发布到Hosted Graphite,从命令行,以下curl将产生意图效果:
echo {someMetricHere} | nc carbon.hostedgraphite.com 2003
但是在我的groovy脚本中,生成度量标准的最后一步是运行以下命令:
"echo "+ metric +" | nc carbon.hostedgraphite.com 2003".execute()
它回归:
抓到:java.io.IOException:无法运行程序“|”:error = 20,不是目录java.io.IOException:无法运行程序“|”:error = 20,不是hudson8814765985646265134.run目录(hudson8814765985646265134.groovy) :27)引起:java.io.IOException:error = 20,不是目录......还有1个
我假设命令不理解“|”命令的一部分,任何建议我如何修复此脚本以运行预期的bash?我认为可能在工作区中创建.sh文件,但我不确定如何。
想要查看完整脚本的人使用Pastebin:https://pastebin.com/izaXVucF
干杯:)
使用管道|
试试这段代码:
// this command line definitely works under linux:
def cmd = ['/bin/sh', '-c', 'echo "12345" | grep "23"']
// this one should work for you:
// def cmd = ['/bin/sh', '-c', 'echo "${metric}" | nc carbon.hostedgraphite.com 2003']
cmd.execute().with{
def output = new StringWriter()
def error = new StringWriter()
//wait for process ended and catch stderr and stdout.
it.waitForProcessOutput(output, error)
//check there is no error
println "error=$error"
println "output=$output"
println "code=${it.exitValue()}"
}
输出:
error=
output=12345
code=0
更简单的方法是使用Jenkins Job DSL。它具有shell
命令,可以在给定的step
内发出。例如:
// execute echo command
job('example-1') {
steps {
shell('echo Hello World!')
}
}
// read file from workspace
job('example-2') {
steps {
shell(readFileFromWorkspace('build.sh'))
}
}
你可以找到参考here。
我认为你所做的连接有问题。
这段代码应该有效:
"echo ${metric} | nc carbon.hostedgraphite.com 2003".execute()