远程主机的Jenkins任务

远程主机的Jenkins任务,jenkins,continuous-deployment,devops,jenkins-pipeline,jenkins-job-dsl,Jenkins,Continuous Deployment,Devops,Jenkins Pipeline,Jenkins Job Dsl,在部署场景中,我需要在主机列表上创建并运行jenkins任务,即创建类似参数化任务(其中ip地址是一个参数)或HOST axis上的任务,但在多个主机上仅由两个主机并行运行 其中一个选项是使用主机列表运行ansible,但我希望分别查看每个主机的状态,并在需要时重新启动jenkins作业。 主要的选择是使用or,但这里我需要帮助理解应该使用哪些dsl groovy代码类/方法来实现这一点 有人可以帮忙吗?假设主机已经配置为Jenkins从属。 假设主机是在管道作业参数中提供的 以空格分隔的列表形

在部署场景中,我需要在主机列表上创建并运行jenkins任务,即创建类似参数化任务(其中ip地址是一个参数)或HOST axis上的任务,但在多个主机上仅由两个主机并行运行

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

主要的选择是使用or,但这里我需要帮助理解应该使用哪些dsl groovy代码类/方法来实现这一点

有人可以帮忙吗?

假设主机已经配置为Jenkins从属。 假设主机是在管道作业参数中提供的
以空格分隔的列表形式承载
。下面的示例应该让您开始学习:

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
}  
和的组合是可能的

您只需设置一个用户定义的轴(例如“targetHost”),将所有IP地址作为值,并在“节流并发构建”下设置所需的节流(请注意,您必须启用“必要时执行并发构建”选项,以告知jenkins允许并发执行)

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

下面是一个带有简单ping&wait构建步骤的config.xml示例:

<?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>

假的
2.
2.
真的
项目
假的
真的
真的
真的
假的
假的
假的
真的
目标论
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
睡眠7
ping-c 7$targetHost
睡眠7
假的

祝你好运

你不能用带轴的来表示IP地址吗?@vlp可能是的,但如何只并行运行2个而不是全部?我从来没有用过,但可能有用。它应该支持限制矩阵构建(在变更日志中提到)。祝你好运请你澄清一点,AFA我对groovy不太熟练:它应该是collapse而不是collate吗?不,collate是正确的。它将把列表划分为给定大小的子列表。例如,在这里,如果你有字符串
HOSTS=“foo-bar-baz-qux”
,在
split()
之后,你将有一个平面列表
[“foo”,“bar”,“baz”,“qux”]
,在
整理(2)
之后,你将有一个列表
[“foo”,“bar”],[“baz”,“quz”]