{"id":62205,"date":"2024-06-06T17:15:58","date_gmt":"2024-06-06T11:45:58","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=62205"},"modified":"2024-06-06T17:18:29","modified_gmt":"2024-06-06T11:48:29","slug":"pro-jenkinsfile-avoid-these-common-ci-cd-mistakes-part-1","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/pro-jenkinsfile-avoid-these-common-ci-cd-mistakes-part-1\/","title":{"rendered":"Pro Jenkinsfile: Avoid These Common Mistakes \u2013 Part 1"},"content":{"rendered":"<h2><span style=\"color: #000000;\"><b>Introduction<\/b><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Hello, DevOps Engineers! Uncle Jenkins here, your trusty CI\/CD butler, ready to help you master the art of writing Jenkinsfiles. Just as a butler ensures everything in the household runs smoothly, I&#8217;m here to ensure your CI\/CD pipelines are efficient and error-free.\u00a0<\/span><span style=\"font-weight: 400;\">Let&#8217;s dive into some common pitfalls you might face and, more importantly, how to get them right. Let&#8217;s get started!<br \/>\n<\/span><\/span><\/p>\n<div id=\"attachment_62208\" style=\"width: 203px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62208\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-62208\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-21-32-285x300.png\" alt=\"Jenkins logo\" width=\"193\" height=\"204\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-21-32-285x300.png 285w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-21-32-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-21-32.png 405w\" sizes=\"(max-width: 193px) 100vw, 193px\" \/><p id=\"caption-attachment-62208\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Jenkins logo<\/span><\/p><\/div>\n<h2><span style=\"color: #000000;\"><b>Not Using Declarative Syntax: Keep It Simple, Silly!<\/b><\/span><\/h2>\n<p><span style=\"font-weight: 400; color: #000000;\">Declarative syntax in Jenkinsfiles is like the gold standard: clear, concise, and easy to work with. Opting for declarative syntax instead of scripted pipelines can prevent confusion and streamline your CI\/CD workflows.<br \/>\n<\/span><\/p>\n<div id=\"attachment_62211\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62211\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-62211 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-23-43.png\" alt=\"Jenkins Pipeline\" width=\"588\" height=\"172\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-23-43.png 588w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-23-43-300x88.png 300w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><p id=\"caption-attachment-62211\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Jenkins Pipeline<\/span><\/p><\/div>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"><br \/>\n<\/span><b>Tip<\/b><span style=\"font-weight: 400;\">: Use the declarative syntax whenever possible. Here&#8217;s a quick example:<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #333333;\">pipeline {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 agent any<\/span><br \/>\n<span style=\"color: #333333;\"> \u00a0 \u00a0 \u00a0 stages {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(&#8216;Build&#8217;) {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo &#8216;Building..&#8217;<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(&#8216;Test&#8217;) {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo &#8216;Testing..&#8217;<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(&#8216;Deploy&#8217;) {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 echo &#8216;Deploying..&#8217;<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">\u00a0 \u00a0 \u00a0 }<\/span><br \/>\n<span style=\"color: #333333;\">}<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #000000;\"><b>Hardcoding Credentials: A Recipe for Disaster<\/b><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Hardcoding credentials in your Jenkinsfile is like leaving the front door unlocked\u2014not very secure! This practice can lead to security vulnerabilities and make it difficult to manage secrets.<br \/>\n<\/span><b>Avoidance Tip<\/b><span style=\"font-weight: 400;\">: Use Jenkins credentials management to handle sensitive information securely. You can then reference these credentials in your Jenkinsfile:<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">pipeline {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 agent any<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Deploy&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 script {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Using withCredentials to access stored credentials<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 withCredentials([usernamePassword(credentialsId: <\/span><span style=\"font-weight: 400;\">&#8216;my-credentials-id&#8217;<\/span><span style=\"font-weight: 400;\">, usernameVariable: <\/span><span style=\"font-weight: 400;\">&#8216;USERNAME&#8217;<\/span><span style=\"font-weight: 400;\">, passwordVariable: <\/span><span style=\"font-weight: 400;\">&#8216;PASSWORD&#8217;<\/span><span style=\"font-weight: 400;\">)]) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8220;Deploying with credentials&#8221;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/span><span style=\"font-weight: 400;\">&#8220;&#8221;&#8221;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Using the credentials in shell commands<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 curl -u <\/span><span style=\"font-weight: 400;\">$USERNAME<\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\">$PASSWORD<\/span><span style=\"font-weight: 400;\"> -X POST http:\/\/example.com\/deploy<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;&#8221;&#8221;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #000000;\"><b style=\"font-size: 1rem;\">Ignoring Error Handling: Don\u2019t Leave It to Chance<\/b><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Pipelines fail. It\u2019s a fact. Ignoring error handling means you&#8217;ll be unprepared when things go wrong, costing you time and resources.<br \/>\n<\/span><b>Avoidance Tip<\/b><span style=\"font-weight: 400;\">: Use try-catch blocks and the post section to manage errors and ensure proper cleanup.<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">pipeline {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 agent any<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 stages {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Build&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 script {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 try {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/span><span style=\"font-weight: 400;\">&#8216;.\/build.sh&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } catch (hudson.AbortException e) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8220;Build aborted: <\/span><span style=\"font-weight: 400;\">${e.message}<\/span><span style=\"font-weight: 400;\">&#8220;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 error(<\/span><span style=\"font-weight: 400;\">&#8220;Stopping the pipeline due to build abortion.&#8221;<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } catch (IOException e) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8220;Build failed due to I\/O error: <\/span><span style=\"font-weight: 400;\">${e.message}<\/span><span style=\"font-weight: 400;\">&#8220;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 error(<\/span><span style=\"font-weight: 400;\">&#8220;Stopping the pipeline due to I\/O error.&#8221;<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } finally {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8216;Cleaning up&#8230;&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 cleanWs()<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #000000;\"><b>Ignoring Workspace Cleanup: Don\u2019t Be a Hoarder<\/b><\/span><\/h2>\n<p><span style=\"font-weight: 400; color: #000000;\">Unused files cluttering your workspace can cause builds to fail. Clean up after yourself.<br \/>\n<\/span><\/p>\n<div id=\"attachment_62213\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62213\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-62213\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-26-03-300x182.png\" alt=\"Clean workspace\" width=\"300\" height=\"182\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-26-03-300x182.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-26-03.png 347w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-62213\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Clean workspace<\/span><\/p><\/div>\n<p><span style=\"color: #000000;\"><b>Avoidance Tip<\/b><span style=\"font-weight: 400;\">: Use the cleanWs step to clean the workspace after build.<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">pipeline {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 agent any<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 stages {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Build&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/span><span style=\"font-weight: 400;\">&#8216;make build&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 post {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 always {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 cleanWs()<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #000000;\"><b style=\"font-size: 1rem;\">Not using Parallelism: Speed Up Your Pipeline<\/b><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Running tasks sequentially that could run in parallel wastes time.<br \/>\n<\/span><b>Avoidance Tip<\/b><span style=\"font-weight: 400;\">: Use the parallel step to run tasks concurrently.<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">pipeline {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 agent any<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 stages {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Test&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parallel failFast: <\/span><span style=\"font-weight: 400;\">true<\/span><span style=\"font-weight: 400;\">, stages: {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Unit Test&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8216;Running unit tests..&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Integration Test&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8216;Running integration tests..&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">\u00a0<\/span><b>Key Points:<\/b><\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"color: #000000;\"><b>Parallel Block with failFast<\/b><span style=\"font-weight: 400;\">: The failFast parameter is true in the parallel block. If either the &#8220;Unit Test&#8221; or &#8220;Integration Test&#8221; stage fails, the other parallel stage is immediately halted, and the pipeline progresses to any post-failure handling without wasting time or resources on a build that\u2019s already known to have issues.<\/span><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"color: #000000;\"><b>Immediate Feedback<\/b><span style=\"font-weight: 400;\">: This setup ensures that any failure in the testing phase is immediately caught and addressed, speeding up the development cycle by reducing the wait time for feedback.<\/span><\/span><\/li>\n<\/ul>\n<h2><span style=\"color: #000000;\"><b>Neglecting to Use Jenkins Shared Library for Reusable Code: Simplify and Standardize<\/b><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Duplicated code is error-prone and difficult to maintain. Jenkins Shared Libraries help you<\/span><b> DRY (Don\u2019t Repeat Yourself)<\/b><span style=\"font-weight: 400;\">.<br \/>\n<\/span><\/span><\/p>\n<div id=\"attachment_62215\" style=\"width: 370px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-62215\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-62215\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-30-34-300x190.png\" alt=\"shared library\" width=\"360\" height=\"228\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-30-34-300x190.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-30-34-768x487.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-30-34-624x395.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-06-09-30-34.png 789w\" sizes=\"(max-width: 360px) 100vw, 360px\" \/><p id=\"caption-attachment-62215\" class=\"wp-caption-text\">shared library<\/p><\/div>\n<p><span style=\"color: #000000;\"><b>Avoidance Tip<\/b><b>:<\/b> Using shared libraries to encapsulate common logic.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">@Library(<\/span><span style=\"font-weight: 400;\">&#8216;jenkins-shared-library&#8217;<\/span><span style=\"font-weight: 400;\">) _<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">pipeline {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 agent any<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 stages {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 stage(<\/span><span style=\"font-weight: 400;\">&#8216;Build&#8217;<\/span><span style=\"font-weight: 400;\">) {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Using a custom <\/span><span style=\"font-weight: 400;\">function<\/span><span style=\"font-weight: 400;\"> from the shared library<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 customBuildFunction()<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 }<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">}<\/span><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span style=\"color: #000000;\"><b>Conclusion<\/b><\/span><\/h3>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Mastering Jenkinsfile creation is important for optimizing CI\/CD pipelines. Avoiding common pitfalls and adopting best practices can significantly enhance pipeline efficiency. Consider connecting with <strong>TO THE NEW<\/strong> for further support and expertise, especially in managing complex CI\/CD tasks<\/span><span style=\"font-weight: 400;\">. Our team of <\/span><b>AWS Certified Architects &amp; DevOps Engineers <\/b><span style=\"font-weight: 400;\">can assist in streamlining your operations, offering services from migration to managed cloud solutions to disaster recovery, and ensuring your CI\/CD pipelines are as efficient and resilient as possible. <\/span><span style=\"font-weight: 400;\">We still need to discuss more points regarding how to write a professional Jenkinsfile. Stay tuned for the next blog, covering additional strategies and insights to help you master Jenkinsfile creation and further improve your CI\/CD practices.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Hello, DevOps Engineers! Uncle Jenkins here, your trusty CI\/CD butler, ready to help you master the art of writing Jenkinsfiles. Just as a butler ensures everything in the household runs smoothly, I&#8217;m here to ensure your CI\/CD pipelines are efficient and error-free.\u00a0Let&#8217;s dive into some common pitfalls you might face and, more importantly, how [&hellip;]<\/p>\n","protected":false},"author":1601,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":50},"categories":[2348],"tags":[5983,4252,1892,1682,5982],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62205"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1601"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=62205"}],"version-history":[{"count":15,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62205\/revisions"}],"predecessor-version":[{"id":62253,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62205\/revisions\/62253"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=62205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=62205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=62205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}