Skipping the first build on first branch indexing

Félix Belzunce Arcos's picture

Skipping the first build on first branch indexing

Contributing to the Build Branch Strategies

Giving some context… 

Since the beginning of the year, I have been helping the TSM (Technical Success Manager) team to perform some DevOps consultancy for one of the Spanish IBEX companies. In one of the engagements I did, I was asked to look at the possibility to skip the first build on the first branch indexing.

The person who was in direct contact with me during the engagement explained to me:

- You know, we are in an enterprise context, where Bitbucket organizations are very diverse, but in any case, there are usually dozens of repositories - each one with multiple branches and PRs. When we configure the Bitbucket Team/Project it triggers a lot of builds and we need to use a custom script to avoid to perform all those builds.

To be honest, I was thinking about investigating the issue, perhaps look for a workaround but never to really dig into the branch-api plugin. Mainly, because I almost never contributed to this part of Jenkins and also because to work on APIs it is usually very time-consuming… Also, being on site with a customer, it does not seem like a good idea to spend so much time on such kind of specific topic.

However, that same week in the same company, I heard some complaints about this on multiple conversations with different people. At the end of the week, I called one of my colleagues to ask him about this issue and he told me that there were more customers waiting for this well-known RFE (Request For Enhancement). At that moment, it was when I decided to spend time on this topic.

How to skip the first build on the first branch indexing…

The Jenkins RFE is JENKINS-38552 and it was implemented on the following two PRs.

  • https://github.com/jenkinsci/basic-branch-build-strategies-plugin/pull/4
  • https://github.com/jenkinsci/branch-api-plugin/pull/144

The enhancement is available since branch-api-2.3.0 and basic-branch-build-strategies-1.3.0 - so ensure you have those versions installed on Jenkins to be able to use the new build branch strategy,

Once you have the right versions of the two plugins, there should be on all the multi branches jobs, under the Build strategies section, a new option called Skip initial build on first branch indexing

By adding this Build strategy, you are disabling the default behavior in which the first build is always triggered.

Contributing to more Build Branch Strategies… 

To contribute to more build branch strategies is quite easy. You just need to look at the Build Branch Strategies plugin for any of the examples. 

The idea of this plugin is to decide if we should, or not, trigger a build once there is a branch indexing. For this, each build strategy extends from BranchBuildStrategy overriding the method isAutomaticBuild. To decide if the build should be triggered, we currently have the SCMSource the SCMHead, the SCMRevision and the TaskListener.


public boolean isAutomaticBuild(@NonNull SCMSource source, @NonNull SCMHead head, @NonNull SCMRevision currRevision,
                                    SCMRevision prevRevision, TaskListener taskListener)

Thanks to… 

CloudBees for the business hours used to contribute to the OSS community.

To Dario Villadiego for his continued support on this specific contribution since I was close to give up in the beginning of the investigation.