Skip to content

[JENKINS-19022] Add option to keep builds-by-branch history only for matching branches - #4024

Open
PaulRoze wants to merge 2 commits into
jenkinsci:masterfrom
PaulRoze:JENKINS-19022-limit-history
Open

PaulRoze wants to merge 2 commits into
jenkinsci:masterfrom
PaulRoze:JENKINS-19022-limit-history

Conversation

@PaulRoze

Copy link
Copy Markdown

Fixes the growth described in JENKINS-19022 (#2308) for jobs that build one branch per build, without changing behaviour for anyone who does not enable it.

Every build copies the previous build's buildsByBranchName. A job that builds a different branch on each run, for example a parameterized branch specifier for merge requests, therefore stores one entry for every branch it ever built in every build.xml.

On one of our jobs (about 3700 retained builds, up to 1680 entries per map) each build.xml reached 2.9 MB. The GitLab plugin's already-built lookup loads every retained build, and it took about 6 minutes per merge request webhook. Clearing the map with the script from the README fixed it, which is what led to this change.

This adds an opt-in extension, limitBuildsByBranchHistory(), and a matching multibranch trait. Right after GitSCM copies the previous build's BuildData, the extension removes the entries whose branch matches none of the job's branch specifiers. The key is tried in the forms the build chooser accepts (origin/x, refs/remotes/origin/x, remotes/origin/x, x, refs/heads/x).

Compatibility, given why #578 was reverted (#689):

  • The BuildData action is still added to every build. Its class, fields, serialization and lastBuild are unchanged, so plugins that read it keep working.
  • The only change to existing code is one import and a 4-line block in GitSCM._checkout that runs only when the extension is configured. Without the extension nothing changes.
  • Only the default and ancestry build choosers are affected. Other choosers, such as the inverse chooser or third-party choosers, keep the full history.

Trade-off when enabled, documented in the help and README. With a parameterized specifier the history of other branches is dropped, so:

  • the changelog, GIT_PREVIOUS_COMMIT and GIT_PREVIOUS_SUCCESSFUL_COMMIT of a branch start fresh when it is built again after other branches;
  • a job that also polls schedules a build of the default branch after each build of another branch.

Wildcard specifiers keep every matching entry, so the option does not reduce their history. Their polling is unaffected.

This follows the opt-in direction of #568, which was closed by its author in favour of #578. Unlike #568, it keeps the entries that still match the branch specifiers, so wildcard jobs keep their already-built detection.

Testing done

  • mvn clean verify: 5290 tests, 0 failures, SpotBugs 0.

  • New tests in LimitBuildsByBranchHistoryTest and LimitBuildsByBranchHistoryPipelineTest:

    • default behaviour is unchanged;
    • a parameterized job keeps one entry per build and build.xml stays flat;
    • a wildcard job keeps its history and polls clean;
    • the polling trade-off is pinned by a test;
    • every branch specifier form keeps its own entry;
    • the inverse chooser is left alone;
    • a Pipeline checkout scmGit(...) run.
  • Side-by-side Jenkins 2.580.2 runs comparing released 5.11.0 with this branch, option off. Installed plugins: workflow-aggregator, gitlab-plugin 1.2162, git-parameter, git-tag-message 1.7.1, github 1.47, blueocean-autofavorite. Over 50 builds and 25 polls, the following were identical: BuildData in build.xml, the api/json git fields, the GIT_* variables, changelogs, and polling decisions.

  • Option on: these return the same results as with the option off:

    • gitlab-plugin BuildUtil lookups by SHA and by branch;
    • github BuildDataHelper;
    • git-tag-message variables;
    • lastBuiltRevision in api/json (JENKINS-55939);
    • the /git/ page.
  • Not exercised: gitlab-plugin CommitStatusUpdater (needs a live GitLab), and blueocean-autofavorite (it only acts on multibranch jobs).

  • Scale test: two Pipeline jobs, each with two checkouts, built across 300 branches.

    option off option on
    last build.xml 109 KB 7.9 KB
    buildsByBranchName size 111 1
    load of all builds after restart 563 ms 100 ms

@since TODO is left for the maintainer to set.

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

…matching branches

Every build copies the previous build's buildsByBranchName, so a job that
builds a different branch on each run (for example a parameterized branch
specifier for merge requests) stores one entry per branch it ever built in
every build.xml. On a job with ~3700 retained builds this reached 2.9 MB per
build and made the GitLab plugin's already-built lookup take minutes per
webhook.

The new opt-in extension (and multibranch trait), limitBuildsByBranchHistory,
drops entries whose branch matches none of the branch specifiers right after
the history is copied. lastBuild is kept, so BuildData consumers that match on
it are unaffected, and the default behaviour does not change. Only the default
and ancestry build choosers are affected; other choosers keep the full history.
@PaulRoze
PaulRoze requested a review from a team as a code owner November 18, 2026 12:22
@github-actions github-actions Bot added documentation Improvements or additions to documentation tests Automated test addition or improvement labels Nov 18, 2026
@PaulRoze

Copy link
Copy Markdown
Author

Hi @MarkEWaite,

Can you review this PR when you have time?

  1. The option is off by default. When it is off, the plugin works the same as 5.11.0. I compared the two side by side on Jenkins 2.580.2.
  2. When it is on, each build keeps only the branches that match the branch specifier. In a test with 300 branches, build.xml went from 109 KB to 7.9 KB.
  3. All CI checks pass.

Questions:

  1. Is an opt-in option the correct direction for JENKINS-19022? If you want a different design, tell me and I will change it.
  2. Can you add the enhancement label? The labeler added only documentation and tests.
  3. Do you want a screenshot of the new option in the README, like the other options?

Thank you.

@MarkEWaite MarkEWaite added enhancement Improvement or new feature and removed documentation Improvements or additions to documentation tests Automated test addition or improvement labels Nov 24, 2026
@MarkEWaite

Copy link
Copy Markdown
Contributor

Can you review this PR when you have time?

There are many places requesting my time right now. The git plugin is lower on my priority list than Jenkins core and Jenkins core is taking all the time that I have available.

  • Is an opt-in option the correct direction for JENKINS-19022? If you want a different design, tell me and I will change it.

Yes, that is a great direction. It moves the workaround into a user accessible feature that they can choose to enable when the problem occurs, especially in cases like yours where they know the problem will occur.

  • Can you add the enhancement label? The labeler added only documentation and tests.

Label has been added

  • Do you want a screenshot of the new option in the README, like the other options?

Yes, a screenshot needs to be included in the documentation so that it is easier for readers to find the option and understand it in context.

You can use the incremental build of the pull request from the "Jenkins Incremental Publisher" page on the checks page. That allows you to immediately benefit from the change and allows others to use the change as well.

@PaulRoze

Copy link
Copy Markdown
Author

Thank you for the answers and the label.

  1. I added a screenshot of the option to the README in 149e868. It shows the option next to the branch specifier */${BRANCH}, because the option is for that type of job.
  2. I tested the incremental build 5.11.1-rc5601.3ca_4b_b_e1f2b_4. I installed it from the advanced settings of the Plugin Manager on Jenkins 2.580.2, as CONTRIBUTING.adoc says. I took the screenshot from this build.
  3. I built two branches on a Pipeline job with the option on, and on the same job with the option off:
    • Option on: buildsByBranchName has 1 entry.
    • Option off: buildsByBranchName has 113 entries.

@github-actions github-actions Bot added documentation Improvements or additions to documentation tests Automated test addition or improvement labels Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement Improvement or new feature tests Automated test addition or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants