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

17,494

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
17,494
Chrispie
Author by

Chrispie

Love to see what I do, hence love to create front ends… Programming is programming but sometimes I do feel that a frond end developer get so more appreciation. I once created a service that did so much… Caching data that refreshes every 5 minutes and most probable anything possible you can do optimise a backend service. To me it was awesome because I knew what was under the hood. Sadly I am the only to think this was awesome lol. Don’t get me wrong. Back end services needs to exists but love the flashy stuff ;) I programmed in Delphi/Pascal in high school and since varsity in Java and VB.net. Later in my career I learned Magic and C#. Back into Java nowadays using several fronts ends (Flex, GWT and JSP’s). Java is awesome and there are so many geeky things to mix it with like Spring. But that said I still love front ends… would like to see where JavaFX is going. And really hope to see it being used in the future. But time will only tell.

Updated on June 04, 2022

Comments

  • Chrispie
    Chrispie almost 2 years

    I need to give a server name to a maven build. During the maven build this server name will be used to make a call that server do some tests on that server.

    Our servers have jenkins slaves on them and is grouped using labels

    Example

    Slaves/Node |  Label
    
    Server1     |  BackEndServers
    
    Server2     |  BackEndServers
    
    Server3     |  FrontEndServers
    
    Server4     |  FrontEndServers
    

    With Elastic Axis plugin i can say run my Jenkins job on this Node Label (for example on BackEndServers) and the same project will be executed on both of the servers (Server1 & Server2).

    In my case I cannot do this as maven is not installed on the BackEndServers where my code is running. But the maven build needs to know about the server names though.

    So is there a way how I can get the server names from a label and then run the same job multiple times passsing each servername to the maven build?

    Example

    • Giving that I have the label 'BackEndServers'

    • obtain a list of node names 'Server1,Server2'

    • and run my job for each node name and pass a parameter to it
    • aka
      • Having Job (with parameter Server1)
      • Having Job (with parameter Server2)
  • Chrispie
    Chrispie about 9 years
    Thanks so much it helped me to get to a solution.