Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

In deploy scenario I need to create and run Jenkins task on the list of hosts, i.e. create something like a parametrized task (where ip address is a parameter) or a task on Multijob Plugin with HOST axis, but run by only 2 ones in parallel over multiple hosts. 

One of the options could be to run ansible with the list of hosts, but I'd like to see a status per each host separately and relaunch a Jenkins job if needed.

The main option is to use Job DSL Plugin or Pipeline Plugin, but here I need help to understand what classes/methods of dsl groovy code should be used to achieve this. 
Can anyone help with it?

1 Answer

0 votes
by (50.2k points)

Let's start solving this by assuming that we have configured the hosts to Jenkins master as Jenkins slaves, and also make sure that hosts are provided in the pipeline job parameter.

Make HOSTS as a separated list. Go through the code 

def hosts_pairs = HOSTS.split().collate(2)
for (pair in host_pairs) {
  def branches = [:]
  for (h in pair) {
    def host = h  // new variable for every iteration; it will be mutated
    branches[host] = {
      stage(host) {
        node(host) {
          // here we mention the actual job,e.g. 
          sh "echo hello world"
        }
      }  
    }
  }
  parallel branches
}
The above code will help you to understand what are the classes/methods in the dsl-groovy code.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...