Jenkins任务为远程主机

yvs*_*yvs 12 continuous-deployment jenkins jenkins-job-dsl devops jenkins-pipeline

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

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

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

jil*_*jil 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
}  
Run Code Online (Sandbox Code Playgroud)