groovy 带有发布顺序的Gradle发布组合构建

0ve6wy6x  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(171)

我在以下目录结构中有一个gradle composite build

.
├── component-1
├── component-2
├── component-3
└── composite-build

这里所有的组件都是multi-project builds,每个组件都实现了release plugin,可以单独发布,composite-build有以下settings.gradle

rootProject.name = 'composite-build'

includeBuild '../component-1'
includeBuild '../component-2'
includeBuild '../component-3'

我想以特定的顺序执行每个组件的release任务,但我找不到合适的方法。以下是我尝试过但失败的一些案例。在这些案例中,个别release任务也会因为发布插件所做的git标签提交冲突而失败。

案例1:

单位:build.gradle

task releaseAll {
    dependsOn gradle.includedBuild('component-1').task(':release')
    dependsOn gradle.includedBuild('component-2').task(':release')
    dependsOn gradle.includedBuild('component-3').task(':release')
}

案例2:

单位:build.gradle

task releaseComponent1 {
    dependsOn gradle.includedBuild('component-1').task(':release')
}

task releaseComponent2 {
    dependsOn gradle.includedBuild('component-2').task(':release')
}

task releaseComponent3 {
    dependsOn gradle.includedBuild('component-3').task(':release')
}

releaseComponent2.configure {
    mustRunAfter releaseComponent1
}

releaseComponent3.configure {
    mustRunAfter releaseComponent2
}

task releaseAll {
    dependsOn releaseComponent1
    dependsOn releaseComponent2
    dependsOn releaseComponent3
}

错误消息示例:

> Task :component-1:component-1-release:preTagCommit FAILED
Running [git, commit, -m,  [Gradle Release Plugin] - pre tag commit:  'component-1-0.1.10'., -a] produced an error: []
Running [git, push, --porcelain, origin, main] produced an error: [error: failed to push some refs to 'https://github.com/<username>/<repo-name>.git']

> Task :component-1:release FAILED
Release process failed, reverting back any changes made by Release Plugin.
Running [git, checkout, gradle.properties] produced an error: [error: pathspec 'gradle.properties' did not match any file(s) known to git]

1: Task failed with an exception.
-----------

* What went wrong:

Execution failed for task ':component-1:component-1-release:preTagCommit'.
> Failed to push to remote - [To https://github.com/<username>/<repo-name>.git
  !     refs/heads/main:refs/heads/main [remote rejected] (cannot lock ref 'refs/heads/main': is at <xxxx> but expected <yyyy>)
  Done
  ][error: failed to push some refs to 'https://github.com/<username>/<repo-name>.git'
  ]
kkih6yb8

kkih6yb81#

Gradle始终提供一个选项,您可以使用finalizedBy自动执行并链接任务执行,您还可以测试这段简单的代码以检查其工作方式:

task A() {
    println 'taskA'
}
task B() {
    println 'taskB'
}
tasks.named("B") { finalizedBy("A") }

task C() {
    println 'taskC'
}
tasks.named("C") { finalizedBy("B") }

这样,通过调用gradle C,它将回溯执行A,然后执行B,最后执行C
同样,您可以执行3个任务来获得所需的有序执行,如下所示

task c1release(type:Exec) {
  workingDir '.\\component-1'
  commandLine("cmd", "/c", "gradle", "release")
     doLast {
         println "component-1 release is being excuted"
     }
}
tasks.named("c1release") { finalizedBy("c2release") }

task c2release(type:Exec) {
  workingDir '.\\component-2'
  commandLine("cmd", "/c", "gradle", "release")
     doLast {
         println "component-2 release is being excuted"
     }
}
tasks.named("c2release") { finalizedBy("c3release") }

task c3release(type:Exec) {
  workingDir '.\\component-3'
  commandLine("cmd", "/c", "gradle", "release")
     doLast {
         println "component-3 release is being excuted"
     }
}
tasks.named("releaseAll") { finalizedBy("c1release") }

task releaseAll() {
    println "Start releasing"
}

通过调用gradle releaseAll,它将首先执行c1release,然后执行c2release,最后执行c3release
但我还是希望我能知道你在问题中所包括的案例中有什么样的错误。

相关问题