Ideas to implement dynamic parallel build using jenkins pipeline plugin

心已入冬 提交于 2019-12-03 15:50:12

You can use Groovy to read the file from the workspace (readFile) and then generate the map containing the different closures, similar to the following:

parallel(
  task2: {
    node {
      unstash('my-workspace')
      sh('...')
    }
  },
  task3: {
    node {
      unstash('my-workspace')
      sh('...')
    }
  }
}

In order to generate such data structure, you simply iterate over the task data read using XML parsing in Groovy over the file contents you read previously.

By occasion, I gave a talk about pipelines yesterday and included very similar example (presentation, slide 34ff.). In contrast, I read the list of "tasks" from another command output. The complete code can be found here (I avoid pasting all of this here and instead refer to this off-site resource).

The kind of magic bit is the following:

def parallelConverge(ArrayList<String> instanceNames) {
    def parallelNodes = [:]

    for (int i = 0; i < instanceNames.size(); i++) {
        def instanceName = instanceNames.get(i)
        parallelNodes[instanceName] = this.getNodeForInstance(instanceName)
    }

    parallel parallelNodes
}

def Closure getNodeForInstance(String instanceName) {
    return {
        // this node (one per instance) is later executed in parallel
        node {
            // restore workspace
            unstash('my-workspace')

            sh('kitchen test --destroy always ' + instanceName)
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!