11

We are seeing duplicate builds triggered on Jenkins multibranch pipeline projects. Builds are normally triggered using push notifications from Bitbucket using this plugin: https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/server/overview

However, we are now seeing 'double' builds for some reason. If you look at the 2 builds that are triggered, one is triggered by a 'commit notification', and the other is triggered by 'Branch Indexing'.

What is causing the branch indexing, and why is it triggering a build? We are not adding or deleting branches, it's just a normal commit/push.

To make it more complicated, it's not happening all the time. At one point I thought it was only happening after merges, but that's not the case. Also, one way to stop it seems to be by deleting the build history for a job (which obviously isn't ideal).

We are setting properties on the job from the pipeline script, but only to discard old builds:

properties([buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '20'))])

Another source of confusion seems to be if one should have polling enabled (with no interval) in order for push notifications to work for the Git plugin. The documentation for the Git plugin indicates this is required, and indeed seems to be for 'normal' pipeline builds, but doesn't seem to be required for multibranch pipeline builds. Is this correct? From the Jenkins Git Plugin wiki:

This will scan all the jobs that:

Have Build Triggers > Poll SCM enabled. No polling Schedule is required. Are configured to build the repository at the specified URL Are configured to build the optionally specified branches or commit ID For jobs that meet these conditions, polling will be immediately triggered. If polling finds a change worthy of a build, a build will in turn be triggered.

We are using Bitbucket 4.8.4 and Jenkins 2.30 (and all the latest pipeline plugins).

Tim Webster
  • 684
  • 8
  • 18
  • This is just a guess: I have seen a similar problem if the clock on the repository server is ahead in time compared to the clock on the Jenkins server. You might try to add a Quiet Period (https://jenkins.io/blog/2010/08/11/quiet-period-feature/). – jherb Nov 17 '16 at 19:02
  • @jherb thanks for the info - but I couldn't find how to set quiet period in a pipeline build in the Jenkinsfile (we need to set it on the script because we are setting other build properties - this will overwrite any other settings set via the GUI) – Tim Webster Nov 21 '16 at 10:51
  • I guess you could just use something like `sleep 60` as in this example: https://jenkins.io/doc/pipeline/examples/#timestamper-wrapper (before you call the scm or git command) – jherb Nov 21 '16 at 22:31
  • did your 2 builds kick off immediately on push? – Chris DaMour Feb 27 '19 at 20:18

5 Answers5

7

As the other answer already suggested the "Do not allow concurrent builds" option is what you want. And you can get that via the Jenkinsfile:

 properties ([
      buildDiscarder(logRotator(artifactNumToKeepStr: '5', daysToKeepStr: '15')),
      disableConcurrentBuilds()
    ])

EDIT:

This is not the solution for the actual problem here. but it still seems helpful to some people, so I'll leave it as long as we don't have a better answer.

Reinhold
  • 546
  • 1
  • 3
  • 14
  • 1
    Yes but we want concurrent builds. Disabling them will just queue them up won't it? What I don't want is builds being triggered which shouldn't be. – Tim Webster Dec 02 '16 at 15:42
  • You are right about queuing. I'll edit my answer so that it's at least clear it doesn't answer your specific question. – Reinhold Dec 05 '16 at 11:52
  • This is all I really ever wanted. I just kept getting frustrated because google kept sending me to the "Throttle concurrent builds" plugin. But that's not my real problem, I just wanted this. Thx for leaving the answer up. – djhaskin987 Apr 16 '19 at 18:28
2

In the Jenkins Multi-branch project config change "Discover branches" from "All branches" to "Exclude branches filed as PR".

Ivan Bilan
  • 2,379
  • 5
  • 38
  • 58
1

I'm using a multi-branch pipeline too and I may have some information for you.

When you create a "GitHub organization" job in Jenkins and add the relevant Git information, you are also required to select how Jenkins builds are triggered and how often:

enter image description here

Jenkins will scan the repositories under your configured GitHub organization as often as you set it.

Then, Jenkins will automatically trigger a build as soon as it finds that a new commit has been committed (as long as the Jenkins-Git webhook is configured).

When branch indexing takes place, it also triggers a build.

In other Jenkins job styles you are able to configure "Do not allow concurrent builds" but I couldn't find this setting for a multi-branch job style.

I hope this sheds some light on the matter.

Itai Ganot
  • 5,873
  • 18
  • 56
  • 99
  • 3
    that setting (Periodically if not otherwise run) seems to be a backup for failed push notifications. For example, if the build doesn't receive any notifications after 1 day then run indexing. This isn't what we need. Also, 'do not allow concurrent builds' isn't what we need either - we want concurrent builds. The problem we're having is that a push notification seems to be triggering 2 builds - one which builds the change (correctly), and another one that doesn't list any changes (it just says 'Branch Indexing)... – Tim Webster Nov 21 '16 at 10:57
0

You indicated you're using https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/server/overview. If you're still using that plugin, I suspect you'll be happier with what now seems to be the supported plugin for jenkins/stash (aka bitbucket server) integration: https://marketplace.atlassian.com/plugins/nl.topicus.bitbucket.bitbucket-webhooks/server/overview. Note how it says you need "at least this commit of the bitbucket-webhooks-plugin for Bitbucket server" in the bitbucket branch source plugin docs. We're using it and I've never seen two builds be triggered by the same push notification. When you trigger a build manually, yes, but not by the same push notification. :)

burnettk
  • 13,557
  • 4
  • 51
  • 52
0

You can add property "Supress Automatic SCM Trigger" to job configuration:

Supress Automatic SCM Trigger Property

This will prevent builds to execute due to "Branch Indexing".

The downside is that you may need to manually trigger the branch specific build at least once to get the configuration applied from Jenkinsfile.

Jose Sa
  • 21
  • 4