new comer to Jenkins, for context I have a Jenkins set up that has the controller and 4 other nodes. Each node has it's own label(node1-4). Node3 has the label 'failover' and node4 has the label 'failover2'. What I would like to do is when a build is submitted the person submitting the build can set it to run on a specified node ex. node1. It should then try to run on node1 and if that node is unavailable/busy it then tries to run on node3 and if that node is unavailable/busy try node4, otherwise go into the queue and wait for the specified node to be available.
Not sure if this is possible or not but I have something along the lines of the below just before the pipeline starts. Right now this seems to almost act if it was agent any and seems to randomly select a node to build on.
import groovy.transform.Field
@Field
def BUILD_NODE = ""
if (params.DEBUG_JOB == 'true') {
BUILD_NODE = "${params.NODE_LABELS}"
} else {
BUILD_NODE = '${params.NODE_LABELS} || Failover || Failover2'
}
pipeline {
agent {label "${BUILD_NODE}"}
CodePudding user response:
How about something like the below. Here I'm checking whether each node has any idle executors. If Idle executors are there the getLabels()
function will return the label specific to that node. It will keep iterating until it finds an idling node if not it will schedule the Job in any node. Feel free to improve the code as needed.
def labelSelected = getLabels()
println("Running on Label:" labelSelected)
pipeline {
agent {
label "${labelSelected}"
}
stages {
stage('Build') {
steps {
echo "Running!!!"
}
}
}
}
def getLabels() {
def labelOrder = ["test", "label2", "label3"]
def whereToSchedule = "test || label2 || label3"
def jenkinsNodes = Jenkins.instance.getNodes()
for(String label : labelOrder) {
for(def node: jenkinsNodes) {
if(node.labelString.contains(label)){
if (!node.getComputer().isOffline()){
if(node.getComputer().countIdle() > 0){
println("The node does have executors")
whereToSchedule = label
break
}
}
}
}
}
return whereToSchedule
}