CI/CD & build tooling
Groovy for pipelines
Groovy is the language Jenkins pipelines and shared libraries are written in. It runs on the JVM, accepts most Java syntax, and adds dynamic typing, closures and concise collection handling. You don't need to be a Groovy developer — but a senior engineer owning Jenkins is expected to read and write shared-library code confidently, understand why pipeline Groovy behaves differently from normal Groovy (CPS and the sandbox), and avoid the classic traps around string interpolation and secrets.
After this topic you can
- Read and write the Groovy used in pipelines
- Use closures, maps and string interpolation correctly
- Avoid sandbox and serialisation errors and build shared libraries
Lessons
-
1Groovy basics for pipeline authors
- Variables and types
- Strings — the most important part
- Operators that show up everywhere
- Groovy truth
-
2Collections and closures
- Lists and maps
- Closures
- The collection methods you'll use
- Delegates and DSLs (why Jenkins syntax looks the way it does)
-
3Control flow, methods, classes and errors
- Conditions and loops
- Methods
- Classes
- Exceptions
-
4Groovy inside Jenkinsfiles
- Where Groovy goes in declarative pipelines
- Getting data out of shell steps
- Built-in globals
- Files, YAML and JSON
- Dynamic parallel stages
- Interpolation traps: secrets and $
- load
-
5How pipeline Groovy really runs: CPS, serialization and the sandbox
- Continuation-passing style
- @NonCPS
- The Groovy sandbox and script approval
- Trusted vs untrusted libraries
- Performance guidance
-
6Shared libraries: structure and loading
- Layout
- vars/: custom steps
- src/: classes
- resources/
- Loading a library
-
7Designing, testing and governing a shared library
- The "standard pipeline" pattern
- What belongs in a library
- Testing
- Versioning and rollout
-
8Interview questions and model answers
- "What's the difference between single and double quotes in a Jenkinsfile, and why does it matter for secrets?"
- "What is CPS in Jenkins and what problems does it cause?"
- "When would you use @NonCPS?"
- "Explain the structure of a shared library."
- "How do you test and roll out changes to a shared library used by 80 pipelines?"
- "What's the security difference between trusted and untrusted libraries?"
- "You see NotSerializableException: java.util.regex.Matcher. What happened?"