You are on page 1of 13

30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

CI/CD with Git, Jenkins and Maven


Updated: Sep 16, 2022

In this tutorial, we will see how to use Jenkins, Git and maven to set up a CI/CD pipeline. We will use
the spring boot project from this gitlab link. If you wish you can use a maven project of your choice ;)

Prerequisite

JDK 11 or above

Maven

A linux server

Step 1 : Install a Jenkins on a linux server (centos)


in our tutorial we will install Jenkins on a centos machine.

Go to this link and install Jenkins on your centos

After following the jenkins installation procedure, you can check if the service is installed
correctly.

as you see , the service is installed but inactive , so we need to start it with :

systemctl start jenkins

// and check the status

systemctl status jenkins

now the service is started , now on your browser type http://<your-host>:8080 , the first connection
show you this :

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 1/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

in order to have your password open the file /var/lib/jenkins/secrets/initialAdminPassword and get the
password .
you will be asked to install the most used plugins. in our tutorial we put yes.

for the rest we have skipped all the other requests, and we obtain the page below:

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 2/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

now Jenkins is installed ;)

Step 2 : Integrate git and gitlab plugin with Jenkins


In this step we're going to install maven plugin and ensure gitlab plugin are installed. if it not case for
gitlab we'll install it . but first we have to install Git on the server

Install git on the instance with :

yum install git

check if git is installed with :

git version

Install Gitlab plugins

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 3/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

From Jenkins Dashboard Manage Jenkins -> Manage Plugins. On plugin manager page , on
Available option search gitlab , choose it and click on Install without restart

ensure on installed option gitlab plugin was installed as below:

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 4/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

Step 3 : Integrate maven and maven plugin with Jenkins

Install and setup Maven on the server

Go to maven download page https://maven.apache.org/download.cgi and get one binary link and with
wget tool download the binary on /opt folder of the host :

extract the tar.gz file in opt folder

tar -xvzf apache-maven-3.8.6-bin.tar.gz

mv apache-maven-3.8.6 maven

edit the file .bash_profile , in order to declare JAVA_HOME, M2_HOME, M2 and update $PATH as
below:

vi ~/.bash_profile

and add this if not exist

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 5/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

M2_HOME=/opt/maven
M2=/opt/maven/bin
JAVA_HOME=/usr/lib/jvm/<YOUR JAVA>
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2

after updated your bash profile , run this commande

source ~/.bash_profile

check maven and java version by typing mvn -version and java -version

now install maven plugin on Jenkins

From Jenkins Dashboard Manage Jenkins -> Manage Plugins. On plugin manager page , on
available option search maven , choose it and click on Install without restart

Setup java path and maven on Jenkins GUI as


https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 6/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

On global Tool Configuration complete JDK section and maven section as below:

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 7/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

and click to save button ;)

Step 4 : Create a Jenkins Job in order to build our Spring project.

From Jenkins Dashboard click on "New Item"

Set the build name , choice maven project and save

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 8/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

After save

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 9/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

after saving the job will be created as below :

And now click to Build Now

if everything ok we have :

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 10/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

by clicking on #1 you will see the information about the build

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 11/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

on console output :

You will be able to see where the build jar artefact is :

The jar is installed on your host in the folder : /var/lib/jenkins/workspace/testbuild/target/ , here


spring-dockerize-app-0.0.1-SNAPSHOT.jar

Step 5 : Test application

Now go to the folder /var/lib/jenkins/workspace/testbuild/target/ and run the command :


https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 12/13
30/05/2023, 21:59 CI/CD with Git, Jenkins and Maven

java -jar spring-dockerize-app-0.0.1-SNAPSHOT.jar --


server.port=9999

We use port 9999 , because Jenkins use the port 8080 on the . Our application is normaly configured
to use port 8080 , but in this test case we forced the port to 9999.
If your application uses another port other than 8080, you don't need to use the server.port option
when starting.

The ouput log will be:

on browser type http://<your host>:9999/ping , you have :

this confirme the build is on successful ;)

Step 5 : Conclusion
With this tutorial, we have seen how to easily install Jenkins on Centos and do the necessary to be
able to build a spring boot project in simple CI/CD mode.

https://www.thetechmonitor.com/post/ci-cd-with-git-jenkins-and-maven 13/13

You might also like