简体   繁体   中英

Jenkins Pipeline - Run job on parallel on multiple remote hosts

I have a Jenkins job with string parameter Name = "HOST". I am trying to run one script remotely by connecting to the HOST. It works fine. Similarly, if I enter multiple host names in the HOST parameter, the job has to run on those multiple machines on parallel. How to achieve this?

If anybody has any code for this, please do share. Appreciate this help!

An easy way to run a job on different machines in parallel is to use thedeclarative Matrix .

Pipeline example:

 pipeline {
        agent none
        stages {
            stage('Matrix stage') {
                matrix {
                    agent {
                        label "${NODE}"
                    }
                    axes {
                        axis {
                            name 'NODE'
                            values 'node1', 'node2', 'node3'
                        }
                    }
                    stages {
                        stage('Parallel stage') {
                            steps {
                                echo "Run on ${NODE}"
                            }
                        }
                    }
                }
            }
        }
    }

This pipleline will execute the defined stages on ['node1', 'node2', 'node3'] in parallel.

Note that declarative Matrix is a native declarative Pipelines feature, so no additional Plugin installation needed.

Due not able to parametrize Matrix axis values, this could be one approach (declarative pipeline syntax with script block):

def deploys = [:]
def servers = ['host1','host2','host3']

pipeline {
    agent any
    stages {
        stage ('Deploying multiple hosts') {    
            steps {
                script {
                    servers.each { server ->
                        deploys[server] = {
                            sh "echo run stuff.."
                        }
                    }
                    parallel deploys
                }
            }
        }
    }
}

Blue Ocean imaging

Downside of this is you can't make real pipeline with multiple tasks dependent each other. I am looking answer for that question too.. How to loop parametrized parallel stages in Jenkins declarative pipeline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM