AKP
AKP

Reputation: 88

How to get the jenkins node is free or not in pipeline groovy script

Im running master and multiple slave nodes in my jenkins setup pipe line i have an requirement where i need to find the slave node is free (Need to confirm that there is no jobs are running on that node) or not. If it's free i need to perform some activity on that node. Any help would be appreciated

nodelist = ['master', 'node1', 'node2']
def builders = [:]
for (x in nodelist) {
    def nodelabel = x 
    builders[nodelabel] = {
        node(nodelabel) {
#how to get the node is free or not 
if free do some task 
else continue.
 

Upvotes: 1

Views: 1382

Answers (1)

MaratC
MaratC

Reputation: 6889

Edit: re-wrote the answer.

You can access the Computer object that you can get from a Node object.

for (Node node in Hudson.instance.nodes) {
    def nodeName = node.getNodeName()
    def computer = node.toComputer()
    if (computer  != null && node.toComputer().online) {
      println """node ${nodeName} 
     busy: ${computer.countBusy()} 
     idle: ${computer.countIdle()} 
     total: ${computer.countExecutors()} """
    }
}

Apparently nodes with countBusy() of 0 are absolutely free.

Upvotes: 3

Related Questions