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

Build not being prevented to run when it is the first build of that branch

      I have a filter like this: 

      If the build is the first build of a given branch OR a change request/PR, the build gets triggered even if the conditions don't pass.

      On the log, I can see the following
      Matched included region:project-a/** with file path:project-a/Jenkinsfile

      But the PR doesn't have any change under project-a folder, and it still gets recognized as it has.

          [JENKINS-60882] Build not being prevented to run when it is the first build of that branch

          Jesse S added a comment -

          Running into the same thing, is there any idea as to why this is occurring?

          Jesse S added a comment - Running into the same thing, is there any idea as to why this is occurring?

          Felipe Santos added a comment -

          I think changeSet is not properly populated in such cases. I did not find any workaround, but in case you're using Gerrit Code Review plugin, you can now filter the changes using the Gerrit query, where you can specify the path of files to watch.

          https://issues.jenkins-ci.org/browse/JENKINS-60876

          Felipe Santos added a comment - I think changeSet is not properly populated in such cases. I did not find any workaround, but in case you're using Gerrit Code Review plugin, you can now filter the changes using the Gerrit query, where you can specify the path of files to watch. https://issues.jenkins-ci.org/browse/JENKINS-60876

          Jesse S added a comment - - edited

          Thanks for the suggestion, but I am not. I think it might be triggered because it is passed the whole git history for a branch when a new branch is created. The plugin does not compare this branch to any others so it assumes that the complete branch history is new. My guess is that we would need to configure a 'main' branch to compare its to and only use the new changes.

          Jesse S added a comment - - edited Thanks for the suggestion, but I am not. I think it might be triggered because it is passed the whole git history for a branch when a new branch is created. The plugin does not compare this branch to any others so it assumes that the complete branch history is new. My guess is that we would need to configure a 'main' branch to compare its to and only use the new changes.

          Patrick McNerthney added a comment - - edited

          felipecassiors  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 felipecassiors   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
            felipecassiors Felipe Santos
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: