Skip Builds On Branch Indexing with Jenkins Multibranch Pipelines

Featured image for sharing metadata for article

Jenkins has the ability to set up a multibranch pipeline which allows you to run configure building of your branches and Pull/Merge requests using the same configuration, which is very useful.

If you've ever worked with it, you'll likely have needed to hit the Scan Multibranch Pipeline Now, which goes through each branch on your repository and will build it if it needs to. Although this can be very useful, it can also be quite frustrating, as if you have 10s of branches, these new builds will swamp the executors on your agents, or depending on how you run your pipelines, end up triggering costly Cloud deployments.

It turns out we can avoid this issue by adding the following into our Jenkinsfile for a Declarative pipeline:

// execute this before anything else, including requesting any time on an agent
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause')) {
  print "INFO: Build skipped due to trigger being Branch Indexing"
  currentBuild.result = 'ABORTED' // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful
  return
}

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

Or in our Scripted Jenkinsfile:

// execute this before anything else, including requesting any time on an agent
if (currentBuild.getBuildCauses().toString().contains('BranchIndexingCause')) {
  print "INFO: Build skipped due to trigger being Branch Indexing"
  currentBuild.result = 'ABORTED' // optional, gives a better hint to the user that it's been skipped, rather than the default which shows it's successful
  return
}

node {
    stages {
        stage('Build') {
                echo 'Building..'
        }
        stage('Test') {
                echo 'Testing..'
        }
        stage('Deploy') {
                echo 'Deploying....'
            }
    }
}

Any builds triggered will simply be skipped as SUCCESS, not having executed anything:

Jenkins build console output showing the INFO message, but nothing in the job having executed

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #jenkins.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.