Multibranch and configuration reload triggering rebuild (branch indexing)

I’m using Multibranch pipeline for building devs branches. I’m using JobDSL for configuring this pipeline together with JCasC reload feature (https://github.com/jenkinsci/configuration-as-code-plugin/blob/04d018ef0a6da25f59d0910cc671bf2c015a1f23/docs/features/configurationReload.md - POST option) to manage my Jenkins instance.

The problem is every time when I execute a configuration reload this forces Jenkins to “Branch Indexing” which for unknown reason triggers rebuild for all branches. Whole rebuild takes a lot of time and makes Jenkins reconfiguration procedure nightmare.

This is a part of the log for “Branch Indexing” event which causes this massive rebuild. Unfortunately, I do not know what and why Jenkins lose track of previous source:

Takeover for [pipeline » branch-3549](https://jenkins:8443/job/pipeline/job/branch-3549/) by source #1 from source that no longer exists Branch reopened: branch-3549 (eedac86891fb53ce6e6bf89c09583c00eb1490e1) Scheduled build for branch: branch-3549

I fully understand that when Jenkins loose “branch source” then it needs to rebuild all, but why “branch source” disappear in the first place? What to change to avoid these massive rebuilds?

This is my JobDSL configuration of the pipeline.

jobs:
  - script: >
      multibranchPipelineJob('pipeline') {
        displayName('pipeline')
        branchSources {
          branchSource {
            source {
              bitbucket {
                id = "pipeline-build"
                credentialsId("bitbucket")
                repoOwner("repos")
                repository("pipeline")
                serverUrl("http://bitbucket:7990")
                traits {
                  authorInChangelog()
                  bitbucketSshCheckout {
                    // Credentials used to check out sources.
                    credentialsId('bitbucket_key')
                  }
                  bitbucketBranchDiscovery {
                    // Determines which branches are discovered.
                    strategyId(3)
                  }
                  headWildcardFilter {
                    includes('*/*')
                    excludes('master')
                  }
                }
              }
            }
          }
        }
        triggers {
          cron { spec('H H(1-4) * * *') }
          bitBucketMultibranchTrigger {}
        }
        orphanedItemStrategy {
          discardOldItems {
            numToKeep(0)
            daysToKeep(0)
          }
        }
      }

Radek

Jenkins setup:
Jenkins: 2.409
OS: Linux - 4.19.82-1300.strike.fc29.x86_64
Java: 17.0.7 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

ace-editor:1.1
ant:487.vd79d090d4ea_e
antisamy-markup-formatter:159.v25b_c67cd35fb_
apache-httpcomponents-client-4-api:4.5.14-150.v7a_b_9d17134a_5
apache-httpcomponents-client-5-api:5.2.1-1.0
authentication-tokens:1.53.v1c90fd9191a_b_
bitbucket:223.vd12f2bca5430
bitbucket-build-status-notifier:1.4.2
bitbucket-push-and-pull-request:2.8.3
blueocean:1.27.4
blueocean-autofavorite:1.2.5
blueocean-bitbucket-pipeline:1.27.4
blueocean-commons:1.27.4
blueocean-config:1.27.4
blueocean-core-js:1.27.4
blueocean-dashboard:1.27.4
blueocean-display-url:2.4.2
blueocean-events:1.27.4
blueocean-git-pipeline:1.27.4
blueocean-github-pipeline:1.27.4
blueocean-i18n:1.27.4
blueocean-jwt:1.27.4
blueocean-personalization:1.27.4
blueocean-pipeline-api-impl:1.27.4
blueocean-pipeline-editor:1.27.4
blueocean-pipeline-scm-api:1.27.4
blueocean-rest:1.27.4
blueocean-rest-impl:1.27.4
blueocean-web:1.27.4
bootstrap4-api:4.6.0-6
bootstrap5-api:5.3.0-1
bouncycastle-api:2.28
branch-api:2.1109.vdf225489a_16d
build-timeout:1.31
caffeine-api:3.1.6-115.vb_8b_b_328e59d8
checks-api:2.0.0
cloud-stats:267.v577e3742c282
cloudbees-bitbucket-branch-source:805.v7f97d29dc0f5
cloudbees-disk-usage-simple:178.v1a_4d2f6359a_8
cloudbees-folder:6.815.v0dd5a_cb_40e0e
command-launcher:100.v2f6722292ee8
commons-httpclient3-api:3.1-3
commons-lang3-api:3.12.0-36.vd97de6465d5b_
commons-text-api:1.10.0-36.vc008c8fcda_7b_
configuration-as-code:1647.ve39ca_b_829b_42
credentials:1254.vb_96f366e7b_a_d
credentials-binding:604.vb_64480b_c56ca_
data-tables-api:1.13.4-1
disk-usage:0.28
display-url-api:2.3.7
docker-build-step:2.9
docker-commons:419.v8e3cd84ef49c
docker-java-api:3.3.1-79.v20b_53427e041
docker-plugin:1.4
docker-workflow:563.vd5d2e5c4007f
durable-task:507.v050055d0cb_dd
echarts-api:5.4.0-5
email-ext:2.98
envinject:2.901.v0038b_6471582
envinject-api:1.199.v3ce31253ed13
favorite:2.4.2
file-operations:113.vb_9472a_325a_92
font-awesome-api:6.4.0-1
generic-webhook-trigger:1.86.3
git:5.1.0
git-client:4.4.0
github:1.37.1
github-api:1.314-431.v78d72a_3fe4c3
github-branch-source:1725.vd391eef681a_e
gradle:2.8
handlebars:3.0.8
handy-uri-templates-2-api:2.1.8-22.v77d5b_75e6953
htmlpublisher:1.31
http_request:1.16
instance-identity:142.v04572ca_5b_265
ionicons-api:56.v1b_1c8c49374e
jackson2-api:2.15.2-350.v0c2f3f8fc595
jakarta-activation-api:2.0.1-3
jakarta-mail-api:2.0.1-3
javadoc:233.vdc1a_ec702cff
javax-activation-api:1.2.0-6
javax-mail-api:1.6.2-9
jaxb:2.3.8-1
jdk-tool:66.vd8fa_64ee91b_d
jenkins-design-language:1.27.4
jersey2-api:2.39.1-2
jira:3.10
jira-steps:2.0.165.v8846cf59f3db
jjwt-api:0.11.5-77.v646c772fddb_0
jnr-posix-api:3.1.16-2
job-dsl:1.84
jquery3-api:3.7.0-1
jsch:0.2.8-65.v052c39de79b_2
junit:1207.va_09d5100410f
ldap:682.v7b_544c9d1512
lockable-resources:1156.v5e9f897ece02
mailer:457.v3f72cb_e015e5
matrix-auth:3.1.8
matrix-project:789.v57a_725b_63c79
maven-plugin:3.22
mercurial:1260.vdfb_723cdcc81
metrics:4.2.18-439.v86a_20b_a_8318b_
mina-sshd-api-common:2.10.0-69.v28e3e36d18eb_
mina-sshd-api-core:2.10.0-69.v28e3e36d18eb_
momentjs:1.1.1
multiple-scms:0.8
nexus-artifact-uploader:2.14
nomad:0.10.0
okhttp-api:4.11.0-145.vcb_8de402ef81
pam-auth:1.10
permissive-script-security:0.7
pipeline-build-step:496.v2449a_9a_221f2
pipeline-github-lib:42.v0739460cda_c4
pipeline-graph-analysis:202.va_d268e64deb_3
pipeline-groovy-lib:656.va_a_ceeb_6ffb_f7
pipeline-input-step:468.va_5db_051498a_4
pipeline-milestone-step:111.v449306f708b_7
pipeline-model-api:2.2141.v5402e818a_779
pipeline-model-definition:2.2141.v5402e818a_779
pipeline-model-extensions:2.2141.v5402e818a_779
pipeline-rest-api:2.32
pipeline-stage-step:305.ve96d0205c1c6
pipeline-stage-tags-metadata:2.2141.v5402e818a_779
pipeline-stage-view:2.32
pipeline-utility-steps:2.15.4
plain-credentials:143.v1b_df8b_d3b_e48
plugin-util-api:3.3.0
popper-api:1.16.1-3
popper2-api:2.11.6-2
prometheus:2.2.3
pubsub-light:1.17
resource-disposer:0.22
scm-api:672.v64378a_b_20c60
script-security:1251.vfe552ed55f8d
skip-certificate-check:1.1
snakeyaml-api:1.33-95.va_b_a_e3e47b_fa_4
sse-gateway:1.26
ssh-credentials:305.v8f4381501156
ssh-slaves:2.877.v365f5eb_a_b_eec
sshd:3.303.vefc7119b_ec23
stashNotifier:1.28
structs:324.va_f5d6774f3a_d
throttle-concurrents:2.13
timestamper:1.25
token-macro:359.vb_cde11682e0c
trilead-api:2.84.v72119de229b_7
variant:59.vf075fe829ccb
view-job-filters:369.ve0513a_a_f5524
windows-slaves:1.8.1
workflow-aggregator:596.v8c21c963d92d
workflow-api:1213.v646def1087f9
workflow-basic-steps:1017.vb_45b_302f0cea_
workflow-cps:3691.v28b_14c465a_b_b_
workflow-durable-task-step:1247.v7f9dfea_b_4fd0
workflow-job:1308.v58d48a_763b_31
workflow-multibranch:756.v891d88f2cd46
workflow-scm-step:415.v434365564324
workflow-step-api:639.v6eca_cd8c04a_a_
workflow-support:839.v35e2736cfd5c
ws-cleanup:0.45

Hi,
maybe this will help (added after branchSource)

buildStrategies {
  buildAllBranches {
    strategies {
      skipInitialBuildOnFirstBranchIndexing()
    }
  }
}

Thanks for suggestion. I’ll test it and share my findings.

I had an other issue with multibranch pipeline before when I set two multibranch pipelines with the same source ID, then every Branch Indexing trigger all branches build because “source changed”. I resolved it with unique source ID.

Here I have a single multibranch pipeline in this instance.

Unfortunately the solution with skipInitialBuildOnFirstBranchIndexing() did not worked - I was unable to setup it in my instance. Documentation in this area is very sparse. But I think I managed it to work with help of: Loading... where I incorrectly setup the branch source id parameter for branch source.
As you can see in my configuration I did id = "pipeline-build", which is I think incorrect despite Jenkins JobDSL did not complain about this syntax. To fix I simply changed it to: id("pipeline-build") and this do the trick. As this multibranch pipeline is the only one in this instance this id-string is unique.