jenkins - 如何将值从 bash 传递到 groovy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29550237/
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 12:45:28  来源:igfitidea点击:

jenkins - how to pass a value from bash to groovy?

bashgroovyjenkins

提问by ethrbunny

I'm working on a jenkins install with two script components. The bashbits run first and then groovy. I'd like to be able to pass a value (property? Other?) from the bash script->groovy script.

我正在使用两个脚本组件进行 jenkins 安装。该庆典位,然后再运行常规。我希望能够从 bash 脚本->groovy 脚本传递一个值(属性?其他?)。

Is this possible? Do I need to write the value to a property file and read it back in groovy?

这可能吗?我是否需要将值写入属性文件并在 groovy 中读回?



EDIT: my goal from this was to generate a build # in bash and pass this to groovy so I could set the description and build # in the jenkins display. It appears that groovy isn't available on the build server so I'm looking for another direction. Currently experimenting with the 'postbuild' plugin and the 'env-inject' plugin. Open to suggestions.

编辑:我的目标是在 bash 中生成一个 build # 并将其传递给 groovy,这样我就可以在 jenkins 显示中设置描述和 build #。似乎 groovy 在构建服务器上不可用,所以我正在寻找另一个方向。目前正在试验“postbuild”插件和“env-inject”插件。对建议持开放态度。

回答by bitwised

Here are a few things to consider to make this successful:

要想成功,需要考虑以下几点:

  1. Make sure you're trying to accomplish this with one "Execute shell" in Jenkins or from a script.
  2. Export the shell variable so that the variable will be present in the child process that will execute your groovy script.
  1. 确保您正在尝试使用 Jenkins 或脚本中的一个“执行 shell”来完成此操作。
  2. 导出 shell 变量,以便该变量将出现在将执行您的 groovy 脚本的子进程中。
# foo.sh
export foo=bar
groovy myscript.groovy
# myscript.groovy
def env = System.getenv()
String myvar=env['foo']
println myvar

Running foo.sh should produce the following:

运行 foo.sh 应该产生以下结果:

./foo.sh
bar

回答by BalRog

If for some reason you prefer notto export the variable (there may be good and valid reasons for this), you can still explicitlypass the variable to the groovy script as a "system property":

如果出于某种原因您不想导出变量(可能有充分且有效的原因),您仍然可以将变量作为“系统属性”显式传递给 groovy 脚本:

# foo.sh
foo=bar
groovy -Dfoo="$foo" yourscript.groovy

# yourscript.groovy
String yourvar = System.properties['foo']
println yourvar

which produces the following results:

产生以下结果:

$ ./foo.sh
bar
$ 

回答by Amy B Higgins

I just worked on this problem for days and thought I might share what I discovered. I had to access a variable in a groovy file from a .sh file and had difficulty at first grabbing the variable. There is a simple way to do it, though. Here's what I did:

我只是在这个问题上工作了几天,并认为我可以分享我的发现。我必须从 .sh 文件访问 groovy 文件中的变量,并且在第一次获取变量时遇到了困难。不过,有一种简单的方法可以做到。这是我所做的:

In your bash file, save the value in a variable. Then in the groovy script, do this:

在 bash 文件中,将值保存在变量中。然后在 groovy 脚本中,执行以下操作:

variableToGet = sh(returnStdout: true, script: """
                . ${DIRECTORY}/bash_file.sh
                echo $VARIABLE
            """).trim()

Hope this helps. This problem was a good challenge! It's important to note, however, that standard out will return a String, regardless of what type of variable you are grabbing. If you need to use an integer value, you can then use the integer value with Integer.parseInt(variableToGet)

希望这可以帮助。这个问题是一个很好的挑战!但是,重要的是要注意,无论您获取的是什么类型的变量,标准输出都将返回一个字符串。如果需要使用整数值,则可以将整数值与Integer.parseInt(variableToGet)

回答by Jeff Sloyer

The best way is setting an environment variable to share the information from bash into groovy. You could pipe things as well using standard in/out as well.

最好的方法是设置一个环境变量来将信息从 bash 共享到 groovy。您也可以使用标准输入/输出进行管道传输。

So if you are setting the env in a bash script it wont be available outside of that script. Instead of doing a bash script put the script inline in your command in jenkins. Run your bash code then call the groovy script.

因此,如果您在 bash 脚本中设置 env,它将在该脚本之外不可用。不是执行 bash 脚本,而是将脚本内联到 jenkins 的命令中。运行您的 bash 代码,然后调用 groovy 脚本。

Something like below

像下面这样的东西

#do somebash scripting
VARIABLE="something"

#call your groovy code
groovy util.groovy

your groovy code (util.groovy):

你的常规代码(util.groovy):

String variable = env['VARIABLE']