Jenkins Groovy 脚本来执行 shell 命令

Wil*_*ent 1 bash shell groovy jenkins graphite

我正在使用一个 groovy 脚本来计算我的构建持续时间并将指标发布到托管石墨,从命令行将产生以下卷曲并产生预期效果:

echo {someMetricHere} | nc carbon.hostedgraphite.com 2003
Run Code Online (Sandbox Code Playgroud)

但是,在我的 groovy 脚本中,生成指标的最后一步是运行以下命令:

"echo "+ metric +" | nc carbon.hostedgraphite.com 2003".execute()
Run Code Online (Sandbox Code Playgroud)

它的回归:

捕获:java.io.IOException:无法运行程序“|”:错误=20,不是目录 java.io.IOException:无法运行程序“|”:错误=20,不是 hudson8814765985646265134.run(hudson8814765985646265134. :27) 由:java.io.IOException: error=20, Not a directory ... 1 more

我假设该命令不理解“|” 命令的一部分,对如何修复此脚本以运行预期的 bash 有何建议?我认为可以在工作区中创建一个 .sh 文件,但我不确定如何。

想要查看完整脚本的人的 Pastebin:https : //pastebin.com/izaXVucF

干杯:)

dag*_*ett 8

使用管道|试试这个代码:

// 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()}"
}
Run Code Online (Sandbox Code Playgroud)

输出:

error=
output=12345
code=0
Run Code Online (Sandbox Code Playgroud)