In this example we will first use Git CLI to upload source code to GitHub repository. Then we will use Jenkins which will check for any new code uploaded to GitHub repository every 15 minutes, and then use Maven to package the code, and then deploy it to the Tomcat server. The CI/CD life cycle.
- A new “CentOS 7” host with 2GB RAM and 10GB free space
- Install Maven, following the example shown in my tutorial “Install Apache Maven on CentOS”. This can be accessed by clicking here.
- Create the source repository structure to store code, create the “pom.xml” file for Maven, etc using the same example shown in my previous tutorial “Maven: Use Maven to package a “.war” file (Ready to deploy in Tomcat)”. This can be accessed by clicking here.
- Though we will not be using the specific tutorial “Git and GitHub basics”, it can get you familiarize with Git. This can be accessed by clicking here.
- Install Git tool following the above tutorial.
- Create a new GitHub repository named “MySecondWebApp“, accessible via “https://github.com/<GitHub user account>/MySecondWebApp.git“.
- Install Jenkins, following the example shown in my tutorial “Jenkins — Intro and Installation”. This can be accessed by clicking here.
- Install Tomcat, following the example shown in my tutorial “Apache Tomcat : Installation and application deployment basics”. This can be accessed by clicking here.
- Configure Jenkins to be accessed via tcp port “9090” since tcp port “8080” will be used by Tomcat.
[root@gw16-lap-devops /]# systemctl restart jenkins
[root@gw16-lap-devops /]# systemctl restart tomcat
Let us start by initialising a local Git repository. Let us create and navigate to the folder named “/sandbox”
[root@gw16-lap-devops /]# mkdir /sandbox
[root@gw16-lap-devops /]# cd /sandbox
Now let us initialize the folder
[root@gw16-lap-devops sandbox]# git init
[root@gw16-lap-devops /]# git remote add origin “https://github.com/<GitHub account name>/MySecondWebApp.git”
Copy the contents and folders for the project created by Maven in the tutorial “Maven: Use Maven to package a “.war” file (Ready to deploy in Tomcat)”
[root@gw16-lap-devops /]# cp -R /data2/MySecondWebApp/* .
[root@gw16-lap-devops /]# rm -rf target/
Add the new files to Git and proceed
[root@gw16-lap-devops /]# git add src
[root@gw16-lap-devops /]# git commit -a -m “Added the entire folder”
The below command will upload the code to the GitHub repository. User credentials will be asked.
[root@gw16-lap-devops /]# git push origin master
Now let us work on Jenkins. Ensure you are familiar with the Jenkins tutorial mentioned in the pre-requisite!!
Ensure the folder “/var/lib/jenkins/workspace” exists in the host.
chown -R jenkins.jenkins /var/lib/jenkins/workspace
Ensure the following plugins are present in Jenkins:
- GitHub plugin
- Git plugin
- Git client plugin
- Maven Integration
- Deploy to container Plugin
- Go to Jenkins dashboard by accessing the URL “http://192.168.1.16:9090/“.
- Click on “New Item“
- Enter the value for “Enter an item name” as “MySecondWebAppProj“
- Select “Maven project“, and Press OK.
- Enter the Description as “Download code using Git, package using Maven, and deploy to Tomcat“.
- Select “Discard old builds” and enter the value for “Max # of builds to keep” as “1“.
- In the “Source Code Management” select “Git” and enter the value for “Repository URL” as “https://github.com/<GitHub account name>/MySecondWebApp.git“.
- In the “Build Triggers” section, the only option marked check should be “Poll SCM“. The value for “Schedule” should be “H/15 * * * *“.
- Nothing should be selected under “Build Environment” or “Pre Steps“.
- In the “Build” section, the value for “Root POM” should be “/var/lib/jenkins/workspace/MySecondWebAppProj/pom.xml“, and the value for “Goals and options” should be “clean package“.
- Click on “Apply” and then on “Save“
Before we start configuring Jenkins to deploying “.war” file in tomcat, let us check if we can make a build successfully.
- If we can get a successful build, then verify if a file named “/var/lib/jenkins/workspace/MySecondWebAppProj/target/MySecondWebApp.war” gets created.
- Verify the contents of the folder “/var/lib/jenkins/workspace/MySecondWebAppProj/target/MySecondWebApp” to ensure all files in the “/sandbox” are present.
- Go to the Jenkins dashboard, and select the newly created project ” MySecondWebAppProj“.
- Once in the project’s page, click on “Configure“.
- Go to the section “Post-build Actions“, and click on “Add Container“.
- In the text-field “WAR/EAR files“, enter the value ” **/*.war “. This will look for “.war” files inside the Jenkins Home folder and its sub-folders.
- In the text-filed “Context path” enter a name, for example “MySecondWebApp.war“.
- The “Containers” selected in my case is “Tomcat 7.x“.
- Ensure the credentials are added and selected.
- The “Tomcat URL” I use is “http://192.168.1.16:8080“.
- Select “Deploy on failure“.
- Click on “Apply” and “Save“
To verify if everything is good, run the build for the project, check if the result is successful, and verify in Tomcat if the war file is getting deployed.