CD-Implementation-with-Jenkins

In DevOps, CI/CD (Continuous Integration / Continuous Delivery) is a process used by software development companies to frequently update and deploy any app by automating the integration and delivery process. There are various tools available by which one can achieve this objective. 

Jenkins has built-in functionality for CI CD process implementation. Using Jenkins, CI CD pipeline alongside your assignment will substantially accelerate your software development process.

You’re at the ideal place if you want to learn more about the Jenkins CI CD pipeline. Continue on, and in this article, you’re going to discover how to build up the CI/CD pipeline using Jenkins.

1. What is Continuous Integration (CI)/Continuous Delivery (CD)?

“Continuous Integration” refers to the process of integrating the new code into an existing repository in real time. As a preventative measure, it employs automated checks to spot issues before they become major. While CI can’t guarantee bug-free code, it can speed up the process of locating and fixing problems.

With the “Continuous Delivery” process, modifications to the code are performed before deployment. During this stage, the team determines what will be released to consumers and when. Deployments are the crucial stage in the entire process.

When these two methods are combined, the resulting process is called continuous integration/continuous delivery (CI/CD) since all the stages are automated. With CI/CD in place, the team can deliver code more rapidly and reliably. With this procedure, the team becomes more nimble, productive, and self-assured.

The most popular DevOps tool for CI/CD pipelines is Jenkins. Therefore, let’s take a look at the creation part of CI/CD pipelines using Jenkins. 

2. What is Jenkins?

Jenkins is a free and open-source automation server. The tool streamlines the process of incorporating code modifications into the development. Jenkins performs Continuous Integration with the aid of plugins.

Jenkins’s adaptability, approachability, plugin-capability, and user-friendliness make it the ideal tool for constructing a CI/CD pipeline.

Here are some of the advantages of Jenkins:

  • Jenkins can be utilized as a simple continuous integration server or CD (Continuous delivery) hub for any project. 
  • Jenkins can be useful with any operating system like Windows, Linux, MacOS, etc. 
  • It also has an intuitive online interface for installation and configuration, along with real-time error checking and contextual guidance. Hence, provides a seamless set up.
  • Jenkins is compatible with nearly every tool or plugins used in the CI/CD.
  • Jenkins can also be extended by using various plugins. 
  • Tasks can be easily distributed among various machines using Jenkins. So, the development, testing and deployment process can become faster across multiple machines. 

2.1 Why use Jenkins for CI/CD?

Using Jenkins for CI/CD can be very beneficial. Here are the top three advantages:

1. Costs

Jenkins is available on an open-source platform, giving it an edge over other tools. And because it is free, there won’t be any procurement costs. Additionally, developers can optimize the infrastructure and cloud costs by implementing the IAC approach and efficiently using the available resources with Jenkins.

2. Plugins

Another valuable trait of Jenkins is the variety of plugins it offers. This enables the cloud service users to use CI processes in far less time. On top of that, Jenkins comes with a set of common tools and services. Interestingly, you can use them on both cloud and on-premise. Some of the top plugins available in Jenkins are Build Pipeline Plugins, Test Analysis Plugins, and Dashboard View.

3. Faster integration and deployment 

Builds and tests on each commit provide a fast-paced environment that helps resolve bugs. This allows you to deliver new features and releases quickly. Before, code integration and debugging were manual and complex. Only after getting through multiple commits were they able to build a functional version. But, with the use of Jenkins, integration has become possible after every commit. It ensures that the program functionality is operational and available.

3. Building CI/CD Pipeline with Jenkins

3.1 Prerequisites

In order to proceed the instructions and scenarios in this article, you will need the following:

  1. Git SCM. The most up-to-date 64-bit Windows version of Git (2.40.0) is used in this article.
  2. A repository on Github that can be integrated with the Jenkins CI CD system.
  3. Jenkins. 

Now, let’s first create a new github repository that we will use to build CI/CD pipelines on Jenkins. 

3.1.1 Preparing the GitHub Repository

1. Start up your browser and sign in to your GitHub profile and go to the repositories section.

