Jenkins - Installation, Job for Auto Build / Deployment of Maven Application

This tutorial covers the following contents :

1) Jenkins installation on Linux - Cent OS 7

2) Creating a Jenkins Job for build triggering of a Maven based Java application

3) Configure Auto Deployment onto Tomcat instance

4) Configure Job to trigger Auto Build and Deployment of application on Git Commit

Jenkins installation on Linux - Cent OS 7

1) Jenkins is a Java based application and requires Java to be installed. Install Java 8 by running the below command.

sudo yum install java-1.8.0-openjdk-devel

2) Add Jenkins repository using following command .

curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo

3) Add GPG key for Jenkins.

sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

4) Install Jenkins using Yum

sudo yum install jenkins

5) Start the Jenkins service now.

sudo systemctl start jenkins
Note : i ) You can verify the status using the command "systemctl status jenkins".
ii) You can turn on auto start of Jenkins service at system boot up using command "systemctl status jenkins" .

6) If you want to access Jenkins outside of installed host, add port 8080 used by Jenkins to public zone of Firewall.

sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp && sudo firewall-cmd --reload

Now , Jenkins installation is done. To be able to start using Jenkins, few steps are required for initial setup of Jenkins as shown in next step.

7) Initial Setup of Jenkins :

i) Access Jenkins using following URL and copy paste the generated password as shown below.

Jenkins URL : http://<hostname>:8080

Now , Jenkins page asks for some initial generated password which you can copy and paste by running below command.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

ii) On Continue, Jenkins will ask for Plugins installation . Here, you can click on Install suggested plugins to proceed .

iii) Once plugins installation is done, it will prompt for admin user creation for login. Fill the details and click on "Save and Continue".

iv) Next page shows Instance Configuration with auto populated URL, where you can simply click on "Save and Finish" without any changes.

v) Now , click on "Start using Jenkins" button in next page to see the Home page of Jenkins.

You are done with installation and initial setup of Jenkins now. You can start creating the Jenkins Job for CICD flow.

Creating a Jenkins Job for build triggering of a Maven based Java application

 

Tools configurations ( JDK, Maven and Git )

Configure JDK, Maven and Git tools required for fetching source code, maven application compilation and build.

1) JDK configuration : Navigate to Jenkins -> Manage Jenkins -> Global Tool Configuration. In JDK section, click on Add JDK and input name and Java Home as shown below.

jenkins_gtc_jdk

2) Maven configuration : In the same page as above, scroll down to find Maven section and Add Maven by giving name and checking "Install Automatically" checkbox. Then click on Save button to save configurations.

jenkins_gtc_maven

3) Git configuration : Ensure Git Plugin is installed which can be seen in Jenkins -> Manage Jenkins -> Manage Plugins -> Available. If not available, Install it. Also, make sure command "git" is running in Jenkins host machine which you can verify by running "git" command. If not installed, install it by running "yum install git".

 

Create Job for Build

1) In Jenkins home page, Click on New Item and create an item of type "Freestyle Project" as shown below.

jenkins_newitem

2) Click on OK and configure Source Code Management with Git repo as following.

Note : It is ok to use git repo shown in image for your exploration here. But , if you want to try "Auto Build and Deployment on Git Commit" , you need to Fork the Git Repo and use it. Because , "Auto Build and Deployment on Git Commit" steps include adding of a webhook in repo which you cannot do with below repo because of permissions.

jenkins_scm

3) Next configure Build stage with Maven target as shown below.

Jenkins-build-stage-maven

Save the configurations. Now Build job creation is done. To trigger Build , navigate to Jenkins -> SpringBootRestExample Job and click on Build Now. You will see that a new job started under Build History. Click on build number such as #1 and then Console Output to see the output of Job SUCCESS as shown below.

Jenkins-build-now-success

You can see that build file war generated and placed under target folder. Till now, you have generated war file and next we will see how to configure Auto Deployment onto Tomcat.

Configure Auto Deployment onto Tomcat instance

1) Install Tomcat : Download and extract Tomcat in some location in host which you want to use for Tomcat. It can be same host as Jenkins.

2) Modify few files and Start Tomcat :

i) Add Tomcat user with role manager-script in file conf/tomcat-users.xml .

Ex : <user name="admin" password="adminadmin" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" />

ii) Comment the following line in file webapps/manager/META-INF/context.xml .

<!––<Valve className="org.apache.catalina.valves.RemoteAddrV alve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> ––>

iii) Start Tomcat by running conf/catalina.sh using following command .

Command : bash conf/catalina.sh start

3) Install Jenkins Plugin "Deploy to container Plugin" by navigating to Jenkins -> Manage Jenkins -> Manage Plugins -> Available

4) Configure Tomcat in Job : Open Job configuration by clicking on Jenkins -> SpringBootRestExample -> Configure -> Post-Build Actions as shown below.

Credentials : credentials for user with role "manager-script"

Tomcat URL : Tomcat URL as shown in image

Jenkins-Post-Build-Tomcat

On click of Save , you will be done with configuration of Auto Deployment of build . Now, test the Auto Deployment by clicking on Build Now in Job and see the Console Output. Once you see the SUCCESS for Deploying war to container step, you can go to application URL and see the output.

Application URL : http://<host-name-or-ip>:8081/cfe/hello

Configure Job to trigger Auto Build and Deployment of application on Git Commit

Till now you have seen triggering the build manually by clicking on "Build Now" of Job. You can automate this build and deployment triggering on Git commit by configuring as below.

1) Configure Build Job : Open Job Configuration by navigating to Jenkins -> SpringBootRestExample -> Configure

i) Check  the checkbox for GitHub Project and provide project URL

https://github.com/<username>/SpringBootRestExample/

ii) Under "Build Triggers" section, check the checkbox for "GitHub hook trigger for GITScm polling" and Save the configuration.

2) Add Jenkins webhook in Git repository.

Git-Webhook-for-Jenkins

That's it. You are done with configuring Auto Build and Deployment on Git Commit. Now you can try committing some change in Git such as to modify the message in "SpringBootRestExample/src/main/java/com/cfe/SpringBootRestController.java" and see the auto build trigger and deployment with change by opening the Jenkins build history and result of Git change in deployed application at URL : http://<tomcat-hostname>:8081/cfe/hello

 

 

2 Comments

  1. Please provide steps for Install and configuration Jenkins in windows env.

    Reply
    • @Sreya, You can download the Jenkins from below link and extract it. Then you can install by clicking on Jenkins exe file and following the instructions which are simple to follow. After installation, Jenkins job creation and configuration will be same as shown in this article.

      Link to download latest Jenkins for Windows : http://mirrors.jenkins.io/windows/latest

      Reply

Leave a Reply to Prashanth Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

www.000webhost.com