0
0
Jenkinsdevops~20 mins

Script blocks for Groovy in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Groovy Script Blocks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Groovy script block in Jenkins pipeline
What is the output of this Groovy script block in a Jenkins pipeline?
Jenkins
def result = ''
script {
  result = 'Hello from Groovy script block'
}
println(result)
Anull
BCompilation error
CSyntaxError
DHello from Groovy script block
Attempts:
2 left
💡 Hint
Remember that variables assigned inside script blocks are accessible outside in Jenkins scripted pipelines.
Configuration
intermediate
2:00remaining
Correct syntax for a Groovy script block in Jenkins pipeline
Which of the following is the correct way to write a Groovy script block inside a Jenkins declarative pipeline?
A
steps {
  script echo 'Running Groovy code'
}
B
script {
  echo 'Running Groovy code'
}
C
groovy {
  echo 'Running Groovy code'
}
D
sh {
  echo 'Running Groovy code'
}
Attempts:
2 left
💡 Hint
The keyword for embedding Groovy code in declarative pipelines is 'script'.
Troubleshoot
advanced
2:00remaining
Troubleshooting variable scope in Groovy script block
Given this Jenkins pipeline snippet, what is the cause of the error 'groovy.lang.MissingPropertyException: No such property: count'?
Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          def count = 5
        }
        echo "Count is ${count}"
      }
    }
  }
}
AVariable 'count' is defined inside script block and not accessible outside it
BSyntax error in the script block
CThe echo step does not support variable interpolation
DMissing 'def' keyword before 'count' in echo step
Attempts:
2 left
💡 Hint
Think about variable scope inside and outside script blocks in Jenkins pipelines.
🔀 Workflow
advanced
2:00remaining
Order of execution in Jenkins pipeline with multiple script blocks
Consider this Jenkins pipeline snippet. What will be the output order of the echo statements?
Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          echo 'First script block'
        }
        echo 'Outside script block'
        script {
          echo 'Second script block'
        }
      }
    }
  }
}
AFirst script block\nSecond script block\nOutside script block
BOutside script block\nFirst script block\nSecond script block
CFirst script block\nOutside script block\nSecond script block
DSecond script block\nFirst script block\nOutside script block
Attempts:
2 left
💡 Hint
Steps in Jenkins pipeline run in the order they are written.
Best Practice
expert
2:00remaining
Best practice for complex Groovy logic in Jenkins pipelines
Which approach is best for managing complex Groovy logic in Jenkins pipelines to keep the pipeline readable and maintainable?
AUse shared libraries to encapsulate complex Groovy logic and call them from the Jenkinsfile
BPlace all Groovy code inside multiple script blocks directly in the Jenkinsfile
CWrite Groovy code inline in the pipeline steps without script blocks
DAvoid Groovy and write all logic in shell scripts called from the pipeline
Attempts:
2 left
💡 Hint
Think about code reuse and separation of concerns in Jenkins pipelines.