Skip to main content
In this lesson, we’ll cover essential recommendations to optimize your Jenkins pipelines—both scripted and declarative—and then dive into guidelines that apply specifically to scripted pipelines. Following these practices will improve performance, maintainability, and reliability across your CI/CD workflows.
These general best practices apply to both Scripted and Declarative Pipelines in Jenkins.

General Best Practices

Scripted Pipeline–Specific Guidelines

  1. Stick to Basic Groovy Syntax
    Use only core Groovy features (loops, conditionals, closures). Avoid DSL meta-programming or AST transformations that make debugging difficult.
  2. Avoid Direct Jenkins API Calls
Do not invoke Jenkins internal APIs (e.g., hudson.model.*) from your Jenkinsfile.
For advanced integration, develop a custom Pipeline Step Plugin instead.
  1. Prefer CLI Parsers Over Groovy Libraries
    For XML/JSON manipulation, use tools like xmllint or jq rather than in-pipeline Groovy parsing to reduce controller memory usage.
  2. Don’t Perform Raw Network or I/O Operations
Never fetch URLs or read/write files directly in your Jenkinsfile.
Wrap these operations in sh, bat, or custom steps to ensure proper error handling and resource cleanup.

The image is a slide titled "Pipelines – Best Practices," listing best practices such as efficient log management and leveraging Jenkins plugins, alongside practices to avoid like complex Groovy features and network calls.

References

Watch Video

Practice Lab