Skip Builds On Branch Indexing with Jenkins Multibranch Pipelines
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: