Complete the code to define a simple Groovy method in a Jenkins pipeline.
def greet() { echo [1] }
The echo step requires a string argument. The string must be quoted.
Complete the code to call the method greet inside the pipeline script.
pipeline {
agent any
stages {
stage('Say Hello') {
steps {
[1]()
}
}
}
}The method defined is named greet, so calling greet() runs it.
Fix the error in the method definition to accept a parameter name and print a greeting.
def greet([1]) { echo "Hello, ${name}!" }
Groovy method parameters need the type before the name, like String name.
Fill both blanks to define a method that returns the length of a string parameter.
def getLength([1]) { return [2].length() }
The method parameter is String text, and we call text.length() to get its length.
Fill all three blanks to create a method that filters a list of strings, returning only those longer than 3 characters.
def filterLong([1]) { return [2].findAll { it [3] 3 } }
size() instead of length() on stringsThe method takes a list of strings named words. We use findAll to keep items where it.length() > 3.