Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-67128

Multibranch build strategy extension plugin does not work with initial builds

      We have 50 projects in a monorepo and we use multibranch-build-strategy-extension-plugin to trigger the correct builds based on commit contents.

      It does not work with initial (branch indexing) builds. For example, every time a new merge request is created, all of those 50 MR builds get unnecessarily triggered, no matter what is configured in the plugin strategies. Total waste of build resources.

      What I assume is happening is that the "previous" commit hash that it tries to compare against (to see changed files) is not yet set during the very first build. So the first build would need to be treated somehow differently for the plugin to see the changes between the MR commit hash and the MR's parent commit hash.

      Not sure if that can be fully done within the multibranch-build-strategy-extension-plugin, or does it need changes elsewhere.

          [JENKINS-67128] Multibranch build strategy extension plugin does not work with initial builds

          https://plugins.jenkins.io/multibranch-build-strategy-extension/ should be your friend here.

          In your job, you can then do Property strategy --> For matching branches suppress builds triggered by indexing.

          I can't get multibranch-build-strategy-extension-plugin to work at all, so can't verify the above.

          Brendan Holmes added a comment - https://plugins.jenkins.io/multibranch-build-strategy-extension/ should be your friend here. In your job, you can then do Property strategy --> For matching branches suppress builds triggered by indexing. I can't get multibranch-build-strategy-extension-plugin to work at all, so can't verify the above.

          Danny added a comment -

          Hello brendanh I don't fully understand your comment. Who is exactly "our friend"? The link on your comment's first line points to the very plugin under scrutiny.

          Danny added a comment - Hello brendanh I don't fully understand your comment. Who is exactly "our friend"? The link on your comment's first line points to the very plugin under scrutiny.

          Patrick McNerthney added a comment - - edited

          jmullo I ran into this same issue and what I did to tip toe around it was put a check at the start of the pipeline code that does the include/exclude check comparing the merge branch to the target branch, and immediately returns from the build if it does not match. This check only runs if it is a merge request build job #1.

          Below is the snippet of code run at the start of the build. This code resides in a global, trusted pipeline library:

          import jenkins.scm.api.SCMRevisionAction
          import org.jenkinsci.plugins.workflow.cps.CpsThreadGroup
          
          if (env.CHANGE_ID && currentBuild.number == 1) {
              boolean build = true
              def listener = CpsThreadGroup.current().execution.owner.listener
              currentBuild.rawBuild.getActions(SCMRevisionAction).each { action ->
                  if (build) {
                      def origin = action.revision.origin
                      def target = action.revision.target
                      currentBuild.rawBuild.project.parent.sources.each { source ->
                          if (build) {
                              if (source.source.id == action.sourceId && source.buildStrategies) {
                                  build = false
                                  source.buildStrategies.each { strategy ->
                                      if (!build) {
                                          build = strategy.automaticBuild(
                                              source.source, origin.head, origin, target, target, listener
                                          )
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
              if (!build) {
                  echo "Suppressing initial MR build due to no changes."
                  return
              }
          } 

           

          Patrick McNerthney added a comment - - edited jmullo I ran into this same issue and what I did to tip toe around it was put a check at the start of the pipeline code that does the include/exclude check comparing the merge branch to the target branch, and immediately returns from the build if it does not match. This check only runs if it is a merge request build job #1. Below is the snippet of code run at the start of the build. This code resides in a global, trusted pipeline library: import jenkins.scm.api.SCMRevisionAction import org.jenkinsci.plugins.workflow.cps.CpsThreadGroup if (env.CHANGE_ID && currentBuild.number == 1) {     boolean build = true     def listener = CpsThreadGroup.current().execution.owner.listener     currentBuild.rawBuild.getActions(SCMRevisionAction).each { action ->         if (build) {             def origin = action.revision.origin             def target = action.revision.target             currentBuild.rawBuild.project.parent.sources.each { source ->                 if (build) {                     if (source.source.id == action.sourceId && source.buildStrategies) {                         build = false                         source.buildStrategies.each { strategy ->                             if (!build) {                                 build = strategy.automaticBuild(                                     source.source, origin.head, origin, target, target, listener                                 )                             }                         }                     }                 }             }         }     }     if (!build) {         echo "Suppressing initial MR build due to no changes."         return     } }  

          Patrick McNerthney added a comment - I submitted a pull request to address this issue: https://github.com/jenkinsci/multibranch-build-strategy-extension-plugin/pull/52

            igalg Igal Gluh
            jmullo Jussi Mullo
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: