Groovy basics for pipeline authors
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.
Variables and types
def name = 'orders' // def: dynamically typed
String target = 'prod' // optional static type (never name a variable `env` — it hides the pipeline's env)
int replicas = 3
def tags = ['a', 'b'] // ArrayList
def cfg = [env: 'prod', replicas: 3] // LinkedHashMap (keeps insertion order)
final REGION = 'ap-south-1'
- Semicolons and parentheses on method calls are optional:
println 'hi',echo "Building ${name}". - Comments:
//and/* ... */. - In a Jenkinsfile, a variable declared without
defat the top level becomes a script-binding (global) variable visible everywhere in the script — handy but error-prone; inside methods always usedef. - Everything is an object;
nullis a thing — use the safe-navigation operator (below).
Strings — the most important part
| Syntax | Name | Interpolation | Multi-line |
|---|---|---|---|
'text' |
String | No | No |
"Hello ${name}" |
GString | Yes ($name or ${expr}) |
No |
'''...''' |
Triple single | No | Yes |
"""...""" |
Triple double | Yes | Yes |
/a\d+b/ |
Slashy | Yes; backslashes are literal | Yes — ideal for regex |
def svc = 'orders'
echo 'Deploying ${svc}' // prints: Deploying ${svc}
echo "Deploying ${svc}" // prints: Deploying orders
echo "Image: ${registry}/${svc}:${env.GIT_COMMIT.take(7)}"
Useful string methods: trim(), split(','), tokenize(',') (drops empty items), contains(), startsWith(), endsWith(), replace(), toUpperCase(), take(7), drop(1), isEmpty(), toInteger(), padLeft(), matches(/regex/), and the find operator =~ / match operator ==~.
assert 'v1.4.2' ==~ /v\d+\.\d+\.\d+/ // whole-string match → true
def m = ('release-2.3' =~ /release-(\d+)\.(\d+)/)
if (m) { echo "major=${m[0][1]}" } // careful in pipelines: Matcher isn't serializable (see CPS lesson)
Operators that show up everywhere
def replicas = cfg.replicas ?: 2 // Elvis: use right side if left is null/false/0/empty
def owner = cfg.meta?.owner // safe navigation: null instead of NullPointerException
def names = pods*.name // spread: collect 'name' from each element
def mode = target == 'prod' ? 'strict' : 'relaxed' // ternary
'a' == 'a' // == calls equals() → compares VALUES (unlike Java)
a.is(b) // identity comparison
'prod' in ['prod', 'dr'] // membership
(1..5).each { println it } // ranges
def (host, port) = 'db:5432'.split(':') // multiple assignment
Groovy truth
In conditions, these are false: null, false, 0, empty string '', empty list [], empty map [:], a Matcher with no match. Everything else is true. So if (params.TAG) means "TAG is set and non-empty".