Complete the code to start a Groovy script block in Jenkins pipeline.
pipeline {
agent any
stages {
stage('Example') {
steps {
[1] {
echo 'Hello from Groovy script!'
}
}
}
}
}The script block allows running Groovy code inside a Jenkins pipeline.
Complete the code to assign a variable inside a Groovy script block.
pipeline {
agent any
stages {
stage('Set Variable') {
steps {
script {
def message = [1]
echo message
}
}
}
}
}Strings in Groovy must be quoted. Double quotes allow interpolation if needed.
Fix the error in the Groovy script block to correctly loop over a list.
pipeline {
agent any
stages {
stage('Loop') {
steps {
script {
def items = ['a', 'b', 'c']
for (item [1] items) {
echo item
}
}
}
}
}
}Groovy uses in to loop over collections in for loops.
Fill both blanks to create a Groovy map with keys and values inside a script block.
pipeline {
agent any
stages {
stage('Map') {
steps {
script {
def data = [[1]: 'apple', [2]: 'banana']
echo data['fruit1']
}
}
}
}
}Map keys as strings must be quoted. Here keys are 'fruit1' and 'fruit2'.
Fill all three blanks to filter a list inside a Groovy script block using a closure.
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()
}
}
}
}
}The closure uses n -> n % 2 == 0 to find even numbers.
