Jenkins Scripted Pipeline Design Prompt
Write a scripted (Groovy) Jenkins pipeline for the cases declarative can't express cleanly — dynamic stage generation, complex loops, and conditional flow — without turning it into unmaintainable Groovy spaghetti.
- Target user
- Engineers who hit the limits of declarative pipelines
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Jenkins engineer who reaches for scripted pipelines only when declarative genuinely can't do the job — and keeps the Groovy disciplined.
I will provide:
- What I need that declarative struggles with (dynamically generated stages, data-driven matrices, complex branching)
- My build/test/deploy steps and agent options
- Any constraints (script security / sandbox, no admin approval available)
Your job:
1. **Justify scripted vs declarative** — first confirm the need is real; if a `matrix` or `script {}` island inside declarative would suffice, say so and stop.
2. **node/stage structure** — write the `node('label') { stage('…') { … } }` structure, wrapping the body in `try/finally` so cleanup and notifications always run.
3. **Dynamic stages** — show the safe pattern for generating stages/parallel branches from a list or map, with correct closure variable capture (the classic loop-variable binding bug) called out.
4. **Sandbox awareness** — flag methods that trip the Groovy sandbox / script-security approval, and prefer sandbox-safe constructs so the pipeline runs without an admin approving signatures.
5. **Credentials & workspace** — use `withCredentials`, `ws`, and `dir` correctly; ensure `deleteDir()`/`cleanWs()` runs in `finally`.
6. **Error handling** — use `catchError`/`unstable`, set build result explicitly, and make failures visible.
Output: (a) the scripted pipeline with comments, (b) the loop-variable capture fix highlighted, (c) a list of any sandbox approvals it needs, (d) a note on which parts could move back to declarative.
Bias toward: minimal scripted surface area, correct closure capture, and try/finally cleanup.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Jenkins Declarative Pipeline Design Prompt
Design a clean, maintainable declarative Jenkinsfile from scratch — stages, agents, environment, post conditions, and options — instead of a sprawling freestyle or scripted mess.
-
Jenkins Parallel & Matrix Stages Prompt
Speed up a slow Jenkins pipeline with parallel stages and the declarative matrix — fan out tests across OS/version combos, run independent stages concurrently, and fail fast without starving executors.
-
Jenkins Security Hardening & RBAC Prompt
Harden a Jenkins controller — script security/sandbox, matrix or role-based authorization, agent isolation, CSRF and CLI settings, and least-privilege — so a shared instance isn't a soft target.
-
Jenkins Shared Library Design Prompt
Design a Jenkins shared library that factors repeated pipeline logic into reusable steps (vars/) and classes (src/) so dozens of Jenkinsfiles stay thin, consistent, and testable.
More Jenkins prompts & error guides
Browse every Jenkins prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.