Jenkins任务为远程主机

时间:2016-09-23 11:45:21

标签: jenkins continuous-deployment devops jenkins-pipeline jenkins-job-dsl

在部署场景中,我需要在主机列表上创建和运行jenkins任务,即创建类似参数化任务(其中ip地址是参数)或具有HOST轴的Multijob Plugin上的任务,但仅运行在多个主机上并行2个。

其中一个选项可能是使用主机列表运行ansible,但我希望分别查看每个主机的状态,并在需要时重新启动jenkins作业。

主要选项是使用Job DSL PluginPipeline Plugin,但在这里我需要帮助来理解应该使用dsl groovy代码的类/方法来实现这一点。
任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

假设主机已经配置为Jenkins从属设备。 假设在管道作业参数中提供了主机 HOSTS作为空格分隔列表。以下示例应该让您入门:

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
}  

答案 1 :(得分:2)

Matrix projectThrottle Concurrent Builds Plugin的组合是可能的。

您只需要设置一个用户定义的轴(例如" targetHost"),并将所有IP地址作为值,并在" Throttle Concurrent Builds"下设置所需的限制。 (请注意,必须启用"执行并发构建,如果需要"选项告诉jenkins允许并发执行)。

在相应的环境变量(例如targetHost)中的每个子构建期间,轴值都可用。

下面是一个示例config.xml,其中包含简单的ping& wait构建步骤:

<?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 7
ping -c 7 $targetHost
sleep 7</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers/>
  <executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
    <runSequentially>false</runSequentially>
  </executionStrategy>
</matrix-project>
祝你好运!