简体   繁体   English

访问Groovy中的Jenkins全局属性

[英]Accessing Jenkins global property in Groovy

At this moment I defined a environment variable called GLOBAL_VAR with the value 'test' under Manage Jenkins -> Configure System -> Global Properties. 此时我在Manage Jenkins - > Configure System - > Global Properties下定义了一个名为GLOBAL_VAR的环境变量,其值为'test'。 I have a shell script when a project is being build containing 我正在构建包含的项目时有一个shell脚本

echo "The value of the global variable is ${GLOBAL_VAR}"

If executed, the result is what I expected: 如果执行,结果就是我的预期:

The value of the global variable is test

Now my problem is that I want to access this variable in a Groovy script which is used to create a drop-down menu in the parameterized build properties of Jenkins. 现在我的问题是我想在Groovy脚本中访问这个变量,该脚本用于在Jenkins的参数化构建属性中创建一个下拉菜单。 This drop-down menu contains values also used in the shell script(s) executed when a build is performed. 此下拉菜单包含在执行构建时执行的shell脚本中也使用的值。 So the drop down items should be created when someone clicks "Build with Parameters" in Jenkins itself eg before any build is started . 因此,当有人在Jenkins本身单击“使用参数构建”时,例如在任何构建开始之前,应创建下拉项。

I don't want to change the shell script code and the drop-down Groovy script every time I want to change these values. 每次我想要更改这些值时,我都不想更改shell脚本代码和下拉Groovy脚本。 Moreover, I want to use the same values in different build jobs which means I need to change them all every time I need to change a variable. 此外,我想在不同的构建作业中使用相同的值,这意味着每次需要更改变量时我都需要更改它们。

The Dynamic Choice Parameter creating the drop-down menu contains only the following Groovy expression to generate the choices: 创建下拉菜单的动态选择参数仅包含以下Groovy表达式以生成选项:

 def list = ["Option 1", "Option 2", "Option 3", "Option 4"]

And I want to add the value of my GLOBAL_VAR to this list. 我想将GLOBAL_VAR的值添加到此列表中。

Does anyone know how to access the Jenkins environment variables in Groovy? 有谁知道如何在Groovy中访问Jenkins环境变量? Or does anyone know a different way to manage variables I want to use both within the Dynamic Choice Parameter (drop-down menu) and in the shell scripts executed when building? 或者有没有人知道一种不同的方法来管理我想在动态选择参数(下拉菜单)和构建时执行的shell脚本中使用的变量?

in "Execute Groovy script" you can access global (and build) variables by: 在“执行Groovy脚本”中,您可以通过以下方式访问全局(和构建)变量:

def env = System.getenv()
println(env['GLOBAL_VAR'])

Here is an example of some working code: 以下是一些工作代码的示例:

import jenkins.model.*
nodes = Jenkins.instance.globalNodeProperties
nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
envVars = nodes[0].envVars
envVars['MY_GLOBAL_PROPERTY']

Sources of my solution: 我的解决方案的来源:

https://gist.github.com/johnyzed/2af71090419af2b20c5a https://gist.github.com/johnyzed/2af71090419af2b20c5a

http://pghalliday.com/jenkins/groovy/sonar/chef/configuration/management/2014/09/21/some-useful-jenkins-groovy-scripts.html http://pghalliday.com/jenkins/groovy/sonar/chef/configuration/management/2014/09/21/some-useful-jenkins-groovy-scripts.html

Note to the "hard core" moderator that deleted my previous answer: I have spent quite a few hours searching on the Internet before finding the right solution, you shouldn't delete posts without knowing what is it really talking about. 请注意删除我以前的答案的“硬核”主持人:在找到正确的解决方案之前,我花了几个小时在互联网上搜索,你不应该删除帖子而不知道它真正在谈论什么。 I find such behavior rather discouraging :( 我发现这种行为令人沮丧:(

Does anyone know how to access the Jenkins environment variables in groovy? 有谁知道如何在groovy中访问Jenkins环境变量?

I can provide an example from a Jenkins job we use. 我可以提供一个我们使用的Jenkins工作的例子。 The job is parameterized and provides the user with the ability to set many values, one of which is "InstallVersion" (default value is -1). 作业已参数化,并为用户提供设置许多值的功能,其中一个值为“InstallVersion”(默认值为-1)。 Additionally, we set up an environment variable called "JENKINS_TRUNK_LABEL". 另外,我们设置了一个名为“JENKINS_TRUNK_LABEL”的环境变量。 I won't go into specifics about this job, but, here's how we refer to "JENKINS_TRUNK_LABEL" within a Groovy script. 我不会详细介绍这个工作,但是,这是我们在Groovy脚本中引用“JENKINS_TRUNK_LABEL”的方式。

if (InstallVersion == "-1")
{
  InstallVersion = "$JENKINS_TRUNK_LABEL"
}

You can use: 您可以使用:

import jenkins.model.*
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

newEnvVarsNodeProperty = null
envVars = null

if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
  newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
  globalNodeProperties.add(newEnvVarsNodeProperty)
  envVars = newEnvVarsNodeProperty.getEnvVars()
} else {
  envVars = envVarsNodePropertyList.get(0).getEnvVars()

}

envVars.put("FOO", "foo")
instance.save()

The solution was found here 解决方案在这里找到

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

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