本文介绍了Jenkins插件中的变量替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正在开发一个新的Jenkins插件,该插件将在Jenkins作业的构建阶段执行,并且要求允许用户在插件的作业配置中指定变量名称(而不是文字值) 。目的是当作业执行时,用户指定的变量名将被替换为与变量关联的实际值,然后插件将在运行perform方法时使用此实际值。I am developing a new Jenkins plugin that will be executed during the build phase of a Jenkins job and have a requirement to allow the user to specify a variable name (as opposed to a literal value) in the job configuration for the plugin. The intention is that when the job executes the variable name specified by the user will then be substituted with the real value associated with the variable and that the plugin will then use this real value when running the perform method.例如,如果将值 myValue 的变量 MY_VARIABLE 注入构建环境通过另一部分作业,我的插件的作业配置中指定了值 $ {MY_VARIABLE} ,然后我希望该插件替换 $ {MY_VARIABLE} ,其变量的实际值为 myValue 。For example if the variable MY_VARIABLE with the value myValue was injected into the build environment by another part of job and the value ${MY_VARIABLE} was specified in the job configuration for my plugin, then I would like the plugin to substitute ${MY_VARIABLE} with the real value for the variable which is myValue.完成了一些研究我理解Jenkins不会自动将作业配置中的变量替换为各自的值,这必须由插件处理。我无法解决的是在我的插件中执行替换的最佳方法。到目前为止我找到的唯一解决方案是解析从作业配置传递的字符串,看它是否与变量的正确模式匹配,然后在我的代码中查找该值。Having done some research I understand that Jenkins does not automatically substitute variable in the job configuration for their respective values and this must be handled by the plugin. What I haven't been able to work out is the best way to perform the substitution in my plugin. The only solution I've found so far is the parse the string passed from the job configuration to see whether it matches the correct pattern for a variable and then lookup the value in my code.我的问题是Jenkins API是否提供了一个更好的解决方案,允许我的插件用实际值替换变量?My question is does the Jenkins API provide a better solution that would allow my plugin to substitute a variable with a real value?推荐答案您可以检索构建环境 - EnvVars 对象 - 它有一个方便的方法 expand(String) 。You can retrieve the build environment — an EnvVars object — which has a convenience method expand(String).这识别 $ VARIABLE 和 $ {VARIABLE} -style字符串和替换字符串来自环境的相应值。This recognises $VARIABLE and ${VARIABLE}-style strings and substitutes in the corresponding value from the environment.例如:@Overridepublic boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { ... final EnvVars env = build.getEnvironment(listener); String expandedDbUrl = env.expand(dbUrl); ...} 这篇关于Jenkins插件中的变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 02:21