{"id":826,"date":"2024-08-12T01:09:00","date_gmt":"2024-08-12T01:09:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=826"},"modified":"2024-08-12T01:09:00","modified_gmt":"2024-08-12T01:09:00","slug":"jenkins-pipeline-as-code","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=826","title":{"rendered":"Jenkins Pipeline Tutorial For Beginners: Pipeline as Code"},"content":{"rendered":"<p>Jenkins pipeline as code is a concept that defines a Jenkins build pipeline in Jenkins DSL\/Groovy format. This article covers the key concepts of writing Jenkins pipeline as code using declarative syntax.<\/p>\n<h2 id=\"jenkins-pipeline-tutorial\">Jenkins Pipeline Tutorial<\/h2>\n<p>In this Jenkins pipeline tutorial, we will look at the following<\/p>\n<ol>\n<li>Types of Jenkins Pipeline<\/li>\n<li>Pipeline as code basics<\/li>\n<li>Building a basic CI pipeline as code for a Java app.<\/li>\n<li>Building a job from pipeline code present in source code repo.<\/li>\n<li>Executing parallel stages in a pipeline<\/li>\n<li>Generating pipeline script &amp; directives using Jenkins inbuilt generators.<\/li>\n<\/ol>\n<h2 id=\"types-of-jenkins-pipeline\">Types of Jenkins Pipeline<\/h2>\n<p>There are two types of Jenkins pipeline code.<\/p>\n<ol>\n<li>Declarative Pipeline<\/li>\n<li>Scripted Pipeline<\/li>\n<\/ol>\n<p>In this tutorial, we will focus only on the declarative syntax, which is an advanced version of the scripted pipeline with extensible features. <strong>I recommend using the declarative pipeline<\/strong> approach for all your Jenkins use cases. It offers many features and benefits.<\/p>\n<p>Now let&#8217;s get started with the pipeline tutorial.<\/p>\n<p>For better understanding, we will create a pipeline for the <strong>Java Spring Boot application<\/strong> build using the declarative pipeline as code.<\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ol>\n<li><strong>Jenkins controller <\/strong> (<a href=\"https:\/\/devopscube.com\/install-configure-jenkins-2-centos-redhat-servers\/\" rel=\"noreferrer\">Check Jenkins installation steps<\/a>)<\/li>\n<li><strong>Jenkins agent<\/strong> node connected to the controller (<a href=\"https:\/\/devopscube.com\/setup-slaves-on-jenkins-2\/\" rel=\"noreferrer\">Check Jenkins agent setup guide<\/a>)<\/li>\n<li><strong>Access to Github.com<\/strong> from your Jenkins server. If you are trying out from your corporate Jenkins setup, you can use your organization&#8217;s private git repo.<\/li>\n<\/ol>\n<p>Here is the pictorial representation of the simple build pipeline we will build.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-pipeline-flow.gif\" class=\"kg-image\" alt=\"Jenkins Pipeline workflow\" loading=\"lazy\" width=\"750\" height=\"799\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/jenkins-pipeline-flow.gif 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-pipeline-flow.gif 750w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to View in HD<\/span><\/figcaption><\/figure>\n<blockquote><p><strong>Note: <\/strong>Do not worry about the DSL used in the pipeline code. Read the article fully to understand how to generate the DSLs in an easy way.<\/p><\/blockquote>\n<p>Here is the pipeline code for the above workflow. Before setting up the pipeline, we will understand what each block means.<\/p>\n<pre><code>\npipeline {\n\n    agent {\n        node {\n            label 'AGENT-01'\n        }\n    }\n\n    tools { \n        maven 'maven3' \n    }\n\n    options {\n        buildDiscarder logRotator( \n                    daysToKeepStr: '15', \n                    numToKeepStr: '10'\n            )\n    }\n\n    environment {\n        APP_NAME = \"DCUBE_APP\"\n        APP_ENV  = \"DEV\"\n    }\n\n    stages {\n        \n        stage('Cleanup Workspace') {\n            steps {\n                cleanWs()\n                sh \"\"\"\n                echo \"Cleaned Up Workspace for ${APP_NAME}\"\n                \"\"\"\n            }\n        }\n\n        stage('Code Checkout') {\n            steps {\n                checkout([\n                    $class: 'GitSCM', \n                    branches: [[name: '*\/main']], \n                    userRemoteConfigs: [[url: 'https:\/\/github.com\/spring-projects\/spring-petclinic.git']]\n                ])\n            }\n        }\n\n        stage('Code Build') {\n            steps {\n                 sh 'mvn install -Dmaven.test.skip=true'\n            }\n        }\n\n        stage('Printing All Global Variables') {\n            steps {\n                sh \"\"\"\n                env\n                \"\"\"\n            }\n        }\n\n    }   \n}<\/code><\/pre>\n<p>Now, let&#8217;s understand what each block means.<\/p>\n<p><strong>The Pipeline Block:<\/strong> All your pipelines, as code, should be wrapped inside the pipeline block.<\/p>\n<pre><code>pipeline {\n\n---&lt;All Pipeline blocks go here&gt;---\n\n}<\/code><\/pre>\n<p><strong>Agent Block:<\/strong> In the agent block, you have to mention the agent details for where you want to run the pipeline job. It supports both <a href=\"https:\/\/devopscube.com\/setup-slaves-on-jenkins-2\/\" rel=\"noreferrer noopener\">static agents<\/a> and <a href=\"https:\/\/devopscube.com\/docker-containers-as-build-slaves-jenkins\/\" rel=\"noreferrer noopener\">docker-based<\/a> dynamic agents. Here we have mentioned the label as &#8220;<code>AGENT-01<\/code>&#8220;. It is the name of an agent node.<\/p>\n<pre><code>    agent {\n        node {\n            label 'AGENT-01'\n        }\n    }<\/code><\/pre>\n<p><strong>Tools Block:<\/strong> Here, you can refer to the tools you have configured in the Jenkins Global tool configuration. We have covered the tool configuration in the next section.<\/p>\n<pre><code>tools { \n        maven 'maven3' \n    }<\/code><\/pre>\n<p><strong>Options Block:<\/strong> It contains all the options required for the job. One example we have given is &#8220;Build Discarder,&#8221; which rotates the build logs.<\/p>\n<pre><code>options {\n        buildDiscarder logRotator( \n                    daysToKeepStr: '15', \n                    numToKeepStr: '10'\n            )\n    }<\/code><\/pre>\n<p><strong>Environment Block:<\/strong> In this block, you can mention all the required variables for the job that must be available in all the stages. You can define any number of variables, like a key-value pair.<\/p>\n<pre><code>    environment {\n        APP_NAME = \"DCUBE_APP\"\n        APP_ENV  = \"DEV\"\n    }<\/code><\/pre>\n<p><strong>Stages:<\/strong> Stages block contains multiple stages.<\/p>\n<pre><code>stages {\n\n---&lt;All Pipeline Stages Go Here&gt;---\n\n}<\/code><\/pre>\n<p><strong>Stage Block:<\/strong> A stage is a subset of stages. Each stage has a <code>steps<\/code> block.<\/p>\n<p><strong>Steps Block<\/strong>: In each <code>steps<\/code> block, you define a set of operations to perform. In the following example, we show a workplace cleanup step and echo a variable we defined in the environment block. What you want to run inside each stage depends totally on your use case.<\/p>\n<pre><code>stage('Cleanup Workspace') {\n            steps {\n                cleanWs()\n                sh \"\"\"\n                echo \"Cleaned Up Workspace for ${APP_NAME}\"\n                \"\"\"\n            }\n        }<\/code><\/pre>\n<p>Now that we have a basic understanding of a minimal pipeline as code, let&#8217;s execute it practically on a Jenkins server with an agent node.<\/p>\n<h2 id=\"configure-pipeline-as-code-job-in-jenkins\">Configure Pipeline as Code Job In Jenkins<\/h2>\n<p>To execute the pipeline code we have in this article, we need to configure maven in the global tool configuration.<\/p>\n<p>Go to Manage Jenkins &#8211;&gt; Global Tool Configuration &#8211;&gt; Maven &#8211;&gt; Maven Installation.<\/p>\n<p>Add a maven configuration as shown below. We are using the tool name as <code>maven3<\/code> in the pipeline so that it refers to the maven under &#8220;Global Tool Configuration&#8221;.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-maven-tool.jpg\" class=\"kg-image\" alt=\"configuring maven as global\" loading=\"lazy\" width=\"1952\" height=\"752\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/jenkins-maven-tool.jpg 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/jenkins-maven-tool.jpg 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1600\/2025\/03\/jenkins-maven-tool.jpg 1600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/jenkins-maven-tool.jpg 1952w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<blockquote><p><strong>Note:<\/strong> We have selected &#8220;Install Automatically&#8221; option, which will download the selected version every time you execute the job.<\/p><\/blockquote>\n<h2 id=\"creating-building-a-jenkins-pipeline-job\">Creating &amp; Building a Jenkins Pipeline Job<\/h2>\n<p>Follow the steps given below to create and build our pipeline as code.<\/p>\n<p><strong>Step 1:<\/strong> Go to Jenkins home and select &#8220;New Item&#8221;<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-10-22.png\" class=\"kg-image\" alt=\"creating a new build item\" loading=\"lazy\" width=\"756\" height=\"303\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-10-22.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-10-22.png 756w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 2:<\/strong> Give a name, select &#8220;Pipeline&#8221; and click ok.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-19.png\" class=\"kg-image\" alt=\"selecting a build type and its name\" loading=\"lazy\" width=\"1175\" height=\"912\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-11-19.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-11-19.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-11-19.png 1175w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 3:<\/strong> Scroll down to the Pipeline section, copy the whole pipeline code in the script section, and save it.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-23-16.png\" class=\"kg-image\" alt=\"write a pipeline script and press save to save the configurations\" loading=\"lazy\" width=\"1155\" height=\"552\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-23-16.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-23-16.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-23-16.png 1155w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p><strong>Step 4:<\/strong> Now, click &#8220;Build Now&#8221; and wait for the build to start.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-13-19.png\" class=\"kg-image\" alt=\"press build now button to start the build\" loading=\"lazy\" width=\"843\" height=\"413\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-13-19.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-13-19.png 843w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>While the job starts, you can view each stage executing in stage view. Here is the screenshot of a successfully executed job. Also, you can the job logs by clicking the blue icon.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-27.png\" class=\"kg-image\" alt=\"build success logs\" loading=\"lazy\" width=\"1278\" height=\"608\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-14-27.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-14-27.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-27.png 1278w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you have the Blue Ocean plugin installed, you will have a very good UI to view your job status and logs, as shown below. Use the &#8220;Open in Blue Ocean&#8221; from the left to open a job in the blue ocean view.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-15-17.png\" class=\"kg-image\" alt=\"blue ocean build view\" loading=\"lazy\" width=\"1282\" height=\"528\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-15-17.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-15-17.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-15-17.png 1282w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"executing-jenkins-pipeline-from-github-jenkinsfile\">Executing Jenkins Pipeline From Github (Jenkinsfile)<\/h2>\n<p>In the last section, we used the pipeline script directly on Jenkins. In this section, we will look at how to execute a pipeline script that is available in an SCM system like Github.<\/p>\n<p><strong>Step 1: <\/strong> Create a Github repo with our pipeline code in a file named <code>Jenkinsfile<\/code>. Or you can use this Github repo for testing. <a href=\"https:\/\/github.com\/devopscube\/pipeline-as-code-demo?ref=devopscube.com\">https:\/\/github.com\/devopscube\/pipeline-as-code-demo<\/a><\/p>\n<p><strong>Step 2: <\/strong>Follow the same steps we used for creating a pipeline job. But instead of entering the code directly into the script block, select the &#8220;Pipeline script from SCM&#8221; option and fill in the details as shown below.<\/p>\n<ol>\n<li><strong>Definition: <\/strong>Pipeline script from SCM<\/li>\n<li><strong>Repository URL:<\/strong> <a href=\"https:\/\/github.com\/devopscube\/pipeline-as-code-demo?ref=devopscube.com\">https:\/\/github.com\/devopscube\/pipeline-as-code-demo<\/a><\/li>\n<li><strong>Script Path:<\/strong> Jenkinsfile<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/blog.techiescamp.com\/content\/images\/2024\/08\/image-69.png\" class=\"kg-image\" alt=\"using pipeline script from github\" loading=\"lazy\" width=\"1326\" height=\"693\"><\/figure>\n<p>Specify the required details and save the configurations.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/blog.techiescamp.com\/content\/images\/2024\/08\/image-70.png\" class=\"kg-image\" alt=\"using pipeline script from github\" loading=\"lazy\" width=\"1328\" height=\"745\"><\/figure>\n<p><strong>Step 3:<\/strong> Save the configuration and run the build. You should see a successful build.<\/p>\n<h2 id=\"executing-jenkins-pipeline-stages-in-parallel\">Executing Jenkins Pipeline Stages In Parallel<\/h2>\n<p>There are use cases where you have to execute different stages in parallel because each stage is independent and does not depend on other steps. Running separate stages in parallel will also reduce the build times.<\/p>\n<p>You can achieve parallelism in Jenkins pipelines as code using the <strong>parallel block.<\/strong><\/p>\n<p>Here is an example stage that contains three parallel stages. It&#8217;s like you will have multiple stages inside a stage. You can test this by adding the following code to your existing pipeline.<\/p>\n<pre><code>stage('Environment Analysis') {\n\n            parallel {\n\n                stage('Priting All Global Variables') {\n                    steps {\n                        sh \"\"\"\n                        env\n                        \"\"\"\n                    }\n                }\n\n                stage('Execute Shell') {\n                    steps {\n                        sh 'echo \"Hello\"'\n                    }\n                }\n\n                stage('Print ENV variable') {\n                    steps {\n                        sh \"echo ${APP_ENV}\"\n                    }\n                }\n\n            \n            }\n        }<\/code><\/pre>\n<p>You can clearly see the parallel execution on the blue ocean view.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-18-23.png\" class=\"kg-image\" alt=\"blue ocean build view\" loading=\"lazy\" width=\"1255\" height=\"569\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-18-23.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-18-23.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-18-23.png 1255w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"how-to-generate-jenkins-pipeline-scripts\">How to Generate Jenkins Pipeline Scripts?<\/h2>\n<p>Is it possible to generate Jenkins pipeline scripts?<\/p>\n<p>Yes! you can generate most of the pipeline scripts from Jenkins. Here is how it works.<\/p>\n<p>Jenkins has its own pipeline script generator. You can access the generator on the <code>\/pipeline-syntax<\/code> path.<\/p>\n<pre><code>http:\/\/&lt;your-jenkins-ip&gt;:port\/pipeline-syntax\/<\/code><\/pre>\n<p>You can also get the syntax generator path from your pipeline job configuration as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-19-16.png\" class=\"kg-image\" alt=\"pipeline syntax generator\" loading=\"lazy\" width=\"916\" height=\"467\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-19-16.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-19-16.png 916w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h3 id=\"snippet-generator\">Snippet Generator<\/h3>\n<p>A Snippet generator is used to generate all the scripts that are used inside the stages.<\/p>\n<p>Here is how the generator looks. You can select the required option from the steps dropdown.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-24-15.png\" class=\"kg-image\" alt=\"snippet generator\" loading=\"lazy\" width=\"1399\" height=\"730\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-24-15.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-24-15.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-24-15.png 1399w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Fill in the required details, and generate the script for your pipeline.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/blog.techiescamp.com\/content\/images\/2024\/08\/image-74.png\" class=\"kg-image\" alt=\"snippet generator\" loading=\"lazy\" width=\"1263\" height=\"556\"><\/figure>\n<h3 id=\"declarative-directive-generator\">Declarative Directive Generator<\/h3>\n<p>You can use the directive generator to generate all other options in the pipeline, such as options, parameters, triggers, etc.<\/p>\n<p>Here is an example of generating the agent block.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-22-18.png\" class=\"kg-image\" alt=\"Declarative Directive Generator\" loading=\"lazy\" width=\"1073\" height=\"662\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-22-18.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/image-22-18.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-22-18.png 1073w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Adopting pipeline code for all Jenkins workflows will give you more control and streamline your pipelines for your CI\/CD needs.<\/p>\n<p>In the next post, we will learn about <a href=\"https:\/\/devopscube.com\/jenkins-multibranch-pipeline-tutorial\/\">Jenkins Multi Branch Pipeline<\/a><\/p>\n<p>Extending your pipelines with a <a href=\"https:\/\/devopscube.com\/jenkins-shared-library-tutorial\/\" rel=\"noreferrer noopener\">shared library<\/a> lets you reuse the pipeline code for all implementations.<\/p>\n<p>Let us know your thoughts in the comment section below.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/jenkins-pipeline-as-code\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jenkins Pipeline Tutorial For Beginners: Pipeline as Code \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/jenkins-pipeline-as-code\/<\/p>\n","protected":false},"author":1,"featured_media":827,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=826"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/826\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/827"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}