Search code examples
shelljenkinsnodesparallel-execution

How to hold Jenkins multiJob execution until chosen nodes are free?


I have a question about jenkins multijob possibilities:


current state:

  • I have 8 Jenkins nodes for job execution, 2 Linux and 6 Windows.
  • I have Multijob set up, consisting of 3 subJobs.
  • MultiJob setting: it has restriction to run only on Linux nodes
  • SubJob settings: n1 can run only on Win node1, n2 only on Win node2, n3 only on Win node3


Desired state:

  • When I build the multiJob, I need it to check and wait till Win nodes 1,2,3 are free
  • I need to execute subJobs 1,2,3 in the same time


this wouldn’t be problem, if all nodes were free...but if at least one of those three node is running some other job, it’s a problem already, because one subJob will be late compared to the other two

Is there any way to set up some pre-build script/another way to run subJobs only if all three chosen nodes are free/to wait for them to be free?

Thanks a lot for all ideas :)


Solution

  • You can check the status of the build executor on particular node as a pre-build action. If the build executor is idle, that means no job is running but if it's busy, something is running into it. Simple groovy script can be used for this purpose.

    import hudson.model.Node
    import hudson.model.Slave
    import jenkins.model.Jenkins
    
    Jenkins jenkins = Jenkins.instance
    def jenkinsNodes =jenkins.nodes
    
    for (Node node in jenkinsNodes) 
    {
    // Make sure slave is online
    if (!node.getComputer().isOffline()) 
    {           
        //Make sure that the slave busy executor number is 0.
        if(node.getComputer().countBusy()==0)
            {
           ...put your logic...
        }
    }
    

    }

    Thanks, Subhadeep