Jenkins :- Run a remote command on all Jenkins slaves via Master

Hi everyone 😀, Today I want to share a solution related to run a common command in all slave nodes via the master node in Jenkins using the Jenkins pipeline. I also faced a similar issue before where I had to run docker related commands in all nodes. The approach is simple 

First, we need to define an agent in our Jenkins pipeline.

Second, we need to define two stages first one for the master (optional) and the second one are for slaves.

Third, we need some built-in functions

  • jenkins.model.Jenkins.instance.getNodes():-  Returns all Nodes in the system, excluding Jenkins instance itself which represents the built-in node
  • slave.toComputer().isOnline():-  To check slave node is online
  • getDisplayName():-  To get the name of the slave node
thereafter we create a user-define function and in this,  we define two loops first one for getting all names of slave nodes and the second one is to define a node-block that is capable of executing commands in the slave nodes. The function will look something like in the given below:-

Full code is given below


Hope you enjoyed my blog post. 😎

Comments

Popular posts from this blog

How to pass parameters in webhook?

Access and modify all the resources of our Wiki.js using WikiJS API

Fahrenheit to Celsius