我正在使用多分支管道项目来构建我的应用程序。我们有大约 100 多个组件和许多管道项目。所以说,例如我们有:
项目 A
依赖于 → 项目 B
依赖于 → 项目 C
我们正在对所有这些作业使用 SCM 轮询。我希望一旦构建项目 C
,就应该构建项目 B
,然后构建项目 A
。我的依赖项列表可能因项目而异。
我知道 Maven 项目可以使用“构建触发器”来处理此问题 → ☑ 每当构建 SNAPSHOT 依赖项时就进行构建,但是使用 Multibranch Pipeline 插件,没有找到任何方法来处理这个问题。
有什么指点吗?
最佳答案
分支的配置页面中有 ☐构建其他项目后构建,可以选择但无法保存分支的配置(分支侧边栏的菜单项只是查看配置,而不是配置)。
以下内容作为 Jenkinsfile
应该可以实现您想要实现的目标。将其添加到您的所有分支。如果发生更改,请在 master
/trunk
上编辑它,并将其挑选到受这些更改影响的分支。
// From: Trigger dependent jobs in Multibranch Pipeline project
// http://stackoverflow.com/a/38151703/1744774
String[][] buildChains = [
['master'],
['branch1', 'master'],
['branch2', 'branch1', 'master'],
['no-build']
// ... further build chains ...
]
for ( buildChain in buildChains ) {
if ( buildChain[0].equalsIgnoreCase( env.BRANCH_NAME ) ) {
int depth = 0
for ( branch in buildChain ) {
String depthIndicator = "+" * ++depth
//optional: String depthIndicator = new String(new char[++depth]).replace('\0', '+')
//optional: String depthIndicator = repeat( "+", ++depth )
println " $depthIndicator Triggering build for branch '$branch'"
build( branch )
} // for ( branches )
break // comment this if there are more build chains for one branch
}
} // for ( buildChains )
def build( String branch ) {
switch ( branch ) {
case "master":
buildMaster()
break
case ["branch1", "branch2"]:
buildBranch( branch )
break
// case ...
// ...
default:
println " --- No build defined for branch \'$branch\' ---"
} // switch ( branch )
} // build( branch )
def buildMaster() {
println ' Building branch \'master\'...'
// ... build code ...
}
def buildBranch( String branch ) {
println " Building branch '$branch'..."
// ... build code ...
}
// From: Can I multiply strings in Java to repeat sequences?
// http://stackoverflow.com/a/34650746/1744774
String repeat( String s, int count ) {
return count > 0 ? s + repeat( s, --count ) : ""
}
在branch1
上运行它:
[Pipeline] echo
+ Triggering build for branch 'branch1'
[Pipeline] echo
Building branch 'branch1'...
[Pipeline] echo
++ Triggering build for branch 'master'
[Pipeline] echo
Building branch 'master'...
[Pipeline] End of Pipeline
Finished: SUCCESS
在branch2
上运行它:
[Pipeline] echo
+ Triggering build for branch 'branch2'
[Pipeline] echo
Building branch 'branch2'...
[Pipeline] echo
++ Triggering build for branch 'branch1'
[Pipeline] echo
Building branch 'branch1'...
[Pipeline] echo
+++ Triggering build for branch 'master'
[Pipeline] echo
Building branch 'master'...
[Pipeline] End of Pipeline
Finished: SUCCESS
请记住使用Script Security Plugin批准最初禁止的功能。 管理 Jenkins → 进程内脚本批准。
关于jenkins - 触发多分支管道项目中的依赖作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140074/