0
0
Jenkinsdevops~10 mins

Groovy methods in pipelines in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple Groovy method in a Jenkins pipeline.

Jenkins
def greet() {
    echo [1] 
}
Drag options to blanks, or click blank then click option'
Aprintln "Hello, Jenkins!"
BHello, Jenkins!
C"Hello, Jenkins!"
Decho "Hello, Jenkins!"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string
Using println instead of echo in Jenkins pipeline
2fill in blank
medium

Complete the code to call the method greet inside the pipeline script.

Jenkins
pipeline {
    agent any
    stages {
        stage('Say Hello') {
            steps {
                [1]()
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
Aecho
Bprint
CsayHello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name
Forgetting parentheses when calling the method
3fill in blank
hard

Fix the error in the method definition to accept a parameter name and print a greeting.

Jenkins
def greet([1]) {
    echo "Hello, ${name}!"
}
Drag options to blanks, or click blank then click option'
AString name
Bname String
Cdef name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing parameter type and name
Omitting the type when required
4fill in blank
hard

Fill both blanks to define a method that returns the length of a string parameter.

Jenkins
def getLength([1]) {
    return [2].length()
}
Drag options to blanks, or click blank then click option'
AString text
Btext
CString name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that doesn't match inside the method
Forgetting to specify the parameter type
5fill in blank
hard

Fill all three blanks to create a method that filters a list of strings, returning only those longer than 3 characters.

Jenkins
def filterLong([1]) {
    return [2].findAll { it [3] 3 }
}
Drag options to blanks, or click blank then click option'
AList<String> words
Bwords
C.length() >
D.size() >
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type
Using size() instead of length() on strings
Mismatching parameter name inside the method