{"id":62261,"date":"2024-06-27T17:57:21","date_gmt":"2024-06-27T12:27:21","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=62261"},"modified":"2024-06-27T18:45:47","modified_gmt":"2024-06-27T13:15:47","slug":"pro-jenkinsfile-avoid-these-common-mistakes-part-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/pro-jenkinsfile-avoid-these-common-mistakes-part-2\/","title":{"rendered":"Pro Jenkinsfile: Avoid These Common Mistakes \u2013 Part 2"},"content":{"rendered":"<p><span style=\"color: #000000;\"><b>Introduction<br \/>\n<\/b><\/span><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Hello again, DevOps Engineers! Uncle Jenkins is back, ready to dive deeper into the world of Jenkinsfiles. Our previous blog covered some important tips to avoid common CI\/CD pitfalls. If you missed it, check out <\/span><a style=\"color: #000000;\" href=\"https:\/\/www.tothenew.com\/blog\/pro-jenkinsfile-avoid-these-common-ci-cd-mistakes-part-1\/\"><span style=\"font-weight: 400;\">Part 1<\/span><\/a><span style=\"font-weight: 400;\"> of this series to catch up on those crucial points. In this blog, we&#8217;ll explore more advanced strategies to refine and optimize your Jenkinsfiles. <\/span><span style=\"font-weight: 400;\">Let&#8217;s continue our journey towards mastering Jenkinsfile creation. Ready to level up? Let&#8217;s get started!<br \/>\n<\/span><\/span><\/p>\n<div id=\"attachment_62351\" style=\"width: 300px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-62351\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-62351\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-44-00-290x300.png\" alt=\"Jenkin-logo\" width=\"290\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-44-00-290x300.png 290w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-44-00-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-44-00.png 458w\" sizes=\"(max-width: 290px) 100vw, 290px\" \/><p id=\"caption-attachment-62351\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Jenkin-logo<\/span><\/p><\/div>\n<p><span style=\"color: #000000;\"><b>1.\u00a0 Ignoring Environment Variables: Don&#8217;t Hardcode Values<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Hardcoding values make your pipeline inflexible and difficult to maintain.<br \/>\n<\/span><\/span><span style=\"text-decoration: underline;\"><b>Avoidance Tip<\/b><\/span><span style=\"font-weight: 400;\">: Use the <\/span><a href=\"https:\/\/www.jenkins.io\/doc\/pipeline\/tour\/environment\/\"><span style=\"font-weight: 400;\">environment<\/span> <\/a><span style=\"font-weight: 400;\">directive to set variables and the <\/span><a href=\"https:\/\/www.jenkins.io\/doc\/pipeline\/steps\/credentials-binding\/\"><span style=\"font-weight: 400;\">withCredentials<\/span><\/a><span style=\"font-weight: 400;\"> blocks to <\/span><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">manage configurations and secrets.<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><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 environment {<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 APP_ENV = <\/span><span style=\"font-weight: 400;\">&#8216;prod&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 API_ENDPOINT = <\/span><span style=\"font-weight: 400;\">&#8216;https:\/\/api.example.com&#8217;<\/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 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 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8220;Building application for <\/span><span style=\"font-weight: 400;\">${env.APP_ENV}<\/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 }<\/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 \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 withCredentials([string(credentialsId: <\/span><span style=\"font-weight: 400;\">&#8216;aws-credentials-id&#8217;<\/span><span style=\"font-weight: 400;\">, variable: <\/span><span style=\"font-weight: 400;\">&#8216;AWS_CREDENTIALS&#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 <\/span><span style=\"font-weight: 400;\">echo<\/span> <span style=\"font-weight: 400;\">&#8220;Deploying to <\/span><span style=\"font-weight: 400;\">${env.APP_ENV}<\/span><span style=\"font-weight: 400;\"> using <\/span><span style=\"font-weight: 400;\">${env.API_ENDPOINT}<\/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 }<\/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><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"><b style=\"font-size: 1rem;\">2. Stashing and Unstashing Artifacts<\/b><\/span><\/span><\/h2>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"><span style=\"text-decoration: underline;\">Common Mistakes:<\/span><br \/>\n<\/span><\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Overuse of stash and unstash: Excessive use can lead to unnecessary resource usage and complexity.<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Naming Collisions: Using non-unique names for stashes can lead to unexpected behaviors.<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Size Limitations: Not considering the size of stashed files, can lead to performance issues.<\/span><\/p>\n<p><span style=\"font-weight: 400; color: #000000;\"><span style=\"text-decoration: underline;\">Best Practices<\/span>:<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Use stash and unstash for small, critical artifacts that need to be transferred between stages.<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Ensure unique and meaningful names for each stash to avoid collisions.<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\">&#8211; Be mindful of the size and frequency of stashed content.<\/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 sh <\/span><span style=\"font-weight: 400;\">&#8216;echo &#8220;Build artifact&#8221; &gt; artifact.txt&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stash includes: <\/span><span style=\"font-weight: 400;\">&#8216;artifact.txt&#8217;<\/span><span style=\"font-weight: 400;\">, name: <\/span><span style=\"font-weight: 400;\">&#8216;build-artifact&#8217;<\/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 \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 unstash <\/span><span style=\"font-weight: 400;\">&#8216;build-artifact&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/span><span style=\"font-weight: 400;\">&#8216;echo &#8220;Deploying artifact:&#8221; &amp;&amp; cat artifact.txt&#8217;<\/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<p><span style=\"color: #000000;\"><b>3. Overusing sh Steps for Shell Commands: Keep it Clean<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Using sh steps everywhere makes your Jenkinsfile look like a tangled mess of shell scripts.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><b><span style=\"text-decoration: underline;\"><br \/>\nAvoidance Tip<\/span>:<\/b> <span style=\"font-weight: 400;\">Use built-in Jenkins steps and plugins whenever possible. Reserve sh steps for when you truly need to execute shell commands. Example:<br \/>\nInstead of this:<br \/>\n<\/span><\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>pipeline {<\/b><b><br \/>\n<\/b><b> \u00a0 agent any<\/b><b><br \/>\n<\/b><b> \u00a0 stages {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 stage(<\/b><b>&#8216;Build&#8217;<\/b><b>) {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/b><b><br \/>\n<\/b><b>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/b><b>&#8216;cp src\/* output\/&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/b><b>&#8216;gradle build&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 }<\/b><b><br \/>\n<\/b><b>}<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Keep it clean like this:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>pipeline {<\/b><b><br \/>\n<\/b><b> \u00a0 agent any<\/b><b><br \/>\n<\/b><b> \u00a0 stages {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 stage(<\/b><b>&#8216;Build&#8217;<\/b><b>) {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/b><b>&#8221;&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 cp src\/* output\/<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 gradle build<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8221;&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 }<\/b><b><br \/>\n<\/b><b>}<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b style=\"color: #000000; font-size: 1rem;\">4. Ignoring Pipeline as Code Best Practices<\/b><\/p>\n<p><span style=\"font-weight: 400; color: #000000;\">Not treating your Jenkinsfile with the same care as other code can lead to issues. This includes not version controlling your Jenkinsfile, not reviewing changes through pull requests, and not testing changes thoroughly.<br \/>\n<\/span><span style=\"color: #000000;\"><b><span style=\"text-decoration: underline;\"><br \/>\nAvoidance Tip<\/span>:<\/b><\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400; color: #000000;\"><span style=\"font-weight: 400;\">Storing your Jenkinsfile in your version control system like Github.<br \/>\n<\/span><\/span><\/p>\n<p><div id=\"attachment_62352\" style=\"width: 337px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-62352\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-62352\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-45-58-300x145.png\" alt=\"Code Best Practices\" width=\"327\" height=\"158\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-45-58-300x145.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-45-58-768x371.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-45-58-624x302.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-45-58.png 920w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><p id=\"caption-attachment-62352\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Code Best Practices<\/span><\/p><\/div><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400; color: #000000;\">Using code reviews and pull requests to review changes to the Jenkinsfile.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400; color: #000000;\">Testing Jenkinsfile changes in a staging environment before applying them to production.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><b>5. Neglecting Notifications: Don&#8217;t Keep Stakeholders in the Dark<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Failing to notify stakeholders about build status can lead to communication breakdowns and delays.<br \/>\n<\/span><\/span><strong><span style=\"text-decoration: underline;\">Avoidance Tip<\/span><\/strong><span style=\"font-weight: 400;\">: Use the <\/span><a href=\"https:\/\/www.jenkins.io\/doc\/book\/pipeline\/syntax\/\"><span style=\"font-weight: 400;\">post<\/span><\/a><span style=\"font-weight: 400;\"> section to send notifications about build status.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>@Library(<\/b><b>&#8216;shared_lib@master&#8217;<\/b><b>) _<\/b><b><br \/>\n<\/b><b>pipeline {<\/b><b><br \/>\n<\/b><b> \u00a0 agent any<\/b><b><br \/>\n<\/b><b> \u00a0 stages {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 stage(<\/b><b>&#8220;Custom Stage&#8221;<\/b><b>) {<\/b><b><br \/>\n<\/b><b>\u00a0 \u00a0 \u00a0 \u00a0 \/\/ some steps<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 post {<\/b><b><br \/>\n<\/b><b>\u00a0 \u00a0 \u00a0 \u00a0 failure {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 script {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 def failureMessage = <\/b><b>&#8220;Job Failed &#8211; <\/b><b>${BUILD_URL}<\/b><b>&#8220;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 jenkinsNotifications.notify(<\/b><b>&#8220;Failure&#8221;<\/b><b>, failureMessage, <\/b><b>&#8220;Jenkins-prod-failures&#8221;<\/b><b>)<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 }<\/b><b><br \/>\n<\/b><b>}<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\"><b>6. Ignoring Agent Specification for Different Stages<\/b><\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-weight: 400;\"><span style=\"text-decoration: underline;\"><strong>Problem:<\/strong><\/span> Not specifying agents for different stages can lead to inefficient resource usage, especially in multi-node setups.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><span style=\"text-decoration: underline;\"><b>Avoidance Tip<\/b><\/span>: Specifying agents at the stage level when different resources are needed.<br \/>\n<\/span><\/span><\/p>\n<div id=\"attachment_62353\" style=\"width: 310px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-62353\" decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-62353\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-48-02-300x192.png\" alt=\"Jenkins-agents\" width=\"300\" height=\"192\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-48-02-300x192.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-48-02-624x399.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/06\/Screenshot-from-2024-06-16-20-48-02.png 651w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><p id=\"caption-attachment-62353\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Jenkins-agents<\/span><\/p><\/div>\n<p><span style=\"font-weight: 400; color: #000000;\">Example With Stage-specific Agents :<br \/>\n<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>pipeline {<\/b><b><br \/>\n<\/b><b> \u00a0 stages {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 stage(<\/b><b>&#8216;Build&#8217;<\/b><b>) {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 agent {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 label <\/b><b>&#8216;build_agent&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/b><b>&#8216;make build&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 stage(<\/b><b>&#8216;Test&#8217;<\/b><b>) {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 agent {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 label <\/b><b>&#8216;test_agent&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 steps {<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sh <\/b><b>&#8216;make test&#8217;<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 \u00a0 \u00a0 }<\/b><b><br \/>\n<\/b><b> \u00a0 }<\/b><b><br \/>\n<\/b><b>}<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400; color: #000000;\">\u00a0<\/span><span style=\"color: #000000;\"><b>7. Implementing Complex workflow with <\/b><b>build<\/b><b> and <\/b><b>waitUntil.<\/b><\/span><\/p>\n<p><span style=\"font-weight: 400; color: #000000;\"><span style=\"text-decoration: underline;\"><strong>Problem:<\/strong><\/span> Incorrect Job Triggering and Poor Error Handling. <\/span><span style=\"font-weight: 400; color: #000000;\">Misconfiguring downstream job triggers can lead to pipeline failures and hard-to-debug issues.<br \/>\n<\/span><span style=\"font-weight: 400; color: #000000;\"><span style=\"text-decoration: underline;\"><strong>Avoidance Tip<\/strong><\/span>: Handle Downstream Jobs and Errors Gracefully.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Use <\/span><span style=\"font-weight: 400;\">build<\/span><span style=\"font-weight: 400;\"> and <\/span><a href=\"https:\/\/www.jenkins.io\/doc\/pipeline\/steps\/workflow-basic-steps\/#waituntil-wait-for-condition\"><span style=\"font-weight: 400;\">waitUntil<\/span><\/a><span style=\"font-weight: 400;\"> with proper error handling and exit conditions. <\/span><\/span>Example: Triggering Downstream Job<\/li>\n<\/ul>\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;Trigger Downstream&#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 def downstreamBuild = build job: <\/span><span style=\"font-weight: 400;\">&#8216;downstream-job&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">wait<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">true<\/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;Downstream build result: <\/span><span style=\"font-weight: 400;\">${downstreamBuild.result}<\/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 } catch (Exception 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 error <\/span><span style=\"font-weight: 400;\">&#8220;Downstream job failed: <\/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 }<\/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<p><span style=\"color: #000000;\"><b>Conclusion<br \/>\n<\/b><\/span><span style=\"font-weight: 400; color: #000000;\">In this second part of our blog series, we dived deeper into the degree of professional Jenkinsfile creation. These strategies are designed to streamline your CI\/CD processes and mitigate risks. In the next blog of our series, we will continue to explore additional strategies to help you master Jenkinsfile creation and further improve your CI\/CD practices. <\/span><span style=\"font-weight: 400; color: #000000;\">Contact our team at <a href=\"https:\/\/www.tothenew.com\/\"><strong>TO THE NEW<\/strong><\/a> for personalized assistance and to elevate your DevOps practices. Let\u2019s build a more reliable and efficient CI\/CD pipeline together!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Hello again, DevOps Engineers! Uncle Jenkins is back, ready to dive deeper into the world of Jenkinsfiles. Our previous blog covered some important tips to avoid common CI\/CD pitfalls. If you missed it, check out Part 1 of this series to catch up on those crucial points. In this blog, we&#8217;ll explore more advanced [&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":15},"categories":[2348],"tags":[5983,4252,1892,1682,5982],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62261"}],"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=62261"}],"version-history":[{"count":12,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62261\/revisions"}],"predecessor-version":[{"id":62656,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/62261\/revisions\/62656"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=62261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=62261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=62261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}