bash Jenkins Groovy 脚本来执行 shell 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46484521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:29:42  来源:igfitidea点击:

Jenkins Groovy script to execute shell commands

bashshelljenkinsgroovygraphite

提问by WillBroadbent

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:

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

echo {someMetricHere} | nc carbon.hostedgraphite.com 2003

However in my groovy script the last step having generated a metric is to run the following:

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

"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

捕获:java.io.IOException:无法运行程序“|”:错误=20,不是目录 java.io.IOException:无法运行程序“|”:错误=20,不是 hudson8814765985646265134.run(hudson8814765985646265134. :27) 由: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.

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

Pastebin for those wanting to see full script: https://pastebin.com/izaXVucF

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

Cheers :)

干杯:)

回答by daggett

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

回答by Carolinne Torres

I think something is wrong with the concatenation you did.

我认为你所做的连接有问题。

This code should work:

此代码应该工作:

"echo ${metric} | nc carbon.hostedgraphite.com 2003".execute()

回答by Alvaro Cavalcanti

A simpler way to achieve this is to use the Jenkins Job DSL. It features a shellcommand that can be issued from within a given step. For example:

实现此目的的更简单方法是使用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'))
    }
}

You can find the reference here.

您可以在此处找到参考。

回答by GordonFreeman

If you have to pass in a variable to a groovy script, you do it using ${variableName}. Double quotes are not interpreted like the way you think they would, and every compiler treats it in a weird way.

如果必须将变量传递给 groovy 脚本,请使用${variableName}. 双引号不会像你想象的那样解释,每个编译器都以一种奇怪的方式对待它。

In your case, the below line should help to do the thing you want:

在您的情况下,以下行应该有助于做您想做的事情:

sh "echo ${metric} | nc carbon.hostedgraphite.com 2003"