Interview Prep/Jenkins/Shared libraries & multibranch

A team asks to write a `vars/` step that calls Jenkins internal APIs (`Jenkins.instance.getItem`) to list jobs. Should it live in the trusted library?

AdvancedScenarioShared libraries & multibranch
Answer

Situation read → Trusted library code runs unsandboxed; any step there becomes a capability every pipeline gets.

What I'd do →

  1. Ask what the real need is; often the REST API or a plugin step answers it.
  2. If needed, implement a narrow step (listJobsInFolder(folder)) that returns names only, validates input, and checks the caller's folder.
  3. Review it like privileged code; never expose generic "run this Groovy" helpers.
  4. Prefer doing such admin tasks in a separate admin-only job.

Why → A trusted library is an admin API surface for all Jenkinsfile authors.

Interview tip → Tie it to the principle of least privilege.

All Jenkins questions Roadmap: Jenkins

More on Shared libraries & multibranch