I'm using a groovy script to calculate my build duration and publish a metric to Hosted Graphite, from the command line the following curl will result with the intend effect:
echo {someMetricHere} | nc carbon.hostedgraphite.com 2003
However in my groovy script the last step having generated a metric is to run the following:
"echo "+ metric +" | nc carbon.hostedgraphite.com 2003".execute()
Its returning:
Caught: java.io.IOException: Cannot run program "|": error=20, Not a directory java.io.IOException: Cannot run program "|": error=20, Not a directory at hudson8814765985646265134.run(hudson8814765985646265134.groovy:27) Caused by: java.io.IOException: error=20, Not a directory ... 1 more
I assume the command doesn't understand the "|" part of the command, any suggestions how I can fix this script to run the intended bash? I thought it might be possible to create a .sh file in the workspace but am not sure how.
Pastebin for those wanting to see full script: https://pastebin.com/izaXVucF
Cheers :)
to use pipe |
try this code:
// 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()}"
}
the output:
error=
output=12345
code=0