Interview › Scripting (Bash, Groovy)
What is the difference between a declarative and a scripted Jenkins pipeline in Groovy? [Advanced]
Answer
Declarative Pipeline is a structured, opinionated Jenkins syntax inside a pipeline { } block. Scripted Pipeline is more free-form Groovy using node { } and direct control flow. Declarative is easier to standardize; scripted is more flexible for complex logic.
Technical explanation
Declarative has sections such as agent, environment, stages, steps, post, options, and when.
Scripted Pipeline allows arbitrary Groovy control flow but can become harder to read and govern.
In enterprise CI, I normally use Declarative for consistency and shared libraries for reusable complex behavior.
Hands-on example
// Declarative
pipeline { agent any; stages { stage('Test') { steps { sh 'make test' } } } }
// Scripted
node {
stage('Test') {
sh 'make test'
}
}Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- How do you define and call a function (or step) in a Jenkins Groovy pipeline? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- How do you handle errors and retries in a Groovy pipeline (try/catch, retry, timeout)? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you run stages in parallel in a Jenkins Groovy pipeline? [Advanced]
- What is the difference between a cron job and a systemd timer? [Advanced]
- What is the difference between Groovy and Java for scripting purposes? [Advanced]
- How would you write a health-check script that polls a URL until it returns 200? [Advanced]