Bird
Raised Fist0
Jenkinsdevops~20 mins

Why scripted pipelines offer flexibility in Jenkins - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Scripted Pipeline Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do scripted pipelines allow more complex logic than declarative pipelines?

Choose the best reason why scripted pipelines in Jenkins offer more flexibility compared to declarative pipelines.

AScripted pipelines automatically generate UI forms for input parameters.
BScripted pipelines have a simpler syntax that restricts complex logic.
CDeclarative pipelines allow more customization than scripted pipelines.
DScripted pipelines use Groovy code, allowing loops, conditionals, and custom functions.
Attempts:
2 left
💡 Hint

Think about what programming features Groovy code provides.

💻 Command Output
intermediate
2:00remaining
Output of a scripted pipeline with conditional stage execution

What will be the output of this scripted pipeline snippet when the variable runTests is false?

Jenkins
def runTests = false
node {
  stage('Build') {
    echo 'Building...'
  }
  if (runTests) {
    stage('Test') {
      echo 'Testing...'
    }
  }
  stage('Deploy') {
    echo 'Deploying...'
  }
}
ABuild stage skipped; only Deploying...
BBuilding...\nTesting...\nDeploying...
CBuilding...\nDeploying...
DTesting...\nDeploying...
Attempts:
2 left
💡 Hint

Check the condition controlling the 'Test' stage.

🔀 Workflow
advanced
2:00remaining
How to implement parallel stages in a scripted pipeline

Which scripted pipeline code correctly runs two stages in parallel?

Aparallel firstBranch: { echo 'Running first branch' }, secondBranch: { echo 'Running second branch' }
Bstage('Parallel') { runInParallel { echo 'First'; echo 'Second' } }
Cparallel { stage('First') { echo 'First' } stage('Second') { echo 'Second' } }
Dnode { parallel { echo 'First'; echo 'Second' } }
Attempts:
2 left
💡 Hint

Remember the syntax for the parallel step in scripted pipelines.

❓ Troubleshoot
advanced
2:00remaining
Why does this scripted pipeline fail with a missing method error?

Given this scripted pipeline snippet, why does Jenkins report groovy.lang.MissingMethodException?

Jenkins
node {
  stage('Example') {
    sh 'echo Hello'
  }
  stage('Example') {
    sh 'echo World'
  }
}
ADuplicate stage names cause Jenkins to fail with a missing method error.
BThe <code>sh</code> step is not allowed inside scripted pipelines.
CStages must be inside a <code>pipeline</code> block in scripted pipelines.
DThe <code>node</code> block is missing a required parameter.
Attempts:
2 left
💡 Hint

Check the stage names and how Jenkins handles them.

✅ Best Practice
expert
2:00remaining
Best practice for sharing variables between stages in scripted pipelines

In a scripted pipeline, what is the best way to share a variable's value between multiple stages?

ADeclare the variable outside the <code>node</code> block as a global variable.
BUse <code>env</code> environment variables to store and share values.
CDeclare the variable inside each stage separately to avoid conflicts.
DUse <code>def</code> inside each stage to create local copies.
Attempts:
2 left
💡 Hint

Think about persistence and accessibility of variables across stages.

Practice

(1/5)
1. Why do scripted pipelines in Jenkins offer more flexibility compared to declarative pipelines?
easy
A. Because they only support predefined stages without customization.
B. Because they have a simpler syntax with fewer features.
C. Because they allow using loops, conditions, and custom error handling.
D. Because they automatically generate pipeline code without user input.

Solution

  1. Step 1: Understand scripted pipeline features

    Scripted pipelines allow writing code with loops, conditions, and error handling, giving more control.
  2. Step 2: Compare with declarative pipelines

    Declarative pipelines have a simpler, fixed structure and less flexibility for complex logic.
  3. Final Answer:

    Because they allow using loops, conditions, and custom error handling. -> Option C
  4. Quick Check:

    Scripted pipelines = flexible code control [OK]
Hint: Look for options mentioning loops and conditions for flexibility [OK]
Common Mistakes:
  • Confusing simplicity with flexibility
  • Thinking declarative pipelines allow all custom code
  • Assuming scripted pipelines auto-generate code
