2
votes

I am running a Declarative Pipeline where one of the steps runs a (very long) integration test. I'm trying to split my test into several smaller ones and run them in parallel over several nodes. I have 8 of these smaller tests and I have 8 nodes (under a label), so I'd like to have each test run on a separate node. Unfortunately, two tests — when run on the same node — interfere with each other, and so both fail.

I need to be able to first get the list of available nodes, and then run the smaller tests in parallel, one of each node; if there are not enough nodes, one of the smaller tests need to wait until the node is finished.

However, what happens is that when asking for a node by label, two of the smaller tests usually get the same node, and so both fail. Nodes are configured to run up to 3 executors, otherwise the whole system halts, so I can't change that.

My current configuration for the smaller test is:

    stage('Integration Tests') {
        when {
            expression {params.TESTS_INTEGRATION}
        }
        parallel {
            stage('Test1') {
                agent {node {label 'my_builder'}}
                steps {
                    script {
                        def shell_script = getShellScript("Test1")
                        sh "${shell_script}"
                    }
                }
            }

I am able to get the list of available slaves from a label like this:

pipeline {
stages {
    // ... other stages here ...
    stage('NodeList'){
        steps {
            script {
                def nodes =  getNodeNames('my_builder')
                free_nodes = []
                for (def element = 0; element < nodes.size(); element++) {
                    usenode = nodes[element]
                    try { 
                      // Give it 5 seconds to run the nodetest function
                        timeout(time: 5, unit: 'SECONDS') { 
                            node(usenode) { 
                                nodetest() 
                                free_nodes += usenode
                            } 
                        }
                    } catch(err) { 

                    }
                }
                println free_nodes
            }
        }
    }

Where

def getNodeNames (String label) {
    def lgroup = Jenkins.instance.getLabel(label)
    def nodes = lgroup.getNodes()
    def result = []
    if (nodes.size() > 0) {
        for (def element = 0; element < nodes.size(); element++) {
            result += nodes[element].getNodeName()
        }
    }
    return result
}

def nodetest() { 
  sh('echo alive on \$(hostname)') 
}

How can I get the node name programmatically out of the free_nodes array and direct the stage to use that?

1

1 Answers

1
votes

I've figured it out, so for the people from the future:

It turns out you can run a Scripted Pipeline inside a Declarative Pipeline, like this:

pipeline {
    stage('SomeStage') {
        steps {
            script {
                // ... your scripted pipeline here
            }
        }
    }

The script can do anything, and that includes... running a pipeline!

Here is the script:

script {
    def builders = [:]
    def nodes =  getNodeNames('my_label')
    // let's find the free nodes
    String[] free_nodes = []
    for (def element = 0; element < nodes.size(); element++) {
        usenode = nodes[element]
        try { 
          // Give it 5 seconds to run the nodetest function
            timeout(time: 5, unit: 'SECONDS') { 
                node(usenode) { 
                    nodetest() 
                    free_nodes += usenode
                } 
            }
        } catch(err) { 
            // do nothing
        }
    }
    println free_nodes

    def tests = params.TESTS_LIST.split(',')

    for(int i = 0; i < tests.length; i++) {
        // select the test to run
        def the_test = tests[i]
        // select on which node to run it
        def the_node = free_nodes[i % free_nodes.length]

        // here comes the scripted pipeline: prepare steps
        builders[the_test] = {
            // run on the selected node
            node(the_node) {
                // lock the resource with the name of the node so two tests can't run there at the same time
                lock(the_node) {
                    // name the stage
                    stage(the_test) {
                        println "Running on ${NODE_NAME}"
                        def shell_script = getShellScript("${the_test}")
                        sh "${shell_script}"
                    }
                }
            }
        }
    }
    // run the steps in parallel
    parallel builders
}