Creating Efficient Jenkins Pipelines with Declarative Syntax

Creating Efficient Jenkins Pipelines with Declarative Syntax

What is Continuous Delivery Pipelines? How does it work?

In a Jenkins pipeline, every job or event has some sort of dependency on at least one or more events.The picture above represents a continuous delivery pipeline in Jenkins. It contains a group of states called build, deploy, test and release. These events are interlinked with each other. Every state has its events, which work in a sequence called a continuous delivery pipeline.

A continuous delivery pipeline is an automated expression to display your process for getting software for version control. Thus, every change made in your software goes through a number of complex processes on its way to being released. It also involves developing the software in a reliable and repeatable manner, and progression of the built software through multiple stages of testing and deployment.

What is a JenkinsFile?

Jenkins pipelines can be defined using a text file called JenkinsFile. You can implement the pipeline as code using JenkinsFile, and this can be defined by using a domain-specific language (DSL). With JenkinsFile, you can write the steps needed for running a Jenkins pipeline.

The benefits of using JenkinsFile are:

  • You can create pipelines automatically for all branches and execute pull requests with just one JenkinsFile.

  • You can review your Jenkins code on the pipeline

  • You can audit your Jenkins pipeline

  • This is the singular source for your pipeline and can be modified by multiple users.

JenkinsFile can be defined by either Web UI or with a Jenkins File.

Declarative versus Scripted pipeline syntax:

There are two types of Jenkins pipeline syntax used for defining your JenkinsFile.

  1. Declarative

  2. Scripted

Declarative:

Declarative pipeline syntax offers an easy way to create pipelines. It contains a predefined hierarchy to create Jenkins pipelines. It gives you the ability to control all aspects of a pipeline execution in a simple, straightforward manner.

Scripted:

Scripted Jenkins pipeline runs on the Jenkins master with the help of a lightweight executor. It uses very few resources to translate the pipeline into atomic commands. Both declarative and scripted syntax are different from each other and are defined totally differently.

Why Use Jenkin’s Pipeline?

Jenkins is an open continuous integration server that has the ability to support the automation of software development processes. You can create multiple automation jobs with the help of use cases, and run them as a Jenkins pipeline.

Here are the reasons why you use should use the Jenkins pipeline:

  • Jenkins pipeline is implemented as a code that allows multiple users to edit and execute the pipeline process.

  • Pipelines are robust. So if your server undergoes an unforeseen restart, the pipeline will be automatically resumed.

  • You can pause the pipeline process and make it wait to resume until there is input from the user.

  • Jenkins Pipelines support big projects. You can run multiple jobs, and even use pipelines in a loop.

Jenkins Pipeline Concepts

Term

Description

Pipeline

The pipeline is a set of instructions given in the form of code for continuous delivery and consists of instructions needed for the entire build process. With a pipeline, you can build, test, and deliver the application.

Node

The machine on which Jenkins runs is called a node. A node block is mainly used in scripted pipeline syntax.

Stage

A stage block contains a series of steps in a pipeline. That is, the build, test, and deploy processes all come together in a stage. Generally, a stage block is used to visualize the Jenkins pipeline process.

Step

A step is nothing but a single task that executes a specific process at a defined time. A pipeline involves a series of steps.

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).

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.