To write A Pipeline is to write Groovy code. Jenkins Pipeline is actually a DSL based on the Groovy language. It’s important to know some Groovy syntax

If you don’t want to install the Groovy environment locally, you can open groovy-playground to run through the Groovy code and see the results directly, which may require scientific surfing.

Necessary knowledge of Groovy syntax

  • Define variables and methods using the def keyword,def name="jack"
  • The semicolon at the end of the statement is not required
  • Method calls can omit parentheses
def say(String name = "world") {
  return "hi "+ name} // Call say name ="jack"
Copy the code
  • Double quotes support interpolation, single quotes do not parse variables, and output as is
def name = 'world'// Result: Hello worldprint "hello ${name}"// Result: hello${name}
print 'hello ${name}'
Copy the code
  • Both triple double and triple single quotes support newlines, and only triple double quotes support interpolation
def foo = """ line one
line two
${name}
"""
Copy the code
  • Support closures
Def codeBlack = {def codeBlack = {print "hello closure"} def pipeline(closure) {closure()} codeBlack (codeBlack) {closure()} codeBlack (codeBlack)Copy the code

// If the brackets are not required, the result is the same as Jenkins pipeline

pipeline( {print "hello closure"} )
pipeline { 
  print "hello closure"
} 
pipeline codeBlack
Copy the code
  • Another use for closures
Def closure(String name, closure) {println name closure()}"stage name", {
   println "closure"}) closure */ * Stage name closure */ // In Groovy, you can write stage("stage name") {
  print "closure"
}
Copy the code