1

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!

2 Answers2

2

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.

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

Viktor Be
  • 658
  • 1
  • 11
  • 25
  • Thanks for this pointer. Since i am novice to this pipeline groovy code, i find difficult to implement this logic. Could you please share me an alternative approach to achieve this. Appreciate your help! – Saravanan Jothilingam Feb 19 '20 at 17:06
  • 1
    You are welcome. If you provide an example of what you already tried and it didn't work, I will try to figure out what is wrong. – Viktor Be Feb 20 '20 at 07:13
0

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

Jari Ypyä
  • 21
  • 1
  • 6