Interview Prep/Jenkins/Shared libraries & multibranch

A library uses a class that stores the build's config in a static field. Builds started at the same time see each other's values. Why and how do you fix it?

AdvancedScenarioShared libraries & multibranch
Answer

Situation read → Library classes are loaded once per library version per controller classloader context and static fields are shared across concurrent builds.

What I'd do →

  1. Remove static mutable state; pass config as method arguments or instance fields.
  2. Create instances per build (new Config(this, params)) and keep them Serializable.
  3. Add a JenkinsPipelineUnit test that simulates two configs.

Why → Static fields behave like global variables across builds, and CPS resume makes them even less predictable.

Interview tip → Say "library code must be stateless across builds".

All Jenkins questions Roadmap: Jenkins

More on Shared libraries & multibranch