Reputation: 99
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!
Upvotes: 2
Views: 3407
Reputation: 788
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.
Upvotes: 2
Reputation: 21
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
}
}
}
}
}
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
Upvotes: 0