我有一组用于测试构建结果的虚拟机。所有VM都配置为Jenkins从属节点。主节点可以启动和停止VM。
虚拟机以三个为一组进行标记(cluster1
,cluster2
),并具有不同的角色(role1
,role2
,role3
)。
我想编写一个以GROUP名称作为参数(StringParameterValue)和
的管道脚本管道脚本看起来大致如下:
def VM1 = ??? get name of the node labeled ("GROUP && role1") ???;
def VM2 = ??? get name of the node labeled ("GROUP && role2") ???;
def VM3 = ??? get name of the node labeled ("GROUP && role3") ???;
def VMs = [VM1, VM2, VM3];
node (master) {
stage 'Start'
def start = [:];
for ( vm in VMs ) {
start["${vm}"] = {
build job: 'Start_VM', parameters: [
[$class: 'StringParameterValue', name: 'VMNAME', value: vm]];
}
}
parallel start;
def tests = [:];
for (int i = 0; i < VMs.size; i++) {
tests["role${i}"] = {
node (VMs[i]) {
build job: "Test_role${i}";
}
}
}
parallel tests;
Stage 'Stop'
def stop = [:];
for ( vm in VMs ) {
stop["${vm}"] = {
build job: 'Stop_VM', parameters: [
[$class: 'StringParameterValue', name: 'VMNAME', value: vm]];
}
}
parallel stop;
}
问:如何通过组标签派生单个节点名称?
答案 0 :(得分:7)
我实现了一个从标签中获取节点名称的函数:
def roles = ['role1', 'role2', 'role3'];
def getNodeNames (group, roles) {
def lgroup = Jenkins.instance.getLabel(group)
def result = [:]
for (role in roles) {
def lrole = lgroup.and(Jenkins.instance.getLabel(role))
def nodes = lrole.getNodes()
if (nodes.size() > 0) {
result[role] = nodes[0].getNodeName()
}
}
print "getNodeNames($group) = ${result}"
return result
}
def VMs = getNodeNames(GROUP)