Jenkins Pipeline - Run job on parallel on multiple remote hosts

橙三吉。 提交于 2020-02-25 04:00:09

问题


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!


回答1:


An easy way to run a job on different machines in parallel is to use the declarative 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.




回答2:


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



来源:https://stackoverflow.com/questions/60302232/jenkins-pipeline-run-job-on-parallel-on-multiple-remote-hosts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!