尝试循环遍历数组并将每个值传递到 gradle 任务中

MDa*_*alt 7 groovy gradle jenkins-job-dsl

我正在尝试做一系列本来应该很简单的事情,但却给我带来了很多痛苦。在较高的层次上,我想循环遍历一个数组并将每个值传递到一个 gradle 任务中,该任务应该返回一个自己的数组。然后我想使用这个数组来设置一些 Jenkins 配置。

我尝试了多种方法来实现这项工作,但这是我当前的设置:

project.ext.currentItemEvaluated = "microservice-1"

task getSnapshotDependencies {
    def item = currentItemEvaluated
    def snapshotDependencies = []

    //this does a load of stuff like looping through gradle dependencies,
    //which means this really needs to be a gradle task rather than a 
    //function etc. It eventually populates the snapshotDependencies array.

    return snapshotDependencies
}

jenkins {
    jobs {
        def items = getItems() //returns an array of projects to loop through
        items.each { item ->
           "${item}-build" {
                project.ext.currentItemEvaluated = item
                def dependencies = project.getSnapshotDependencies
                dsl {
                    configure configureLog()
                    //set some config here using the returned dependencies array
             }
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

我无法真正改变詹金斯块的设置方式,因为它已经很成熟,所以如果可能的话,我需要在该结构内工作。

我尝试了多种方法来尝试将变量传递到任务中 - 这里我使用项目变量。问题似乎是任务在詹金斯块之前评估,并且我无法弄清楚如何使用新设置的currentItemEvaluated变量再次正确评估任务。

关于我还可以尝试什么的任何想法?

MDa*_*alt 3

经过更多研究,我认为这里的问题是 Gradle 中没有“调用任务”的概念。Gradle 任务只是任务及其依赖项的图表,因此它们将按照仅遵循这些依赖项的顺序进行编译。

我最终不得不解决这个问题,而无需尝试调用 Gradle 任务(我有一个构建任务将相关数据打印到文件中,并且我的 jenkins 块从文件中读取)

这里