Retrieving Jenkins node variables from master

社会主义新天地 提交于 2019-12-11 09:17:58

问题


So my google-fu has failed me and the Jenkins O'Reilly book isn't helping either.

I have a Jenkins setup with a master and 20-odd nodes. I make heavy use of custom environment variables on the nodes, as many of them perform similar tasks, only slightly different platforms.

I'm now in a position that I have a job that runs on the master (by necessity), which needs to know certain node properties for any given node, including some of the environment variables that I've set up.

Is there any way to reference these? My alternative seems to be to have hundreds of environment variables in the master in the form node1_var1, node2_var1, node1_var2, node2_var2 etc., which just seems messy. The master clearly has knowledge of the variables, as that's where the configuration for them is done, but I just can't find a way to specify them in a job.

Any help (or ridicule and pointing out of obvious answers) much appreciated...


回答1:


Here's a simple Groovy script that prints the list of environment variables for each slave:

for (slave in jenkins.model.Jenkins.instance.slaves) {
  println(slave.name + ": ")
  def props = slave.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
  for (prop in props) {
    for (envvar in prop.envVars) {
      println envvar.key + " -> " + envvar.value
    }
  }
}

Warning: I am not an experienced Groovy programmer, so I may not be using the appropriate idioms of Groovy.

You can run this from the Jenkins script console in order to experiment. You can also run a "System Groovy Script" as a build step. Both of the above require the Groovy plugin. If you don't use Groovy in your job, you could use this script to write a properties file that you load in the part of your build that does the real work.



来源:https://stackoverflow.com/questions/28070515/retrieving-jenkins-node-variables-from-master

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!