0
0
Jenkinsdevops~20 mins

When to use scripted over declarative in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jenkins Pipeline Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When is scripted pipeline preferred over declarative in Jenkins?

Which situation best justifies using a scripted Jenkins pipeline instead of a declarative pipeline?

AWhen you need complex conditional logic and loops that declarative syntax cannot easily express
BWhen you want a simple, easy-to-read pipeline with built-in error handling
CWhen you want to use a visual editor to create your pipeline quickly
DWhen you want to enforce strict syntax rules and predefined stages
Attempts:
2 left
💡 Hint

Think about which pipeline style offers more programming flexibility.

🧠 Conceptual
intermediate
2:00remaining
Why might a team choose declarative pipelines over scripted ones?

What is a main advantage of declarative pipelines compared to scripted pipelines?

AThey allow writing any Groovy code without restrictions
BThey provide a simpler syntax with built-in error handling and validation
CThey require no knowledge of Jenkins or pipelines
DThey support dynamic stage creation using loops
Attempts:
2 left
💡 Hint

Consider which pipeline style helps avoid syntax errors and is easier for beginners.

🔀 Workflow
advanced
3:00remaining
Identify the correct use case for scripted pipeline in a Jenkinsfile

Given the need to run a dynamic number of parallel stages based on external input, which Jenkinsfile snippet correctly uses scripted pipeline syntax?

Jenkins
def stages = ['build', 'test', 'deploy']

parallel stages.collectEntries { stageName ->
  [(stageName): {
    echo "Running ${stageName} stage"
  }]
}
AThe code correctly creates parallel stages dynamically using scripted pipeline syntax
BThe code will cause a syntax error because 'collectEntries' is not allowed in Jenkinsfiles
CThe code is invalid because parallel stages cannot be created dynamically
DThe code will run sequentially, not in parallel, due to missing 'stage' blocks
Attempts:
2 left
💡 Hint

Think about how scripted pipelines allow Groovy code to generate stages dynamically.

Troubleshoot
advanced
3:00remaining
Preferred conditional logic in declarative Jenkins pipelines

Examine this declarative Jenkins pipeline snippet and identify the preferred approach for the conditional logic:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          if (env.BRANCH_NAME == 'main') {
            echo 'Building main branch'
          }
        }
      }
    }
  }
}
AThe 'script' block is not allowed inside 'steps' in declarative pipelines
BThe 'if' statement syntax is invalid in Groovy
CThe environment variable 'env.BRANCH_NAME' is not accessible in declarative pipelines
DThe 'script' block is allowed, but use 'when' condition at the stage level instead
Attempts:
2 left
💡 Hint

Declarative pipelines allow 'script' blocks, but there are better ways to handle conditions.

Best Practice
expert
4:00remaining
Choosing pipeline style for a complex multi-branch project

You manage a Jenkins setup with many branches and complex build logic including loops, dynamic stages, and error recovery. Which pipeline style is best and why?

AUse declarative pipeline with embedded scripted blocks for complex parts to balance readability and flexibility
BUse declarative pipeline exclusively for all branches to maintain uniformity and simplicity
CUse scripted pipeline for full Groovy flexibility to handle complex logic and dynamic behavior
DUse freestyle jobs instead of pipelines for better control over complex logic
Attempts:
2 left
💡 Hint

Consider which style allows the most control and flexibility for complex scenarios.