简体   繁体   English

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

[英]jenkins - how to pass a value from bash to groovy?

I'm working on a jenkins install with two script components. 我正在使用两个脚本组件进行詹金斯安装。 The bash bits run first and then groovy . bash位运行,然后再时髦 I'd like to be able to pass a value (property? Other?) from the bash script->groovy script. 我希望能够从bash脚本-> groovy脚本传递一个值(属性?其他?)。

Is this possible? 这可能吗? Do I need to write the value to a property file and read it back in 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. 编辑:我的目标是在bash中生成一个build#并将其传递给groovy,以便我可以在jenkins显示中设置描述并构建#。 It appears that groovy isn't available on the build server so I'm looking for another direction. 看来groovy在构建服务器上不可用,所以我正在寻找另一个方向。 Currently experimenting with the 'postbuild' plugin and the 'env-inject' plugin. 目前正在尝试使用“ postbuild”插件和“ env-inject”插件。 Open to suggestions. 公开征求意见。

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. 确保您尝试使用Jenkins中的一个“执行外壳”或通过脚本来完成此操作。
  2. Export the shell variable so that the variable will be present in the child process that will execute your groovy script. 导出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

If for some reason you prefer not to export the variable (there may be good and valid reasons for this), you can still explicitly pass 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
$ 

The best way is setting an environment variable to share the information from bash into groovy. 最好的方法是设置一个环境变量,以将信息从bash共享到groovy。 You could pipe things as well using standard in/out as well. 您也可以使用标准输入/输出来传送内容。

So if you are setting the env in a bash script it wont be available outside of that script. 因此,如果您在bash脚本中设置env,则在该脚本之外将无法使用它。 Instead of doing a bash script put the script inline in your command in jenkins. 不用执行bash脚本,而是在jenkins的命令中内联该脚本。 Run your bash code then call the groovy script. 运行您的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']

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. 我必须从.sh文件访问groovy文件中的变量,并且在第一次抓取变量时遇到了困难。 There is a simple way to do it, though. 不过,有一种简单的方法可以做到这一点。 Here's what I did: 这是我所做的:

In your bash file, save the value in a variable. 在bash文件中,将值保存在变量中。 Then in the groovy script, do this: 然后在常规脚本中执行以下操作:

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. 需要特别注意的是,无论您要获取哪种类型的变量,标准输出都将返回String。 If you need to use an integer value, you can then use the integer value with Integer.parseInt(variableToGet) 如果需要使用整数值,则可以将整数值与Integer.parseInt(variableToGet)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM