Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a Groovy script block in Jenkins pipeline.
Jenkins
pipeline {
agent any
stages {
stage('Example') {
steps {
[1] {
echo 'Hello from Groovy script!'
}
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sh' or 'bat' blocks which run shell or batch commands, not Groovy code.
Trying to use 'groovy' as a block name which is not valid.
✗ Incorrect
The script block allows running Groovy code inside a Jenkins pipeline.
2fill in blank
mediumComplete the code to assign a variable inside a Groovy script block.
Jenkins
pipeline {
agent any
stages {
stage('Set Variable') {
steps {
script {
def message = [1]
echo message
}
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes syntax errors.
Using single quotes is valid but here double quotes are preferred.
✗ Incorrect
Strings in Groovy must be quoted. Double quotes allow interpolation if needed.
3fill in blank
hardFix the error in the Groovy script block to correctly loop over a list.
Jenkins
pipeline {
agent any
stages {
stage('Loop') {
steps {
script {
def items = ['a', 'b', 'c']
for (item [1] items) {
echo item
}
}
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'of' or '=' causes syntax errors.
Using 'to' is incorrect for looping over collections.
✗ Incorrect
Groovy uses in to loop over collections in for loops.
4fill in blank
hardFill both blanks to create a Groovy map with keys and values inside a script block.
Jenkins
pipeline {
agent any
stages {
stage('Map') {
steps {
script {
def data = [[1]: 'apple', [2]: 'banana']
echo data['fruit1']
}
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causes errors or treats keys as variables.
Mixing quoted and unquoted keys inconsistently.
✗ Incorrect
Map keys as strings must be quoted. Here keys are 'fruit1' and 'fruit2'.
5fill in blank
hardFill all three blanks to filter a list inside a Groovy script block using a closure.
Jenkins
pipeline {
agent any
stages {
stage('Filter') {
steps {
script {
def numbers = [1, 2, 3, 4, 5]
def evens = numbers.findAll { [1] -> [2] [3] 2 == 0 }
echo evens.toString()
}
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and variable causes errors.
Using '==' instead of '%' for modulo causes wrong filtering.
✗ Incorrect
The closure uses n -> n % 2 == 0 to find even numbers.