Search code examples
jenkinsjenkins-pipeline

Jenkins multithreading functions


I'm in need to run two defined functions parallel in Jenkins pipeline. As defined in jenkins, the keyword parallel used with jobs, seems don't work with function calling. What I've tried is -

def first_func(){
    echo "first function"
}

def second_func(){
    echo "second function"
}

node {
    task = [:]
    function_lists = ['first_func()', 'second_func()']
    stage ('build') {
        for (job in function_lists) {
            task[job] = { '${job}' }
        }
    
        parallel task
    }
}

don't actually call the functions. Is there any way to do so in jenkins?


Solution

  • Yes this can be achieved in below way:

    def first_func(){
        echo "first function"
    }
    
    def second_func(){
        echo "second function"
    }
    
    node {
        def task = [:]
        stage ('build') {
            // Loop through list
            ['first_func', 'second_func'].each { 
                 def a = it;
                  task[a] = { "${a}"()}
                  }
        
            parallel task
        }
    }
    

    Output : enter image description here