Interview questions and model answers
"What's the difference between single and double quotes in a Jenkinsfile, and why does it matter for secrets?"
Single-quoted strings are literal; double-quoted GStrings interpolate ${} in Groovy. For sh steps with credentials I use single quotes so the shell expands the secret from the environment. With double quotes Groovy inserts the secret into the command string itself, which can leak it into process listings and logs, and Jenkins warns about it. When I need Groovy values too, I escape shell variables as \$VAR or pass values with withEnv.
"What is CPS in Jenkins and what problems does it cause?"
Pipelines are transformed into continuation-passing style so their state can be checkpointed to disk, which lets them pause on input and resume after a controller restart. The cost: all pipeline Groovy runs on the controller and is slow, any object held across a step must be serializable — otherwise NotSerializableException — and some closure-based methods behave incorrectly. I keep logic thin, push heavy work into shell tools, and put pure computation in @NonCPS methods that don't call steps.
"When would you use @NonCPS?"
For pure, synchronous computation that uses non-serializable objects or CPS-unfriendly closures — parsing text with a regex Matcher, sorting with a comparator, transforming data structures. The method must not call pipeline steps, and should return simple serializable values.
"Explain the structure of a shared library."
vars/ holds global steps — each file's call method is invoked by name, with arguments usually as a Map. src/ holds classes in packages, which need the script object passed in to call steps. resources/ holds files loaded with libraryResource, like pod templates and scripts. It's loaded with @Library('name@version') _ or the library step, configured globally or per folder, implicitly or explicitly.
"How do you test and roll out changes to a shared library used by 80 pipelines?"
Unit tests with JenkinsPipelineUnit mocking steps, linting, and integration tests against sample repos on a test controller — all in CI on the library repo with required reviews. Versioned releases with semver tags; teams pin versions and upgrade deliberately, or follow a major-version tag. Breaking changes ship as a new major with a changelog, deprecation warnings and a migration window, piloted on a few repos first.
"What's the security difference between trusted and untrusted libraries?"
Global libraries configured by admins are trusted and run outside the Groovy sandbox, so they can do anything on the controller including reading credentials — write access to their repo is effectively admin access. Folder-level libraries run in the sandbox like Jenkinsfiles. So global libraries need strict code review and branch protection, and I'd be conservative with script approvals.
"You see NotSerializableException: java.util.regex.Matcher. What happened?"
A Matcher was stored in a local variable that was still alive when the pipeline executed a step, so CPS tried to serialize it at the checkpoint. I'd move the regex work into a @NonCPS method that returns just the matched string, or set the variable to null before the next step.