Jenkins: How to get node name from label to use as a parameter

隐身守侯 提交于 2019-12-06 07:45:51
gclaussn

Use Jenkins environment variables like NODE_NAME in the Maven command of the build job as value for a system property. For example:

 mvn clean install -Djenkins.node.name=${NODE_NAME}

In your Maven project (pom.xml) configure the plugin, which requires the node name, with the help of following property: ${jenkins.node.name}

Here are some links - how to trigger Jenkins builds remotely:

  1. How to trigger Jenkins builds remotely and to pass paramter
  2. Triggering builds remotely in Jenkins
  3. Launching a build with parameters

I don't, if it is possible in the way you want it. But the provided information should help you to find a solution.

Try Jenkins.getInstance().getComputer(env.NODE_NAME).getNode() See more on official Doc

In the end I created a 2 jobs.

  1. To interogate the Jenkens nodes for me and build up a string of servers to use
  2. Then use Dynamic Axis lable with the list I have in Job 1 to execute my maven build

In Job 1 - I used The EnjEnv plugin and it has a 'Evaludated Groovy Script' section that basically you can do anything... but it should return a property map. I don't know how to return a value from a Groovy script so this worked kewl for me as I can reference property (or Environment variables) from almost anyware

import hudson.model.*

String labelIWantServersOf = TheLabelUsedOnTheElasticAxisPlugin; // This is the label assosiated with nodes for which i want the server names of
String serverList = '';

for (aSlave in hudson.model.Hudson.instance.slaves) {          
  out.println('Evaluating Server(' + aSlave.name + ') with label = ' + aSlave.getLabelString());  

  if (aSlave.getLabelString().indexOf(labelIWantServersOf ) > -1) {
    serverList += aSlave.name + ' ';        
    out.println('Valid server found: ' + aSlave.name);                  
  }    

}

out.println('Final server list where SOAP projects will run on = ' + serverList + ' which will be used in the environment envInject map');

Map<String, String> myMap = new HashMap<>(2);
myMap.put("serverNamesToExecuteSoapProjectOn", serverList );
return myMap;

Then I had some issue to pass the Environment var onto my next job. So I simply wrote the values that I wanted to a property file using a windows batc script in the Build process

echo serverNamesToExecuteSoapProjectOn=%serverNamesToExecuteSoapProjectOn%> baseEnvMap.properties

Then as a post build action I had a "Trigger parameterized build on other projects' calling my 2nd job and I passed the baseEnvMap.properties to it.

Then on my Job 2 which is a Multiconfig job I added a Dynamic Axis using the environment var that was passed via the property file to job 2.

This will duplicate Job 2 and execute it each time with the value that the groovy script build up which I can reference in my mvn arguments.

To list out all nodes of label name LABELNAME:

http://ServerIP:8080/label/LABELNAME/api/json?pretty=true

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