Jenkins Pipeline

The Jenkins Pipeline

The Jenkins pipeline is a collection of codes written in a Jenkins file, allows the Jenkins to operate in a sequence. It provides a platform to run each job simultaneously. The pipeline enables the workflow of the jobs by using the pipeline plug-in.

Features of Jenkins Pipeline:

  1. Pipeline as code: Instead of writing each job separately, you can write the code in a single file known as The Jenkins file.
  2.  Code can be checked into VCS: The The Jenkins file is stored in the version control system (VCS), is locally available; the developer can also access it very easily.
  3. Allows conditional loops: The Jenkins pipeline allows you to use the conditional loops for and when loop.
  4. Integrate with other plug-ins: The Jenkins pipeline permits you to make a connection with multiple plug-ins like Git, Docker, etc.
  5. Run jobs in parallel: If you have many jobs, you can group them into 2 or 3 groups as per your requirements and run in parallel. It will save you time in deploying the project.
  6. Restart from the saved checkpoint: It doesn’t require you to start from the beginning. In fact, it provides you a feature by which you can save your work and make a checkpoint. When you come back after the shutdown, you start from where you left (saved checkpoint).
  7. Incorporate use input: Developers can easily interact with the pipeline during the execution process and can modify the codes.

Limitations of Pipeline plug-in

  1. Normally, the pipeline plug-in works well for a small pipeline, but it becomes very difficult to implement Complex pipelines (having a lot of jobs) using this plug-in.
  2. It takes lots of resources.
  3. It takes a lot of time for a complex pipeline.
  4. It makes it boring to do such an extensive job.

Installing process of Pipeline plug-in

The installing process of pipeline plug-in is the same as that of other Jenkins plug-ins. Open The Jenkins in your web browser and go to the Manage The Jenkins page. Search "pipeline” plug-in in the available section and choose the option "install without restart" or "Download now and install after restart." It will auto-install all related plug-ins related to it.

Jenkins Pipeline

Why use Pipeline?

The Jenkins tool is used to deploy the product in the market by using continuous integration (CI) and continuous deployment (CD) features. The Jenkins pipeline is used to continuously deploy the product continuously, and it also helps the developer to modify the code during the execution process.

In the pipeline process, the software is developed in a scalable, trustable and repeatable way.

  1. It is easy to handle hundreds or thousands of jobs with the pipeline process.
  2. In the pipeline process, the maintenance is very less and very inexpensive.
  3. Keep the team in sync.

Required attributes for Jenkins Pipeline

There are three main attributes of The Jenkins pipeline i.e.

  1. Pipeline: The pipeline declaration must be written on the top level of the program. The pipeline syntax will start from here.
  2. Agent: It is available just below the pipeline. It (agent any) tells that the current build is going to execute on any of the Jenkins agents. An agent can be a node, or it could be an executer on that node.
    • Any
    • None
    • Label
    • Docker
  3. Stages: It is the main field, or you can say it is the program's body. All the functions happen here. Inside the stages, we further define several different stages with the name, which consist of steps or statements or scripts written regarding jobs. Usually, there are three-stage, which are as follows:
    • Build
    • Test
    • Deploy

However, there are some other stages that can also be included:

  • Check out
  • Clean up, etc.

The syntax using these three attributes is as follows:

pipeline {
             agent any
             stages {
                         stage (“build”) {
                                     Steps {
                                                 echo ‘building the application..’
                                     }
                         }
             stage (“test”) {
                                     Steps {
                                                 echo ‘testing the application..’
                                     }
                         }
             stage (“deploy”) {
                                     Steps {
                                                 echo ‘deploying the application..’
                                     }
                         }
             }
 } 

Types of Jenkins Pipeline

The Jenkins pipeline can be written based on two syntaxes:

  1. Declarative pipeline syntax: It is a new feature, or you can say it is a recent addition to the Jenkins pipeline. It supports the coding and makes it easy to read and write the pipeline code. These codes are written in the Jenkins file. It makes it easy to write the code in it because of its predefined structure.

The syntax is:

pipeline {
             agent any
             stages {
                         stage (“build”) {
                                     Steps {
                                                 // statements;
                                     }
                         }
             }
 } 
  • Scripted pipeline syntax: It is a traditional way in which the Jenkins file is written on The Jenkins UI instance. It was the first syntax written for the pipeline. It allows you to use the whole The Jenkins file by using the groovy script. There is no predefined structure for it to write the code; its basic syntax is:

