Jenkins:如何从标签获取节点名称以用作参数 [英] Jenkins: How to get node name from label to use as a parameter

查看:1035
本文介绍了Jenkins:如何从标签获取节点名称以用作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要给Maven构建一个服务器名称.在进行Maven构建期间,此服务器名称将用于调用该服务器在该服务器上进行一些测试.

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

示例

Slaves/Node |  Label

Server1     |  BackEndServers

Server2     |  BackEndServers

Server3     |  FrontEndServers

Server4     |  FrontEndServers

使用Elastic Axis插件,我可以说在此Node Label上运行我的Jenkins作业(例如在BackEndServers上),并且将在两个服务器(Server1和Server2)上执行相同的项目.

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).

在我的情况下,我无法执行此操作,因为在运行我的代码的BackEndServers上未安装maven.但是,Maven构建需要了解服务器名称.

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.

那么有没有办法我可以从标签中获取服务器名称,然后多次运行相同的作业,将每个服务器名称传递给Maven版本?

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?

示例

  • 给我贴上"BackEndServers"标签

  • Giving that I have the label 'BackEndServers'

获取节点名称"Server1,Server2"的列表

obtain a list of node names 'Server1,Server2'

  • 拥有作业(带有参数Server1)
  • 拥有作业(带有参数Server2)

推荐答案

最后,我创建了2个工作.

In the end I created a 2 jobs.

  1. 为我询问Jenkens节点并建立一串服务器以供使用
  2. 然后将动态轴标签与作业1中的列表结合使用以执行我的maven构建

在作业1中-我使用了EnjEnv插件,它有一个评估的Groovy脚本"部分,基本上您可以做任何事情...但是它应该返回属性映射.我不知道如何从Groovy脚本中返回值,所以这对我来说非常有效,因为我可以从几乎任何软件中引用属性(或环境变量)

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;

然后我遇到了一些问题,无法将Environment var传递到我的下一份工作中.因此,我只是在构建过程中使用Windows batc脚本将所需的值写到了属性文件中

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

然后,作为构建后的操作,我进行了在其他项目上触发参数化的构建"调用第二项工作,并向其传递了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.

然后在作业2(这是一个多配置作业)上,我使用通过属性文件传递给作业2的环境变量添加了动态轴.

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.

这将复制Job 2并每次使用groovy脚本建立的值执行该作业,我可以在我的mvn参数中引用该值.

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.

这篇关于Jenkins:如何从标签获取节点名称以用作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