0
0
Jenkinsdevops~10 mins

Script blocks for Groovy 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 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'
Ash
Bgroovy
Cbat
Dscript
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.
2fill in blank
medium

Complete 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'
A"Hello, Jenkins!"
BHello, Jenkins!
C'Hello Jenkins'
DHello Jenkins
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes syntax errors.
Using single quotes is valid but here double quotes are preferred.
3fill in blank
hard

Fix 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'
Ain
Bof
C=
Dto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'of' or '=' causes syntax errors.
Using 'to' is incorrect for looping over collections.
4fill in blank
hard

Fill 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'
A'fruit1'
B'fruit2'
Cfruit1
Dfruit2
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causes errors or treats keys as variables.
Mixing quoted and unquoted keys inconsistently.
5fill in blank
hard

Fill 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'
An
C%
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and variable causes errors.
Using '==' instead of '%' for modulo causes wrong filtering.