Jenkins Pipeline Best Practices: Do’s and Don’ts
This article outlines essential Jenkins Pipeline best practices, including using the official Pipeline plugin, treating pipelines as code, organizing work into stages and nodes, leveraging parallel execution, handling inputs with timeouts, and preferring stash over archive for artifact management.
Jenkins is a leading automation tool that can be extended with plugins, and the Jenkins Pipeline plugin enables users to write automation code using a Groovy‑based DSL for complex DevOps pipelines.
DO 1: Use the genuine Jenkins Pipeline plugin suite instead of older plugins such as Build Pipeline or Buildflow.
DO 2: Develop pipelines as code by storing a Jenkinsfile in source control; name the file Jenkinsfile and start it with the Groovy shebang: #!groovy DO 3: Place all non‑setup work inside stage blocks, which logically segment the pipeline and can be visualized with the Pipeline Stage View plugin.
stage("build") {
}
stage("test") {
}DO 4: Execute all substantive work inside node blocks so that heavy tasks run on distributed agents rather than the Jenkins master.
stage("build") {
node {
checkout scm
sh "mvn clean package"
}
}DO 5: Parallelize work using the simple parallel syntax to speed up pipelines and provide faster feedback.
parallel 'shifting': {
// everything
}, 'left': {
// I can
}DO 6: Acquire a node inside each parallel branch to perform substantial tasks.
parallel 'integration-tests': {
node('mvn-3.3') { ... }
}, 'functional-tests': {
node('selenium') { ... }
}DON'T 7: Do not place input steps inside node blocks, as they lock resources; instead, put input outside the node.
stage('deployment')
input 'Do you approve deployment?'
node {
// deploy the things
}DO 8: Wrap input steps with a timeout to automatically abort unattended approvals.
timeout(time: 5, unit: 'DAYS') {
input message: 'Approve deployment?', submitter: 'it-ops'
}DON'T 9: Avoid setting environment variables via the global env map; use withEnv instead.
withEnv(["PATH+MAVEN=${tool 'm3'}/bin"]) {
sh "mvn clean verify"
}DO 10: Prefer stash / unstash over archiveArtifacts for sharing files between stages, reserving archiving for long‑term storage.
stash excludes: 'target/', name: 'source'
unstash 'source'In summary, adopting these Jenkins Pipeline best practices—using the proper plugin, treating pipelines as code, structuring work with stages and nodes, parallelizing tasks, handling inputs safely, and managing artifacts efficiently—will lead to more robust, maintainable, and faster CI/CD workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
