分支的配置页面有☐构建其他项目后构建,可以选择,但不能保存分支的配置(分支侧边栏中的菜单项显示为刚刚查看配置,而不是配置)。
以下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
记得在 Manage Jenkins → In-process Script Approval 下使用Script Security Plugin 批准最初禁止的功能。