简体   繁体   English

访问为Jenkins Groovy插件脚本指定的变量

[英]Accessing Variables Specified for Jenkins Groovy Plugin Script

When writing a script that is run by Jenkins Groovy Plugin as a build step ( Execute System Groovy Script ) one can specify 'variable bindings'. 在编写由Jenkins Groovy Plugin作为构建步骤( Execute System Groovy Script )运行的脚本时,可以指定“变量绑定”。 The helpline says: Define varibale bingings (in the properties file format). 帮助热线说: 定义varibale bingsings(以属性文件格式)。 Spefified variables can be addressed from the script. 可以从脚本中处理明确的变量。 [sic] How do I access those variables from the script? [原文如此]如何从脚本中访问这些变量? They are not set as environment variables for the build, neither are they present among System properties. 它们未设置为构建的环境变量,它们也不存在于系统属性中。

this.getBinding().getVariables()

或者只是binding.variables

I wasn't able to use binding.variables directly, I only got listener , build , launcher and out from binding.variables. 我无法直接使用binding.variables,我只能从binding.variables获得listenerbuildlauncherout

Instead I was able to use build.environment(listener) to retrieve the environment variables as suggested in the responses to this question: Access to build environment variables from a groovy script in a Jenkins build step ( Windows) 相反,我能够使用build.environment(listener)来检索环境变量,如对此问题的响应中所建议的那样: 在Jenkins构建步骤中从groovy脚本构建环境变量(Windows)

def config = new HashMap()
config.putAll(binding.variables)
def logger = config['out']
def envvars = new HashMap()
envvars.putAll(build.getEnvironment(listener))
def myvar= envvars['myvar']

This might have been different for me since I am only looking for system-wide environmental variables: 这对我来说可能有所不同,因为我只是在寻找系统范围的环境变量:

(checked) Prepare an environment for the run \\ Keep Jenkins Environment Variables \\ Keep Jenkins Build Variables (选中)为运行准备一个环境\\保持Jenkins环境变量\\保持Jenkins构建变量

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

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