Jenkins task for remote hosts Jenkins task for remote hosts jenkins jenkins

Jenkins task for remote hosts


Assume that the hosts have been configured as Jenkins slaves already.Assume that hosts are provided in pipeline job parameterHOSTS as whitespace separated list. Following example should get you started:

def hosts_pairs = HOSTS.split().collate(2)for (pair in host_pairs) {  def branches = [:]  for (h in pair) {    def host = h  // fresh variable per iteration; it will be mutated    branches[host] = {      stage(host) {        node(host) {          // do the actual job here, e.g.           // execute a shell script          sh "echo hello world"        }      }    }  }  parallel branches}  


A combination of Matrix project and Throttle Concurrent Builds Plugin is possible.

All you need is to setup a single user-defined axis (e.g. "targetHost") with all IP addresses as values and set the desired throttling under "Throttle Concurrent Builds" (please note that you have to enable the "Execute concurrent builds if necessary" option to tell jenkins to allow concurrent execution).

The axis values are available during every child build in the corresponding environment variable (e.g. targetHost).

Below is an example config.xml with simple ping&wait build step:

<?xml version='1.0' encoding='UTF-8'?><matrix-project plugin="matrix-project@1.7.1">  <actions/>  <description></description>  <keepDependencies>false</keepDependencies>  <properties>    <hudson.plugins.throttleconcurrents.ThrottleJobProperty plugin="throttle-concurrents@1.9.0">      <maxConcurrentPerNode>2</maxConcurrentPerNode>      <maxConcurrentTotal>2</maxConcurrentTotal>      <categories class="java.util.concurrent.CopyOnWriteArrayList"/>      <throttleEnabled>true</throttleEnabled>      <throttleOption>project</throttleOption>      <limitOneJobWithMatchingParams>false</limitOneJobWithMatchingParams>      <matrixOptions>        <throttleMatrixBuilds>true</throttleMatrixBuilds>        <throttleMatrixConfigurations>true</throttleMatrixConfigurations>      </matrixOptions>      <paramsToUseForLimit></paramsToUseForLimit>    </hudson.plugins.throttleconcurrents.ThrottleJobProperty>  </properties>  <scm class="hudson.scm.NullSCM"/>  <canRoam>true</canRoam>  <disabled>false</disabled>  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>  <triggers/>  <concurrentBuild>true</concurrentBuild>  <axes>    <hudson.matrix.TextAxis>      <name>targetHost</name>      <values>        <string>127.0.0.1</string>        <string>127.0.0.2</string>        <string>127.0.0.3</string>        <string>127.0.0.4</string>        <string>127.0.0.5</string>      </values>    </hudson.matrix.TextAxis>  </axes>  <builders>    <hudson.tasks.Shell>      <command>sleep 7ping -c 7 $targetHostsleep 7</command>    </hudson.tasks.Shell>  </builders>  <publishers/>  <buildWrappers/>  <executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">    <runSequentially>false</runSequentially>  </executionStrategy></matrix-project>

Good luck!