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.
2) Add Jenkins repository using following command .
3) Add GPG key for Jenkins.
4) Install Jenkins using Yum
5) Start the Jenkins service now.
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.
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.
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.
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.
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.
2) Click on OK and configure Source Code Management with Git repo as following.
3) Next configure Build stage with Maven target as shown below.
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.
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
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
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.
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