Creating Efficient Jenkins Pipelines with Declarative Syntax

Creating Efficient Jenkins Pipelines with Declarative Syntax

Day 26: #90DaysOfDevOps Challange

Jenkins is an open-source automation server that enables continuous integration and continuous delivery (CI/CD) of software development projects. One of the most important parts of your DevOps and CI/CD journey is the Declarative Pipeline Syntax of Jenkins. In this tutorial, we will create a new Jenkins job using the Declarative Pipeline Syntax. This example will help us understand how to create a basic pipeline using the Declarative Pipeline Syntax.

What is a Pipeline in Jenkins?

A pipeline is a collection of steps or jobs interlinked in a sequence. It is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code. The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.

Why Use Declarative Pipeline Syntax?

Declarative is a more recent and advanced implementation of a pipeline as a code. It provides a more structured and opinionated way of defining pipelines. The Declarative Pipeline Syntax is designed to simplify the creation of pipelines and make them easier to read and maintain. It provides a more concise and readable syntax, making it easier for developers to understand and modify pipelines. Additionally, creating a Jenkinsfile and committing it to source control provides several immediate benefits, including automatically creating a Pipeline build process for all branches and pull requests, and code review/iteration on the Pipeline (along with the remaining source code).

Prerequisites

Before we get started, make sure you have the following:

Creating a Jenkins Job Using Declarative Pipeline Syntax

Follow these steps to create a new Jenkins job using the Declarative Pipeline Syntax:

Step 1 - Create a New Jenkins Job

  1. Log in to your Jenkins instance

  2. Click on the "New Item" link in the left-hand menu

  3. Enter a name for your new job and select "Pipeline" from the list of job types

  4. Click the "OK" button to create the job

Step 2 - Define Your Pipeline

Once you have created your new job, you will be taken to the job configuration page. On this page, you can define the description as follow:

Now, Add a Pipeline using the Declarative Pipeline Syntax.

Here is an example of a basic Pipeline:

pipeline {
    agent any
    stages {
        stage("Code") {
            steps {
                echo "Code Cloned"
            }
        }
         stage("Build") {
            steps {
                echo "Code Build"
            }
        }
         stage("Test") {
            steps {
                echo "Code Test"
            }
        }
         stage("Deploy") {
            steps {
                echo "Code Deploy"
            }
        }
    }

In this Pipeline, we have defined four stages - Code, Build, Test, and Deploy. Each stage has a single step that prints a message to the console. The agent directive specifies that the Pipeline can run on any available agent.

Step 3 - Save and Run Your Pipeline

Once you have defined your Pipeline, save the changes to your job configuration. Jenkins will automatically validate your Pipeline syntax and display any errors or warnings.

To run your Pipeline, click the "Build Now" button on the job page. Jenkins will start the build process and display the console output in real-time.

Conclusion

In this tutorial, we have created a new Jenkins job using the Declarative Pipeline Syntax. We have learned how to define a basic Pipeline using the Declarative Pipeline Syntax and run it in Jenkins. Using the Declarative Pipeline Syntax makes it easier to define and maintain pipelines and provides a more structured and opinionated way of creating them.

Overall, Jenkins is a powerful automation tool that can greatly enhance the efficiency of your development process. By using the Declarative Pipeline Syntax, you can create pipelines that are easy to understand, modify, and maintain. I hope this tutorial has helped understand the basics of creating a Jenkins job using the Declarative Pipeline Syntax.

πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰Happy automating!πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

Β