Opscraft
CI/CD & build tooling

Groovy for pipelines

Must-know8 lessons · 32 min · 3 tests ·

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.

Stage 2 · Core tools

Know these first

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

  1. 1Groovy basics for pipeline authors 4 min read
    • Variables and types
    • Strings — the most important part
    • Operators that show up everywhere
    • Groovy truth
  2. 2Collections and closures 4 min read
    • Lists and maps
    • Closures
    • The collection methods you'll use
    • Delegates and DSLs (why Jenkins syntax looks the way it does)
  3. 3Control flow, methods, classes and errors 3 min read
    • Conditions and loops
    • Methods
    • Classes
    • Exceptions
  4. 4Groovy inside Jenkinsfiles 6 min read
    • 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
  5. 5How pipeline Groovy really runs: CPS, serialization and the sandbox 5 min read
    • Continuation-passing style
    • @NonCPS
    • The Groovy sandbox and script approval
    • Trusted vs untrusted libraries
    • Performance guidance
  6. 6Shared libraries: structure and loading 3 min read
    • Layout
    • vars/: custom steps
    • src/: classes
    • resources/
    • Loading a library
  7. 7Designing, testing and governing a shared library 4 min read
    • The "standard pipeline" pattern
    • What belongs in a library
    • Testing
    • Versioning and rollout
  8. 8Interview questions and model answers 3 min read
    • "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?"