如何在 Jenkins 中执行多个 Gradle 任务,或者串接多个 Gradle 任务?

How can I perform multiple Gradle tasks in Jenkins, or string multiple Gradle tasks?

我有一个 Jenkins 构建,其中“目标”(或 Gradle 中的任务)作为环境变量传递到我的 Jenkinsfile。

例如sh "gradle ${GRADLE_TASKS}" 其中 GRADLE_TASKS 的值为 "clean build" 将 运行 gradle clean build.

在我开始子项目之前,这非常有效。

假设我有子项目

root
|-- com.mycompany
|-- com.mycompany.api
\-- com.mycompany.ui

据我所知,如果我想 运行 在 com.mycompany.api 上进行干净的构建,我需要命令:

gradle :com.mycompany.api:clean :com.mycompany.api:build

我的问题是传递的变量可能代表 运行.cleanbuildclean builddeploy 的一项(或多项)任务, clean deploy, clean publish deploy 等等

Note: I need to keep this to a solution that uses the Jenkinsfile (not the Gradle plugin, for example) because it is a hybrid build that doesn't only utilise Gradle.

我的第一直觉是循环执行任务(如下所示),但 Jenkins 似乎不允许您 运行 像这样循环。

def tasks = GRADLE_TASKS.split(' ')
tasks.each { task ->
    sh "gradle :com.mycompany.api:${task}"
}

我该怎么办?

[edit] 我也应该指定,有没有办法 运行 这个嵌套,这样我就可以 clean 所有子项目然后 build?

例如相当于:

gradle :com.mycompany.api:clean :com.mycompany.ui:clean
gradle :com.mycompany.api:build :com.mycompany.ui:build

这样做(如下),但显然 Jenkins 不喜欢 groovy 闭包...

tasks.each { task ->
    subprojects.each {project -> 
      gradle :${project}:${task}
    }
}

看起来 @tim_yates 提供的答案在某种程度上以非常 'groovy-like' 的方式回答了问题。

sh "gradle ${GRADLE_TASKS.split(' ').collect { ":com.mycompany.api:${it}" }.join(' ')}"