7

We're seeing multiple builds of our Jenkins multi-branch pipeline being triggered by the same merge commit in Bitbucket.

One build is marked triggered by a: "Branch event at "

The other by a: "commit notification "

We have our Jenkins urls setup in a plugin

Bitbucket Server Webhook to Jenkins

and have a our trigger in the Jenkinsfile setup as follows:

triggers {
    pollSCM ""
    cron "H 0 * * *"
}

There seem to be a couple of old questions that unfortunately don't have concrete answers.

Kai
  • 1,709
  • 1
  • 23
  • 36
  • Take a look at https://stackoverflow.com/questions/46561344/jenkins-build-triggered-twice-when-merging-branch. Maybe it will help you as the problem is similar. – Marcin Kłopotek Mar 13 '18 at 11:01

2 Answers2

3

We were using Bitbucket with Jenkins integration and had this issue. Our problem was that on the Bitbucket webhook we had selected notifications from push and from PR. This configuration created a Job with the branch name and a second job called PR-XXX when the PR was created.

Maybe this is the reason?

PaulG
  • 13,871
  • 9
  • 56
  • 78
  • 1
    Our problem was that two plugins both sent events that triggered a build. See my answer above for more info on how we solved it. – Kai Feb 02 '18 at 12:58
0

It looks like both the: Branch API Plugin and the Git client plugin were sending events to Jenkins that triggered a build.

We solved the issue by suppressing automatic triggering.

This can be done either in the UI simply by going to your job, selecting configure from the left then adding the property

Suppress automatic SCM triggering.

Alternatively for a code solution (which I ended up using) add it to the seedjob.groovy as follows:

multibranchPipelineJob("${service.name}-build") {
    // ... unrelated code omitted

    configure { project ->
        project.remove(project / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'strategy' / 'properties')
        def s = project / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'strategy' {
            properties(class: 'java.util.Arrays$ArrayList') {
                a(class: 'jenkins.branch.NoTriggerBranchProperty') {
                    'jenkins.branch.NoTriggerBranchProperty' ''
                }             
            }
        }
    }

}

UPDATE:

It seems like Julian's answer worked but had a bug where it didn't automatically trigger pushes to feature branches. In our Jenkinsfile we added:

properties([overrideIndexTriggers(true)])

Which ensures git commit still triggers a build despite NoTriggerBranchProperty in our seedjob.

ursa
  • 4,404
  • 1
  • 24
  • 38
Kai
  • 1,709
  • 1
  • 23
  • 36