如何在 shell 脚本中使用 Jenkins 参数 [英] How to use Jenkins parameters in a shell script
问题描述
我想使用我们在 Jenkins 作业中定义的参数作为同一作业中 shell 命令的参数.
I want to use the parameters that we define in the Jenkins job as arguments to the shell commands in the same job.
我创建了一个带有以下参数的参数化构建:
I have created a parameterized build with the following parameters:
high.version: 234
low.version: 220
我想将这些变量用作构建的 shell 脚本的参数:
I want to use these variables as arguments for the build's shell script:
/bin/bash /hai/mycode/scripts/run_script.sh high.version
我如何在同一个作业中使用这些参数?
How do I these parameters in the same job?
推荐答案
Jenkins 将使用参数名称创建环境变量.
这里需要注意的是,Jenkins 也会对不代表有效变量名的参数执行此操作——这些参数在 bash
中难以访问.在您的示例中就是这种情况,因为 bash
变量名称不得包含 .
字符.
The caveat here is that Jenkins will also do that for parameters that do not represent valid variable names -- those are difficult to access in bash
. This is the case in your example, as bash
variable names must not contain the .
character.
最简单的解决办法是你
- 重命名参数,例如到
high_version
和low_version
(它们是有效的bash
变量名称) - 然后在调用脚本时使用相应的变量名
- rename your parameters, e.g. to
high_version
andlow_version
(which are validbash
variable names) - then use the corresponding variable names when calling your script
例子:
/bin/bash /hai/mycode/scripts/run_script.sh "$high_version"
如果您不能重命名参数来表示有效的 bash
变量名(例如,出于可用性原因:Jenkins 在 Web 表单中向最终用户提供变量名以开始构建):您仍然可以通过 grepping 在 env
命令的输出中获取参数名称来访问这些参数.
If you cannot rename parameters to represent valid bash
variable names (e.g., for usability reasons: Jenkins presents variable names to end users in the Web form for starting a build): you can still access such parameters by grepping for the parameter name in the output of the env
command.
这篇关于如何在 shell 脚本中使用 Jenkins 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!