GitHub Repository

2. Now, you may create a new repository by filling in the various  information such as Repository name, description, etc.  Some fields are optional so you can skip if you wish. You can also choose who can see your repository on the internet by turning the profile public or private. Once you are done filling the details, click on “Create Repository”.

Create Repository

3. Next, add a new document to the repository titled “Jenkins”. The Jenkins file is a text file that includes the description of a Jenkins Pipeline. This document belongs in the repository where you create the code.

Under your repository tab, select Add file —> Create new file.

Quick Setup

4. Now, copy the script below and paste it into the Jenkins file.

A pipeline is a series of commands expressed in programming for continuous delivery.

The stage block comprises a number of stages in a pipeline, showing the Jenkins pipeline flow.

A step is a single job that performs a certain procedure at a predetermined time. A pipeline comprises a succession of stages.

pipeline {
    agent any
 
    stages {
        stage('Build') {
            steps {
                echo 'Building Example'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing Example'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying Example'
            }
        }
    }
}

After creating the code, the file will appear much like the snapshot below.

Jenkins Pipeline Code

5. Go to the end of the page and tap on the button “Commit new file”.

Commit new file

6. Lastly, copy the GitHub Repository URL. In order to do so, select the Code button and tap on the Copy icon right hand side of the given link. You will require the resultant URL for the Jenkins CI CD pipeline construction.

Resultant URL

3.2 Creating a Jenkins CI/CD Pipeline

Several modifications and additions to the code are necessary when creating any new feature. There are various tasks such as changes in commitment, compilation, testing, packaging, etc. to be done in the intervals when the code is updated. 

Jenkins and similar automation platforms make these procedures quick and foolproof. In this section we will learn to create  CI/CD pipelines through Jenkins.

Step:1 Install Jenkins

First step is the Jenkins Installation. If you already have that in your pc, you can move on to the next step. 

Step:2 Creating a Jenkins CI-CD Pipeline

