3

I am using Multibranch Pipeline projects to build my applications. We have around 100+ components and those many pipeline projects. So say for e.g. we have:

Project A dependent on → Project B dependent on → Project C

We are using SCM polling for all these jobs. I want that once I build Project C, Project B should be built and after that Project A should be built. My list of dependencies can vary from project to project.

I know that a Maven project can handle this with Build Triggers → ☑ Build whenever a SNAPSHOT dependency is built, but with Multibranch Pipeline plugin, did not find any way to handle this.

Any pointers?

daspilker
  • 8,154
  • 1
  • 35
  • 49
AWS Enthusiastic
  • 176
  • 4
  • 14

1 Answers1

2

There is ☐ Build after other projects are built in the branches' configuration pages and it can be selected but the branches' configurations cannot be saved (The menu item in the branches' side bar reads just View Configuration, not Configure).

The following as Jenkinsfile should do what you're trying to achieve. Add it to all of your branches. In case of changes edit it on your master/trunk and cherrypick it to branches that are affected by these changes.

// 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 ) : ""
  }

Running it on 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

Running it on 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

Remember to approve initially prohibited functions with the Script Security Plugin under Manage JenkinsIn-process Script Approval.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107