How to Create a Jenkins Declarative Pipeline | #Day 26 |# 90 Days of DevOps

ยท

3 min read

How to Create a Jenkins Declarative Pipeline | #Day 26 |# 90 Days of DevOps

Demystifying Jenkins Pipelines: Declarative vs. Scripted ๐Ÿš€

In the world of DevOps and Continuous Integration/Continuous Deployment (CI/CD), Jenkins has been a trusty companion for automating the software development and delivery process. One of the key features that makes Jenkins so powerful is its support for pipelines. ๐Ÿ› ๏ธ

What is a Pipeline? ๐ŸŒŠ

A Jenkins pipeline is a suite of plugins that enables the automation of building, testing, and deploying code. It allows you to define a series of steps that are executed in a specific order, facilitating the entire CI/CD process. Jenkins pipelines come in two flavors: Declarative and Scripted.

Declarative Pipeline ๐Ÿ“

The Declarative Pipeline is a more structured and concise way of defining your pipeline. It uses a simple and human-readable syntax, making it easier to understand and maintain. You describe what you want to do, and Jenkins takes care of the "how." Declarative pipelines are great for simple to medium-complexity workflows.

Scripted Pipeline ๐Ÿ“œ

On the other hand, the Scripted Pipeline offers more flexibility and power. It's essentially a full-fledged Groovy script where you have complete control over the flow. While this flexibility is great for complex workflows, it can be overwhelming and less readable for simple tasks.

Why You Should Use a Declarative Pipeline? ๐Ÿง

Choosing between Declarative and Scripted pipelines depends on your specific needs, but here's why you should consider using Declarative pipelines:

  1. Readability: Declarative pipelines are easier to read and understand, even for team members who might not be familiar with Groovy scripting.

  2. Maintainability: They are more maintainable as the structure enforces best practices and separates concerns.

  3. Validation: Jenkins can perform static analysis on Declarative pipelines to catch issues early, reducing pipeline failures.

  4. Security: Declarative pipelines are more secure because they limit what can be executed, reducing the risk of malicious code.

  5. Declarative pipelines are more portable and reusable.

  6. Declarative pipelines are less prone to errors.

How to Create a Simple Pipeline in Jenkins? ๐Ÿš€

Creating a simple Declarative Pipeline in Jenkins is a breeze. Follow these steps:

  1. Log in to Jenkins: Access your Jenkins dashboard.

  2. Create a New Item: Click on "New Item," give it a name, and select "Pipeline" as the project type.

  3. Configure Your Pipeline: Scroll down to the Pipeline section, choose "Pipeline script from SCM," and select your source code management system (e.g., Git). Provide the repository URL.

  4. Define Your Pipeline: In your project's root directory, create a file named Jenkinsfile. This file will contain your Declarative Pipeline script.

  5. Write Your Pipeline Script: In the Jenkinsfile, define your pipeline stages using the Declarative Pipeline syntax. For example:

    Here is an example of a simple declarative pipeline in Jenkins:

  6.   pipeline {
          agent any
          stages {
              stage("code") {
                  steps { 
                      echo "cloning the code"
                  }
              }
              stage('Test') {
                  steps {
                      // Your test steps here
                  }
              }
              stage('Build') {
                  steps {
                      echo "Bulding the code"
                      // Your building steps here
                         }
               }
               stage ('push to docker hub') {
                    steps {
                         echo "pushing the image to docker hub"
                                     // Your pushing steps here
                           }
              }
              stage ('Deploy') {
                    steps {
                         echo "Deplyoing the container"
                          }
              }
          }
      }
    
  7. Save and Build: Save your Jenkinsfile, and Jenkins will automatically detect changes and start the pipeline.

    This pipeline will clone the code from the github repo, build it, test it, push the image to artifactory and then deploy it. You can add or remove steps from the pipeline as needed.

That's it! You've created a simple Declarative Pipeline in Jenkins to automate your CI/CD workflow.

In conclusion, Jenkins pipelines, whether Declarative or Scripted, empower DevOps engineers to automate and streamline software delivery. Declarative pipelines, with their simplicity and readability, are an excellent choice for most projects. So, go ahead, dive into Jenkins, and start building efficient pipelines today! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

ย