1. Start a web browser, enter your Jenkins URL (http://localhost:8080/) into the address bar, and sign in. Use “admin” as a username. You will get the password from Drive C > ProgramData > Jenkins > .jenkins > secrets > initialAdminPassword file.

Login to Jenkins

2. Create a new Jenkins Job: Select the New Item icon on the left side of the Jenkins Dashboard.

New Jenkins Job

3. Type the title of the new pipeline. Choose the “Pipeline” template and give it the label as “pipelines-demo” for this article. The final step is to click OK. Here, 

“Pipeline” enables any developer to define the entire application lifecycle. 

Choose Template

Here, other available options are Freestyle Project and Multi-configuration project. 

  • Jenkins Freestyle project allows developers to automate jobs like testing, packaging apps, creating apps, producing reports, etc. 
  • With a Multi-configuration project, one can create a single job with many configurations. 

4. On the configuration screen, select the General tab, and then select the GitHub project box. The Project URL should then be updated to include the URL you obtained from the repository.

GitHub Project URL

5. Select the “GitHub hook trigger for GITScm polling” in the Build Triggers section, as seen in the picture below.

Build Triggers
Configure a Pipeline Job with SCMScript

6. If you are specifically configuring with the SCMScript, go to the Pipeline section and make the following selections/specifications there:

  • In Definition box, select: Pipeline script from SCM
  • SCM box, select: Git
  • Repository URL box, enter: Your git repository URL
  • In Branches to Build section, write “*/*” in the branch specifier field. 
Pipeline Job with SCMScript

7. After that, Write the file name “Jenkins” in the Script path field. Now, you complete the configurations. Click on the “Save” button. 

Configure
Configure a Pipeline Job through a Direct Script

6.  If you directly want to configure the pipelines without git, you can write the pipelines creation script by selecting the “Pipeline script” option in the “Definition” field. Checkout the below image for the reference.

After writing the script here, click on the “Save” button in the end. 

Pipeline Job through a Direct Script

3.3 Configuring a Webhook in GitHub

Jenkins project requires a webhook to be set up in the GitHub repository before you execute a new job. In the event of a push to the repository, Jenkins server will be notified via this webhook.

Establish a Webhook by following the below steps: 

1. Open your GitHub repository’s “Settings” menu, then select the “Webhooks” tab. Choose “Add webhook” on the “Webhooks” page.

Settings
Add Webhooks

2. Then, add the Payload URL field to your Jenkins URL(/github-webhook/). For instance, HTTP://addJenkinsURL/github-webhook/.

Alter the property of Content type to “application/json” as well.

Add the Payload URL

3. On “Which events would you like to trigger this webhook” selection, click the “Let me select individual events” option.

Events to Trigger the Webhook

4. Click the “Pull request reviews”, “Pushes” and “Pull requests” boxes at the bottom of the page. When these conditions are met, GitHub will trigger the sending of a payload to Jenkins.

Trigger the sending of a payload to Jenkins

5. Add the webhook by pressing the button at the end of the page to validate the webhook.  If everything checked out with the webhook successfully, a message like the one in the snapshot would appear at the very top of the page.

Checked out with the webhook

3.4 Executing the Jenkins CI/CD Pipeline Job

How would you verify the functionality of the pipeline after you’ve set it up? You may see the current state of your pipeline with the use of Jenkins’ Pipeline Stage View add-on.

1. Click on the  “Build Now” option to begin the build procedure and create the initial build data. 

Build Now
Create Initial Build Data

2. Put the pipeline through its paces by adding a dummy file to the repository. Simply navigate back to your GitHub repository and select Add File —> Create new file to start.

Create a new file with the name “demofile” and fill it with any demo text.

Demo file

After you complete, select “Commit new file” in the page’s footer.

3. If you return to the status page for your Jenkins pipeline, you will find a new build entry with a single commit, as seen below. You will get this result in the case when you choose to Configure pipeline jobs with SCMScript.

Stage View SCMScript

4. If you are Configuring pipeline jobs through a direct script, below is the kind of result you’ll get when you click on the “Build now” option. Once you add or update any changes in GitHub, it will appear here. 

Stage View Direct Script

4. Conclusion

In this article, we gained an understanding of how to implement a Jenkins CI CD pipeline for the purpose of automating the software development lifecycle. As an added bonus, you now know how to utilize Jenkins to keep a CI/CD action chain in a software project running well.

Moreover, you may find a wealth of additional information on the Internet that can aid in your comprehension of pipelines. So, how likely are you to implement the Jenkins CI CD pipeline in your projects? Let us know in the comments!

FAQs

What is the role of Jenkins in CI/CD?

Jenkins is an open-source automation software DevOps tool that is used to execute the CI/CD workflows called pipelines. 

What is the difference between CI and CD? 

In the process of code development and deployment, CI is the first phase which focuses on preparing the code for build/test or release. Meanwhile, CD is the second phase which includes the actual release/deploy or deployment of the code. 

Why is CI CD pipeline used?

The CI/CD pipeline is used to automate the entire software delivery process. It builds a codebase, runs tests, and deploys the new version of the app. CI/CD pipelines allow quick iterations, eliminate manual errors, and offer a standardized feedback loop to developers. 

Is Jenkins a free tool?

Yes, Jenkins is a free and open-source automation software DevOps tool. So, developers don’t have to deal with any additional procurement costs to use Jenkins. 

profile-image
Mohit Savaliya

Mohit Savaliya is looking after operations at sysgenpro, and because of his technical background, he also has an understanding of Microservices architecture. He promotes his technical expertise through his bylines. He collaborates with development teams and brings out the best and trending topics in Cloud and DevOps.

Related Service

Know more about Cloud and DevOps services

Learn More

Want to Hire Skilled Developers?


    Comments

    • Leave a message...

      1. Samir Patil

        Continuous Integration / Continuous Delivery process is generally used for regular updates and deployment of any applications by automating integration and deployment. I found building CI/CD pipelines with Jenkins a bit tough. But this resource cleared all my doubts. Thank you.