2. Which of the following is the correct syntax to start a scripted pipeline block in Jenkins?
easy
A. node {
B. stage {
C. pipeline {
D. scripted {

Solution

  1. Step 1: Identify scripted pipeline block syntax

    Scripted pipelines start with node { to define the workspace and steps.
  2. Step 2: Eliminate other options

    pipeline { is for declarative pipelines; stage { defines a stage; scripted { is invalid syntax.
  3. Final Answer:

    node { -> Option A
  4. Quick Check:

    Scripted pipeline starts with node block [OK]
Hint: Scripted pipelines start with 'node {' block [OK]
Common Mistakes:
  • Using 'pipeline {' which is declarative syntax
  • Confusing 'stage {' as pipeline start
  • Assuming 'scripted {' is valid syntax
3. What will be the output of this scripted pipeline snippet?
node {
  for (int i = 1; i <= 3; i++) {
    echo "Count: ${i}"
  }
}
medium
A. Count: 1\nCount: 2\nCount: 3
B. Count: 0\nCount: 1\nCount: 2
C. Syntax error due to for loop
D. No output because echo is invalid

Solution

  1. Step 1: Analyze the for loop range

    The loop runs from i=1 to i=3 inclusive, so it iterates 3 times.
  2. Step 2: Check the echo output each iteration

    Each iteration prints "Count: " followed by the current i value, producing lines for 1, 2, and 3.
  3. Final Answer:

    Count: 1\nCount: 2\nCount: 3 -> Option A
  4. Quick Check:

    Loop 1 to 3 prints counts 1,2,3 [OK]
Hint: Check loop start and end values carefully [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Thinking echo is invalid in scripted pipeline
  • Assuming syntax error in for loop
4. Identify the error in this scripted pipeline snippet:
node {
  if (env.BRANCH_NAME == 'main')
    echo 'Deploying to production'
  else
    echo 'Deploying to staging'
}
medium
A. node block cannot contain if statements
B. Missing curly braces {} around if and else blocks
C. echo command cannot be used inside if
D. Incorrect use of env variable syntax

Solution

  1. Step 1: Check if-else syntax in scripted pipeline

    In scripted pipelines, if and else blocks must use curly braces {} when spanning multiple lines or statements.
  2. Step 2: Identify missing braces

    The snippet lacks braces around the echo statements, which can cause syntax errors or unexpected behavior.
  3. Final Answer:

    Missing curly braces {} around if and else blocks -> Option B
  4. Quick Check:

    If-else blocks need braces in scripted pipelines [OK]
Hint: Always use braces {} for if-else blocks in scripted pipelines [OK]
Common Mistakes:
  • Assuming env.BRANCH_NAME syntax is wrong
  • Thinking echo can't be inside if
  • Believing node block forbids if statements
5. You want to retry a build step up to 3 times if it fails, using a scripted pipeline. Which snippet correctly implements this retry logic?
hard
A. node { for (int i=0; i<3; i++) { sh 'run-build.sh' } }
B. node { while (true) { sh 'run-build.sh' } }
C. node { try { sh 'run-build.sh' } catch (err) { retry(3) } }
D. node { retry(3) { sh 'run-build.sh' } }

Solution

  1. Step 1: Understand Jenkins retry step

    The retry(n) { ... } block retries the enclosed steps up to n times on failure automatically.
  2. Step 2: Evaluate each option

    node { retry(3) { sh 'run-build.sh' } } correctly uses retry(3) around the build command. node { for (int i=0; i<3; i++) { sh 'run-build.sh' } } runs the command 3 times without retry on failure. node { try { sh 'run-build.sh' } catch (err) { retry(3) } } misuses retry inside catch. node { while (true) { sh 'run-build.sh' } } loops infinitely.
  3. Final Answer:

    node { retry(3) { sh 'run-build.sh' } } -> Option D
  4. Quick Check:

    Use retry(n) block for automatic retries [OK]
Hint: Use retry(n) block to retry steps automatically [OK]
Common Mistakes:
  • Using loops instead of retry block
  • Placing retry inside catch block incorrectly
  • Creating infinite loops without exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard