Chapter 27Lesson 04~190 minutes

Job DSL, Seed Jobs, Folder Hierarchies, Programmatic Job Creation, and Jenkins API Automation: Diagnostics, Failure Modes, Security, and Performance

Diagnose Job DSL failures by preserving the seed build/source revision and separating sandbox/approval, authorization, naming/idempotency, removed-item ownership, plugin/API and generated-job execution layers. Repair the smallest layer; never broaden Groovy approval or delete unrelated jobs to make a seed green.

DiagnosticsScript SecurityDeletion safetyNamingAuthorizationPerformance

Learning objectives

  • Run an evidence-first diagnostic sequence for seed/generator failures.
  • Diagnose over-privileged seeds, unsafe Groovy approval and folder-boundary mistakes.
  • Detect non-idempotent naming and accidental removed-item deletion.
  • Separate Job DSL parse/generation failures from later generated-job runtime failures.
  • Repair the smallest layer while preserving first-failure evidence.

1. Evidence-first diagnostic sequence

  1. Preserve seed full name, queue/build number/URL, cause/actor and exact DSL commit.
  2. Preserve the first console/controller exception before retrying.
  3. Confirm Jenkins/Java plus Job DSL, Script Security, Folders and authorization-plugin versions.
  4. Confirm seed SCM checkout and DSL file hashes.
  5. Inspect sandbox/approval and seed run-as identity.
  6. Inspect lookup strategy, generated full names and folder permissions.
  7. Inspect removed-item actions and plugin-generated ownership tracking.
  8. Only after generation succeeds, diagnose queue/agent/Pipeline failures of generated jobs separately.
  9. Apply the least destructive correction and rerun one exact seed build.

2. Intentionally broken example: Pipeline DSL pasted into Job DSL

// WRONG: this is runtime Pipeline syntax, not a Job DSL item definition
pipeline {
  agent any
  stages { stage('Test') { steps { echo 'hello' } } }
}

Expected evidence: Job DSL compilation/method resolution fails because the generator context does not define the Declarative pipeline block. Preserve that seed build. Repair by creating a pipelineJob('name') and placing Pipeline syntax in its definition or SCM Jenkinsfile. Do not add random Groovy approvals.

3. Failure: seed has admin-equivalent power and untrusted editors

This is a security incident waiting to happen, even if every current DSL change looks harmless. An unsandboxed or over-privileged seed can mutate controller model objects and persisted jobs. Evidence to preserve: who can configure the seed, SCM write permissions, run-as identity, sandbox state and folder ACL.

Repair: protect the source branch, enable sandbox where compatible, run as a constrained identity, limit folder permissions, split platform/team seeds and test an explicit out-of-scope denial.

4. Failure: destructive delete of unmanaged or still-needed jobs

Never infer ownership from a prefix alone. A seed configured with removedJobAction: 'DELETE' can erase previously generated items that disappear from the observed desired set. With multiple Job DSL steps, applying DELETE before the last step can also delete then recreate items, potentially losing history.

Evidence: seed build that last generated the item, generated-object action, job history/config backup, DSL commits before/after and removed-item policy. Repair: restore from snapshot/config backup if needed, return to IGNORE or DISABLE, and make ownership/retention explicit before any later purge.

5. Failure: non-idempotent naming

// WRONG for desired-state generation
job("api-${System.currentTimeMillis()}") { /* ... */ }

Every seed run creates a new item, so “success” hides unbounded configuration growth. Use stable names derived from reviewed service identity, not timestamps/random values. If uniqueness is needed for build artifacts, put it in build/version state—not the persistent Jenkins item name.

6. Failure: approving unsafe Groovy broadly

A sandbox rejection such as an unapproved method signature is evidence that the generator is crossing the supported safe surface. Administrators should review the exact signature and intended state mutation. Methods that expose Jenkins internals, file/process access or broad persisted-object mutation deserve strong skepticism.

Prefer supported Job DSL methods from the instance API Viewer. If unsandboxed code is unavoidable, restrict it to protected platform source and treat changes like plugin/controller code.

7. Failure: folder names imply safety but permissions do not

A seed under teams/a can still be dangerous if its identity has root-wide Job/Create or Configure rights. Inspect the authorization strategy and inherited project permissions. In a disposable clone, attempt to create an item outside the folder and preserve the denial.

8. API failure layers

Observation Likely layer Next evidence
401/403 on API authentication/authorization/CSRF actor/token scope; crumb/session only if password auth
201/200 create/config response request accepted query exact item config; does not prove later build
queue URL returned scheduled state queue item → executable build transition
seed SUCCESS but missing expected job DSL target/lookup/removed action generated-object list + full names

9. Performance and controller load

Job DSL executes controller-side configuration work. Huge monolithic scripts, thousands of item updates, repeated API scans and unnecessary seed runs can load CPU, heap and disk. Measure seed duration, number of generated/updated items, controller CPU/heap and configuration I/O before tuning.

More frequent seed runs are not automatically safer. Trigger on reviewed source changes, batch predictable updates, and split ownership domains when one generator becomes too large.

10. Least-destructive repair matrix

Failure Unsafe shortcut Safer correction
Sandbox rejection disable Script Security use supported DSL or narrowly review signature
Wrong item scope give seed admin folder-relative lookup + scoped identity
Removed job delete everything/reseed disable/restore exact generated item
Non-idempotent names cleanup by wildcard fix stable naming; review exact extras
API 403 disable CSRF/authz correct token/permission/crumb semantics
Next lesson

Checkpoint Lab

Generate a bounded hierarchy, rerun it, change one schema, disable one removed job and assemble an evidence packet that proves source, seed identity, generated state and recovery policy.

Knowledge check

Answer before revealing the explanation.

1. A seed reports an unapproved signature. Which layer failed?

2. Why is a generated job unexpectedly deleted after splitting DSL across two jobDsl steps?

3. A DSL script uses pipeline { stages { ... } } directly. What is wrong?

4. Why is “rerun the seed until it works” poor diagnosis?

5. What is the safe response to a seed job that can modify items outside its team folder?

Official references and version notes

Verified baseline — 17 September 2026. Labs target Jenkins 2.568.3 LTS with Java 21 (Jenkins 2.568.3 is tested with Java 21 and 25). Job DSL is 3732.v9a_c49a_61a_313, requires Jenkins 2.479.3 and is currently marked “up for adoption” on the plugin site. Script Security is 1422.v06869826dd9b_; Folders is 6.1106.v3a_d9a_6d2465e; Matrix Authorization Strategy is 3.3; Authorize Project is 534.v2f208c45e11c. Re-check current plugin/API documentation and security advisories before reusing the lab. The mandatory path uses only a disposable local controller, synthetic Git repository and fake identities.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0Send only Ethereum/ERC-20 compatible assets to this address.