{"id":653,"date":"2018-06-03T06:30:58","date_gmt":"2018-06-03T06:30:58","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=653"},"modified":"2018-06-03T06:31:43","modified_gmt":"2018-06-03T06:31:43","slug":"jenkins-workshop-using-git-maven-and-tomcat","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=653","title":{"rendered":"Jenkins : Workshop &#8211; CI\/CD &#8211; Using Git, Maven and Tomcat"},"content":{"rendered":"<p>In this example we will first use <strong>Git<\/strong> CLI to upload source code to <strong>GitHub<\/strong> repository. Then we will use <strong>Jenkins<\/strong> which will check for any new code uploaded to GitHub repository every 15 minutes, and then use <strong>Maven<\/strong> to package the code, and then deploy it to the <strong>Tomcat<\/strong> server. The CI\/CD life cycle.<\/p>\n<p><strong><u>Pre-requisite:<\/u><\/strong><\/p>\n<ul>\n<li>A new &#8220;CentOS 7&#8221; host with 2GB RAM and 10GB free space<\/li>\n<li>Install Maven, following the example shown in my tutorial &#8220;Install Apache Maven on CentOS&#8221;. This can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=455\">here<\/a>.<\/li>\n<li>Create the source repository structure to store code, create the &#8220;pom.xml&#8221; file for Maven, etc using the same example shown in my previous tutorial &#8220;Maven: Use Maven to package a \u201c.war\u201d file (Ready to deploy in Tomcat)&#8221;. This can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=639\">here<\/a>.<\/li>\n<li>Though we will not be using the specific tutorial &#8220;Git and GitHub basics&#8221;, it can get you familiarize with Git. This can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=445\">here<\/a>.<\/li>\n<li>Install Git tool following the above tutorial.<\/li>\n<li>Create a new GitHub repository named &#8220;<strong>MySecondWebApp<\/strong>&#8220;, accessible via &#8220;<em>https:\/\/github.com\/&lt;GitHub user account&gt;\/MySecondWebApp.git<\/em>&#8220;.<\/li>\n<li>Install Jenkins, following the example shown in my tutorial &#8220;Jenkins &#8212; Intro and Installation&#8221;. This can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=464\">here<\/a>.<\/li>\n<li>Install Tomcat, following the example shown in my tutorial &#8220;Apache Tomcat : Installation and application deployment basics&#8221;. This can be accessed by clicking <a href=\"http:\/\/shijuvarghese.com\/?p=635\">here<\/a>.<\/li>\n<li>Configure Jenkins to be accessed via tcp port &#8220;<strong>9090<\/strong>&#8221; since tcp port &#8220;8080&#8221; will be used by Tomcat.<\/li>\n<\/ul>\n<p><em><strong>[root@gw16-lap-devops \/]# systemctl restart jenkins<\/strong><\/em><br \/>\n<em><strong> [root@gw16-lap-devops \/]# systemctl restart tomcat<\/strong><\/em><\/p>\n<p>Let us start by initialising a local Git repository. Let us create and navigate to the folder named &#8220;\/sandbox&#8221;<br \/>\n<em><strong>[root@gw16-lap-devops \/]# mkdir \/sandbox<\/strong><\/em><br \/>\n<em><strong> [root@gw16-lap-devops \/]# cd \/sandbox<\/strong><\/em><\/p>\n<p>Now let us initialize the folder<br \/>\n<em><strong>[root@gw16-lap-devops sandbox]# git init<\/strong><\/em><br \/>\n<em><strong> [root@gw16-lap-devops \/]# git remote add origin &#8220;https:\/\/github.com\/&lt;GitHub account name&gt;\/MySecondWebApp.git&#8221;<\/strong><\/em><\/p>\n<p>Copy the contents and folders for the project created by Maven in the tutorial &#8220;Maven: Use Maven to package a \u201c.war\u201d file (Ready to deploy in Tomcat)&#8221;<br \/>\n<em><strong>[root@gw16-lap-devops \/]# cp -R \/data2\/MySecondWebApp\/* .<\/strong><\/em><br \/>\n<em><strong> [root@gw16-lap-devops \/]# rm -rf target\/<\/strong><\/em><\/p>\n<p>Add the new files to Git and proceed<br \/>\n<em><strong>[root@gw16-lap-devops \/]# git add src<\/strong><\/em><br \/>\n<em><strong> [root@gw16-lap-devops \/]# git commit -a -m &#8220;Added the entire folder&#8221;<\/strong><\/em><\/p>\n<p>The below command will upload the code to the GitHub repository. User credentials will be asked.<br \/>\n<em><strong>[root@gw16-lap-devops \/]# git push origin master<\/strong><\/em><\/p>\n<p>Now let us work on Jenkins. Ensure you are familiar with the Jenkins tutorial mentioned in the pre-requisite!!<\/p>\n<p>Ensure the folder &#8220;<strong>\/var\/lib\/jenkins\/workspace<\/strong>&#8221; exists in the host.<br \/>\n<em><strong>chown -R jenkins.jenkins \/var\/lib\/jenkins\/workspace<\/strong><\/em><\/p>\n<p>Ensure the following plugins are present in Jenkins:<\/p>\n<ul>\n<li>GitHub plugin<\/li>\n<li>Git plugin<\/li>\n<li>Git client plugin<\/li>\n<li>Maven Integration<\/li>\n<li>Deploy to container Plugin<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li>Go to Jenkins dashboard by accessing the URL &#8220;<strong>http:\/\/192.168.1.16:9090\/<\/strong>&#8220;.<\/li>\n<li>Click on &#8220;<strong>New Item<\/strong>&#8220;<\/li>\n<li>Enter the value for &#8220;<em>Enter an item name<\/em>&#8221; as &#8220;<strong>MySecondWebAppProj<\/strong>&#8220;<\/li>\n<li>Select &#8220;<strong>Maven project<\/strong>&#8220;, and Press <strong>OK<\/strong>.<\/li>\n<li>Enter the <em>Description<\/em> as &#8220;<strong>Download code using Git, package using Maven, and deploy to Tomcat<\/strong>&#8220;.<\/li>\n<li>Select &#8220;<strong>Discard old builds<\/strong>&#8221; and enter the value for &#8220;<em>Max # of builds to keep<\/em>&#8221; as &#8220;<strong>1<\/strong>&#8220;.<\/li>\n<li>In the &#8220;<em>Source Code Management<\/em>&#8221; select &#8220;<strong>Git<\/strong>&#8221; and enter the value for &#8220;<em>Repository URL<\/em>&#8221; as &#8220;<strong>https:\/\/github.com\/&lt;GitHub account name&gt;\/MySecondWebApp.git<\/strong>&#8220;.<\/li>\n<li>In the &#8220;<strong>Build Triggers<\/strong>&#8221; section, the only option marked check should be &#8220;<strong>Poll SCM<\/strong>&#8220;. The value for &#8220;<em>Schedule<\/em>&#8221; should be &#8220;<strong>H\/15 * * * *<\/strong>&#8220;.<\/li>\n<li>Nothing should be selected under &#8220;<em>Build Environment<\/em>&#8221; or &#8220;<em>Pre Steps<\/em>&#8220;.<\/li>\n<li>In the &#8220;<strong>Build<\/strong>&#8221; section, the value for &#8220;<em>Root POM<\/em>&#8221; should be &#8220;\/<strong>var\/lib\/jenkins\/workspace\/MySecondWebAppProj\/pom.xml<\/strong>&#8220;, and the value for &#8220;<em>Goals and options<\/em>&#8221; should be &#8220;<strong>clean package<\/strong>&#8220;.<\/li>\n<li>Click on &#8220;<strong>Apply<\/strong>&#8221; and then on &#8220;<strong>Save<\/strong>&#8220;<\/li>\n<\/ul>\n<p>Before we start configuring Jenkins to deploying &#8220;.war&#8221; file in tomcat, let us check if we can make a build successfully.<\/p>\n<ul>\n<li>If we can get a successful build, then verify if a file named &#8220;<strong>\/var\/lib\/jenkins\/workspace\/MySecondWebAppProj\/target\/MySecondWebApp.war<\/strong>&#8221; gets created.<\/li>\n<li>Verify the contents of the folder &#8220;<strong>\/var\/lib\/jenkins\/workspace\/MySecondWebAppProj\/target\/MySecondWebApp<\/strong>&#8221; to ensure all files in the &#8220;\/sandbox&#8221; are present.<\/li>\n<li>Go to the Jenkins dashboard, and select the newly created project &#8221; <strong>MySecondWebAppProj<\/strong>&#8220;.<\/li>\n<li>Once in the project&#8217;s page, click on &#8220;<strong>Configure<\/strong>&#8220;.<\/li>\n<li>Go to the section &#8220;<strong>Post-build Actions<\/strong>&#8220;, and click on &#8220;<strong>Add Container<\/strong>&#8220;.<\/li>\n<li>In the text-field &#8220;<em>WAR\/EAR files<\/em>&#8220;, enter the value &#8221; <strong>**\/*.war<\/strong> &#8220;. This will look for &#8220;.war&#8221; files inside the Jenkins Home folder and its sub-folders.<\/li>\n<li>In the text-filed &#8220;<em>Context path<\/em>&#8221; enter a name, for example &#8220;<strong>MySecondWebApp.war<\/strong>&#8220;.<\/li>\n<li>The &#8220;<em>Containers<\/em>&#8221; selected in my case is &#8220;<strong>Tomcat 7.x<\/strong>&#8220;.<\/li>\n<li>Ensure the credentials are added and selected.<\/li>\n<li>The &#8220;<em>Tomcat URL<\/em>&#8221; I use is &#8220;<strong>http:\/\/192.168.1.16:8080<\/strong>&#8220;.<\/li>\n<li>Select &#8220;<strong>Deploy on failure<\/strong>&#8220;.<\/li>\n<li>Click on &#8220;<strong>Apply<\/strong>&#8221; and &#8220;<strong>Save<\/strong>&#8220;<\/li>\n<\/ul>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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 <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=653\" title=\"Jenkins : Workshop &#8211; CI\/CD &#8211; Using Git, Maven and Tomcat\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":465,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,21,3],"tags":[],"class_list":["post-653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-configuration-management","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":2,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":655,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/655"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/465"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}