CI/CD Pipeline Using Jenkins, Git and Maven
Tags: docker, kubernetes, devops, git
Published: 2020-06-24 12:26:48

Development and release management of a software/application can become a complicated process, especially when deployment infrastructure grows. CI and CD process allows developers to integrate the development work and helps to catch bugs/errors as early as possible.

In this guide, we will deploy an application using Jenkins, Maven, and Git. Here the assumption is you already have Jenkins properly set up, and Maven project is on a VCS repository like GitHub or GitLab.

In case, if you haven’t set up Jenkins yet, then you can run the below-mentioned docker-compose.yml file on your Docker enabled system.

```
version: "3.2"
 
services:
   jenkins:
       image: jenkinsci/jenkins:lts
       container_name: jenkins-container
       restart: always
       ports:
           - "8080:8080"
           - '50000:50000'
       volumes:
       - '/var/run/docker.sock:/var/run/docker.sock'
       - '/data/jenkins/jenkins_home:/var/jenkins_home'
       environment:
       - "JENKINS_OPTS=--prefix=/jenkins"
```

##Step 1: Run Your First Jenkins Job

Once you are through with the installation and configuration of Jenkins, now let us create your first Jenkins Job. Login into Jenkins dashboard, find *New Item* as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zmi1dg1qbap25eswj5v4.png)

In the next page, enter the item name **My_First_Jenkins_Job**, select *Freestyle project* and click on *OK*, as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hh9jq16ex7jzkxc1ouel.png)

Under `Jenkins > My_First_Jenkins_Job`, enter a short description into the text field for future reference. As of now, we are going to leave the *Source Code Management* field to *None*. Next, go to *Build* and select *Execute Shell* from the drop-down menu, as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9g1ckmnqq3kgwvj9vm1p.png)

Next, let us try to run simple Shell commands:

```
touch test-file.txt
echo "Hello World" > test-file.txt
cat test-file.txt
```

Enter the above-mentioned commands in the text box, and save the configuration, as shown in the figure:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lc39c4cwvtkni4z5xiqc.png)

It’s time to build your project **My_First_Jenkins_Job**. On the left-hand side menu, click the *Build Now* option as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i3pvha2a19vno5q9hcka.png)

To see the console output of your first build, follow the screenshot below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uqe4r0gcez80ih2evxh1.png)

On the next page, you will see the following output:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7zwtx4mhiu8tn3hddtgx.png)

You can see the executed command output, and in the last line, `Success` indicates that our job finished successfully. This is how you run a Jenkins job. Next, we are going to configure Maven.

##Install and Configure Maven in Jenkins

In this step, we are going to install and configure Maven in Jenkins. For this, we have to download the Maven binary from the [official website](https://maven.apache.org/download.cgi). At the time of writing, the Apache Maven version is 3.6.3.

The guide is assuming that you are running Jenkins through Docker, so to install Maven binary, you need to follow the below-mentioned steps:

```
$ docker exec -it jenkins-container /bin/bash
 
$ cd /var/jenkins_home
 
$ wget http://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
 
$ tar -xvzf apache-maven-3.6.3-bin.tar.gz && cd apache-maven-3.6.3
```

Now as you have downloaded the required binary, copy the path for later use:

```
$ pwd
/var/jenkins_home/apache-maven-3.6.3
```

**Install Maven Plugins in Jenkins**

To install Maven plugins. Go to *Dashboard > Manage Jenkins > Manage Plugins > Available* and search for *Maven*, as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o3xzs37ezz1owg7kan9z.png)

On the next page, along with *Maven Integration* and *Maven Invoker*, you will see some additional dependencies getting installed, which are required for both plugins to work.

Now, let’s set the Maven path which you copied from the Jenkins's Container. Go to *Dashboard > Manage Jenkins > Global Tool Configuration* and find *Maven* to set the extracted binary home directory path, which is, `/var/jenkins_home/apache-maven-3.6.3`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d72dmpw4oppp6pl9ikdr.png)

Save the setting, following which your Maven application is all set to go. In the next step, we will use Maven to build a job.

##Creating a Job to Build with Maven:

It’s time to create a new Maven Project in Jenkins, and to do so, go to the dashboard and click on *New Item*. On the next page, enter your job name and select *Maven Project* and confirm it by clicking *OK*, as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sh5qcthzqwvisa8u27az.png)

In the configuration page, find *Source Code Management*, under this section, select *Git*, and enter your Repository URL. For this guide, we are using Public Repo; hence we don’t need to add Access Token or any other authentication steps.

In the *Branch Specifier* field, you can change the branch name as per your requirement. Our source code is on the *Master* branch; hence we are leaving it as default.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a5drlffowcgc4ixnl1hk.png)

In the *Build Triggers* section, you can set the build frequency for a specific time interval by selecting the *Build periodically* option. 

Let's say you want to build your Maven project every 30 minutes, then in the text field, add `H/30 * * * *` (i.e., every 30 minutes, every hour, every day, every month, and every day of the week)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5jvaue40eyyhgt7krht4.png)

Now find the *Build* section. In the *Root POM* field, you have to provide the path to `pom.xml` (you are free to provide a relative or absolute path to your pom.xml depending upon your project requirement), as it is a required file to build a Maven Project. 

Apart from this, in the same section, you have to define *Goal and Option*, there are various goals and options to build with maven. For now, we are only going to use three goals: *clean, install, package*; as shown in the figure below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wxszaoztjqrmzjfqt649.png)

Once you save the configuration, navigate to your Maven Project **My_First_Maven_Job** and click on the *Build Now* option as done previously. You might see the following output in your Jenkins’s console:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aefierj9j8txrvsv75u4.png)

As you can see, your build finished with a `SUCCESS` message. In case, if your build had some error, you can refer the console output and provide the logs to your development team.

If you navigate back to the project page, then you will find a folder named `Workspace`. In this directory, you will find the outcomes of your build as well as your war file, as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/88vh2h2742ni88njg1zr.png)