Continuous Integration

Jenkins


Before we start learning Jenkins, we first need to understand the software development process and what continuous integration is. We also need to know the limitations that were present before continuous integration. Then, we will see - how CI has improved the over all scenario?

So, let's dive into it.

Earlier, before continuous integration, developers used to push their code in a repository like SVN, git etc. Once the development or enhancement has been done, the code was compiled and integrated followed by the build. ANT, Maven are the software that were used earlier to perform this build and integration job. Once the integration is complete, the code gets pushed into a test environment and the bugs were notified to the developers. The developers then start debugging the application and push the code again to repository which gets build and installed on test afterwards following the methods mentioned in the image on the right hand side. After successful build, the code eventually get pushed to PRODUCTION system.

Now, let's find the fault in the process described above.

  1. Developers had to wait long time to get their test result. Because, the code first get collected in repository and then once all done, they get build.
  2. In case of any bug, developers had to read the entire source code to find the bug and to fix it. It was very time consuming.
  3. The software development process eventually gets very slow.
  4. Continuous feedback to developers during build was absent.
Now, let's see how the above issues get mitigated using continuous integration?

In continuous integration, the process schema looks something like the image mentioned below:



So, the advantage in the above schematic diagram is: It reduces time of development with continuous feedback. Eventually, it also increase the number of releases. The developers don't need to check the entire code rather they just need to check the code they committed and fix the bugs if any.

What is Jenkins?

Jenkins is one of the useful tool in DevOps. It is an open source software that can automate all type of tasks related to building, testing and deployment. 

How to install Jenkins?

Based on the Operating System (OS) you use, your Jenkins package will differ. You can download your desired Jenkins package from it’s official website: https://jenkins.io/download/

Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed.

Please refer their official guide - https://jenkins.io/doc/book/installing/ for installing Jenkins. 

Case Study

Now, I will take you through a case study that we faced while developing application for one of our reputed client. Initially, our Software Development team used to build the code during night. They used to call it as nightly built. Now, the builds were getting done during night and the developers used to fix them during the next day. They had to check all the commit that were made though out the day. It is indeed time consuming and does not make sense in term of software development. Both developers and project manager got stressed due to this. To avoid such issue, Jenkins were implemented on top of git which pulls the code and build it on every commit. Developers get their feedback quickly soon they commit and take necessary action. This reduces time and improves the delivery by at least 70% in term of time and quality.

So, lets' see how to configure pipeline and integrate Jenkins with git?

Configure pipeline and integrate Jenkins with git:

Steps to configure pipeline:

Pipeline can be broadly classified into two types:

  • Simple pipeline
Lets first see how can we create a simple pipeline. To create the simple pipeline, you need to follow the below steps:

    • Download and install pipeline plugin (if you don't install it during Jenkins installation)
    • Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK.

    • In script text area of the configuration screen, enter your pipeline syntax. 
    • Check the option "Use Groovy Sandbox"

    • Click Save
    • Click on "Build now" to create the pipeline.
    • Go to "Build History" section and click on "Drop-Down" menu. Select "Console Output" to see the output. Sample output will look like below:


  • Complex pipeline a.k.a Multi-branch pipeline
Multi-branch Pipeline project type enables you to configure different jobs for different branches of the same project. In multi branch pipeline, Jenkins will discover by it's own via Jenkinsfile.

In order to create Multi-branch pipeline, you need to follow below steps:


    • Click New Item on Jenkins home page.
    • Put a name of your pipeline and click "Multibranch Pipeline"
    • Provide "Display name" and add a "branch source".
    • Fill the repository URL and credentials based on the branch source you select.
    • Save the Multi-branch Pipeline project.
When you click on "Save", Jenkins detect your repository and creates appropriate items for each branch in the repository which contains a Jenkinsfile.

Sample Jenkinsfile looks like below:


Jenkinsfile (Declarative Pipeline)

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

 
Below are the steps to integrate Jenkins with gitlab:

Integration between Gitlab and Jenkins has two distinct phases.


    • Gitlab end configuration
Step 1: First, you need to create a build user and corresponding access token in gitlab. To do so, go to Profile Settings -> Access Tokens. Create a user of your choice and generate an access token. Save the token in a safe place.
Step 2: Secondly, you need to create webhooks at gitlab. go to Project-> Integrations -> Jenkins CI. Enter the Jenkins project URL in ‘URL’ field, select the trigger events from the list and save. A new web-hook will be added accordingly.
    • Jenkins end configuration
Step 1: Add credentials of gitlab in Jenkins.
In the Jenkins’s System credentials add a new credential of the type GitLab API token. Paste the API token copied earlier in the ‘API Token’ field and save them.
Step 2: To configure Jenkins server, you need to make sure that Jenkins-git plugin is installed. If the plugin is installed, then go to Manage Jenkins -> Configure System and move to the ‘GitLab’ section. Enter the GitLab server URL in the ‘GitLab host URL’ field and select the added Gitlab credentials from the ‘credentials’ drop-drown.
Step 3: Now, you need to configure Jenkins Project. To do so, you either need to create a job or configure an existing job.

After you are done with project configuration on Jenkins, you are all set. If you push a commit to your git repository, you will find the Jenkins job start running.

Other powerful tools for continuous integration: 

Bamboo (a product of Atlassian), Git CI/CD

Future scope:

In the next articles/Posts, we will see more topics on Jenkins integration. Not only on application side but also on Infrastructure side. 

What have we learned from this article?

  • What is continuous integration? 
  • What is the need for continuous integration?
  • What is Jenkins? 
  • How to install Jenkins?
  • How to configure pipeline in Jenkins?
  • How to integrate Jenkins with gitlab?
  • What are the other tools which can also be used for continuous integration?

Please share your comments about this article if you find this useful or not. For the experienced DevOps Engineers, please feel free to share your experience about dealing with Jenkins.

Comments