0
0
Jenkinsdevops~5 mins

Script blocks for Groovy in Jenkins - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a script block in Groovy within Jenkins pipelines?
A script block is a section inside a Jenkins pipeline where you can write Groovy code directly. It allows you to run complex logic that is not supported by the declarative pipeline syntax.
Click to reveal answer
beginner
How do you define a script block in a Jenkins declarative pipeline?
You define a script block using the script { } keyword inside a stage or step. For example:<br>
stage('Example') {
  steps {
    script {
      // Groovy code here
    }
  }
}
Click to reveal answer
beginner
Why use script blocks in Jenkins pipelines?
Script blocks let you use Groovy features like loops, conditionals, and variables that are not available in declarative syntax. This helps you write flexible and powerful pipeline logic.
Click to reveal answer
intermediate
Can you use variables declared inside a script block outside of it in Jenkins pipelines?
No, variables declared inside a script block are local to that block. To share data outside, you need to declare variables at the pipeline or stage level.
Click to reveal answer
intermediate
What happens if you put complex Groovy code outside a script block in a declarative pipeline?
The pipeline will fail to run because declarative syntax only supports limited Groovy expressions. Complex code must be inside a script block to execute properly.
Click to reveal answer
What keyword starts a script block in a Jenkins declarative pipeline?
Agroovy
Bscript
Cblock
Dcode
Where can you place a script block in a Jenkins declarative pipeline?
AInside a stage's steps section
BDirectly at the pipeline root
COnly inside environment variables
DOutside the pipeline block
What is a common reason to use script blocks in Jenkins pipelines?
ATo write shell commands
BTo define environment variables
CTo run complex Groovy logic not supported by declarative syntax
DTo declare pipeline stages
What happens if you try to use a variable declared inside a script block outside it?
AIt will cause a syntax error
BIt will be accessible globally
CIt will automatically convert to a string
DIt will not be accessible outside the script block
Which of these is NOT true about script blocks?
AThey can be used anywhere in the Jenkinsfile
BThey allow use of Groovy loops and conditionals
CThey help extend declarative pipeline capabilities
DThey require the <code>script</code> keyword
Explain what a script block is in Jenkins pipelines and why it is useful.
Think about how you add custom code inside a mostly fixed pipeline structure.
You got /4 concepts.
    Describe the scope of variables declared inside a script block and how to share data outside it.
    Consider where variables live and how to pass data between parts of the pipeline.
    You got /4 concepts.