CI/CD Pipeline Using Jenkins, Git and Maven
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:
In the next page, enter the item name My_First_Jenkins_Job, select Freestyle project and click on OK, as shown below:
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:
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:
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:
To see the console output of your first build, follow the screenshot below:
On the next page, you will see the following output:
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. 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:
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,
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:
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.
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)
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:
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:
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:
We hope the above guide helps you in effective deployment of CI/CD pipeline along with deploying an application using Jenkins, Maven, and Git. Let us know how you did it and how it helped your application development.