Syntax:

node {
             // groovy script;
             stage (build) {
                         }
             stage (test) {
                         }
             Stage (deploy) {
                         }
 } 

You can write the code in a scripted pipeline in a flexible way. It has advanced scripting capability with higher flexibility, but it isn't easy to start writing a code in a scripting pipeline.

Difference between scripted and declarative Pipeline

  Scripted Pipeline  Declarative Pipeline
It is the traditional way of writing code.It is an advanced feature to write the code.
It follows strict groovy syntax.It follows the simple groovy syntax.
The code is written in the node block.The code is written in the pipeline block.
The code is written on The Jenkins UI instance.The code is written locally.
There is no pre define structure to write the code.There is a well-defined structure to write the code.
More flexible with less performanceLess flexible with higher performance
It isn't easy to start coding in it.It is easy to start coding in it.

Pre-requirements to execute Jenkins Pipeline

  1. The Jenkins must be installed on your system or on any virtual machine so that you can use it.
  2. JDK-8 must be installed on your system.
  3. If you want to execute the declarative pipeline, then it is a must to install the GitLab on your system.
  4. Basic knowledge of The Jenkins.

How to create a Jenkins Pipeline

It is easy to create the Jenkins pipeline in the Jenkins tool. You can create both scripted and declarative pipelines easily by clicking on the "new item" option available in The Jenkins dashboard. It requires login details, GitHub account details and coding skills for creating a pipeline. It will allow you to choose between the scripted and declarative pipeline process when you start creating a pipeline.

How to create a Jenkins pipeline

Run your pipeline project

It is also very easy to run a pipeline project in Jenkins. This process will also be done from the Jenkins dashboard. You can run your pipeline by clicking on it, as it is available on your Jenkins dashboard. When you click on your job, you will get the "build now" option. The "build now" option will execute your pipeline, and it will also provide you the run status of your pipeline job. When your job is run successfully, it will come with a green shade in the dashboard, and if it fails, it will be in a red shade.

Run your pipeline project

Pipeline concepts

  • Pipeline: It is the main form where pipeline creation will start. All the instructions related to the software building, software testing and software deployment are given in it. It has the following syntax:
Pipeline {
}
  • Node: A node is a machine that executes the entire code written under it. It is used in the scripted pipeline process. The syntax is as follows:
Node {
 // statements;
 } 
  • Agent: It encompasses four main parameters, which are as follows:
  • Any: It runs the pipeline on any available server.
  • None: This parameter is used when there is no agent available for the whole system. Thus, it becomes very important for each stage to define its agent to execute the pipeline. This parameter is used at the root of the pipeline.
  • Label: This parameter tells that the pipeline will run on the labeled agent.
  • Docker: As you know, Docker is a container, so this parameter tells itself that it will use a Docker container to run the pipeline process or any specific stage.
  • Stages: It is a block in which all steps are available related to software creation. You can say this block is the main body of the pipeline.
  • Step: There are many steps available in the block stages, such that each step performs an individual task. Following are some steps name, which acts according to their name:
  • Check-in
  • Build
  • Test
  • Deploy
  • Monitor
  • QA
  • Clean up, etc.

The functionality of The Jenkins Pipeline

  • Durability: The Jenkins is robust as well as durable due to its survival nature in planned and unplanned restart situations.
  • Pause and resume: It provides a pause and resume feature in the process.
  • Versatile: It is versatile in nature because of its multi-tasking ability.
  • Efficient: Pipeline provides checkpoints to the developer to restart the program.
  • Extensible: The pipeline supports multiple plug-ins; it makes the pipeline extensible.

Benefits of The Jenkins pipeline

  • The Jenkins pipeline allows multiple user access in the execution process. Multiple developers can access, edit and update the process in it.
  • The Jenkins pipeline provides you the resume option. Unfortunately, if your system fails or shuts down during the process, then it allows you to resume automatically from where you left.
  • In The Jenkins pipeline, you can run multiple jobs in parallel.
  • The pipeline process gets automatically pauses while taking input from the end-user; it automatically resumes again after getting the input.
  • It nicely manages large projects.