Java spring auto deployment to Tomcat 8 using Jenkins 2.0 in AWS cloud

Continuous Integration ( CI ) is a step forward in the software development life cycle targeted for faster delivery and early detection of issues. CI requires integration of the code to main repository at least once daily. This demands an automated deployment process which would generate the build and deploy to the container with the latest code changes of stable branch. We can achieve this automation using a CI server and Jenkins is one of most popular tool in this category.

In this post, I will be discussing the steps for setting up Jenkins and configuring auto deployment.

Objective

Our objective is to setup a CI server using Jenkins that can generate a build and deploy to the tomcat running in the production / staging server. We can target the flow as below:

  1. Developer will test a build and push the changes to a particular git branch hosted in github.
  2. Jenkins will identify the change in the branch and generate a build using Maven.
  3. If the build is successful, copy the war file to the Tomcat container and deploy.
  4. Send notification emails to specified recipients on the progress / status of the build.

We are going to automate all the above steps using Jenkins as CI tool.

Setup

This post is based on the setup that I have done in our environment which consists of :

  • Two AWS servers running Centos 7 OS ( One as CI server and another one as staging)
  • Jenkins 2.0
  • Java spring based application
  • Tomcat 8 server
  • Java JDK 8
  • Source code management using Git and hosted in Github

Most of the details specified in this post would be based on the above setup, but can be applied to any environment making small changes.

Configuring AWS servers

We can start by setting up the AWS servers. This involves installation of the JDK, apache tomcat and git

  1. Installing Java and Tomcat

    I am currently using centos 7 with Java 8 installed . The web container used is Tomcat8. You can read about the setting up of Java 8 and Tomcat 8 here.

  2. Installing Git client

    We are using Git as our source code management and is hosted in github.com. We need to have git running in our CI server since it need to connect to github and pull the latest changes.

    Install git by issuing the following command:

    sudo yum install git 

    Type ‘y’ when asked for confirmation and make sure that the installation if finished without error.

  3. SCP setup from CI Server to Prod server in AWS

    1. Make sure that both the servers are having a common security group in AWS.
    2. Add a custom rule in the security group to have all the ports open when the         source is security group itself.
      You can do this by selecting source as ‘Custom IP’ and then start typing the name of the security group in the box and select from suggestion.
    3. After the above steps are done, we need to make sure that the prod server can identify CI server using SSH . Please follow the below link for setting up SSH ( Consider server 1 as CI server and server 2 as prod server )
      http://blog.e-zest.net/how-to-do-scp-from-one-ec2-instance-to-another-ec2-instance/

 

Setting UP  CI Server

Once the AWS setup is done on both servers, we can continue to setup the CI server using jenkins. This is assuming that git and jdk 8 is already setup in the CI server in the above section.

  1. Download and Start Jenkins

    1. Download the latest jenkins.war from following location
      https://jenkins.io
    2. Create a folder in /opt as
      mkdir /opt/jenkins
    3. Copy the jenkins.war file to the above location
    4. Start jenkins using the following command
      java -jar jenkins.war –httpPort=9091
      Note: You can specify any port you want. I have chosen 9091 
    5. Enable the port in AWS security group inbound rules
    6. Enable traffic from the AWS box in the firewall settings
      firewall-cmd –permanent –add-port=9091/tcp
      firewall-cmd –reload
    7. Access the url from browser http://yourserverip:9091/
  2. One time setup for Jenkins

    First access of jenkins will ask you to enter some details and select some configuration :

    1. Enter the admin password. This is the default password generated by Jenkins when its first run.
      If installed as root, it will be in
      /root/.jenkins/secrets/initialAdminPassword
      If as any other user, it will be in the home directory
      /home/user/.jenkins/secrets/initialAdminPassword
      Copy the password from the file and put in the text field
    2. Setup a username and password for the Jenkins instance
    3. Next the screen will ask to install some plugins. Click on the option to install the recommended plugins and wait to finish.
    4. You will be taken to the home page.
  3. Installing the Maven Integration Plugin

    Our build system is maven based and hence need to have integration with maven. So we need to install the ‘Maven Integration Plugin’ from the Manage Plugins section.

    1. Click on the Jenkins icon in the home page and click on ‘Manage Jenkins’
      homepage
    2. Click on ‘Manage Plugins’ and then on ‘Available’ tab. Here search for the ‘Maven Integration Plugin‘ and check the box . Now click on on ‘Install without restart’
      plugins
    3. Wait for the installation to complete
  4. Configure system settings

    We need to setup up the system wide settings such as maven installation, email settings etc for notification.

    1. Click on ‘Manage Jenkins’ and click on ‘Configure System
    2. Scroll to the section ‘Jenkins location’ and put the email address you want to use for notification in the admin email address field
      admin_email
    3. Next scroll to ‘Extended E-mail Notification‘ section and put the SMTP address of your provider and check the box ‘use SMTP authentication’
      Enter the username ,password and PORT for the email account
      email-settings
    4. You can put the recepient list as comma separated email addresses
    5. If you want to send email notification for failed builds, you can setup the same email settings under ‘Email Notification’ section and test the email settings there.
    6. Save and apply
  5. Configure Maven in Jenkins

    We need to configure Maven in Jenkins for building the artifact for deployment. You can either show the maven installation path or ask Jenkins to install maven as plugin.

    1. Click on ‘Manage Jenkins‘ and then ‘Global Tool Configuration
    2. Scroll to the section ‘Maven
    3. Click on ‘Add Maven’
    4. If you already have Maven in the system, un-check the ‘Install automatically’ box and you will be provided with a text field to enter the current maven installation directory
    5. If maven is not already installed, leave the box checked and give a name. Jenkins will download and install the selected maven version as a plugin.
    6. Save and Apply
  6. Adding a job ( Maven project )

    This is the section where we specify the project to be build and the settings for the build. We will setup the job as a Maven project so that we can use Maven to build the project from source.

    1. Goto home page and click on ‘New Item’
    2. A new page will open. Give the name of the project and select the type as Maven project
      project-start
    3. This will open the project configuration page and we need to select the project as a Github project.
      Also provide the github URL for the projectgithub-project
    4. In source code management, put the github repo URL and add the credentials.
      Select provided credentials in the dropdown and make sure that no errors are shown.
      Also select the branch you want to have Jenkins to look for changes and take the build of. You will be pushing the changes to be deployed on this branch and could be specified as your master branch as well.scm-settings
    5. Scroll to  Build Triggers section, select options as in picture
      Here we are asking Jenkins to initiate a build when there is change to the specified branch in the section above.
      Also the poll schedule is set to 11pm every night. You can set it to any interval or time of the day ( The setting is similar to cron job )build-trigger
    6. Next we need to setup a script to be executed to when a build is created in Jenkins. Scroll to the Post Step section and from the dropdown, select ‘Execute Shell’
    7. Here we need to put the shell script for uploading the build to prod server tomcat.We can put the script as below :
      #!/bin/sh
      echo “Starting to copy the build”
      scp $WORKSPACE/target/application.war user@prodip:/opt/apache-tomcat-8.0.32/webapps
      echo “Copied the build to tomcat”

      buildcopyHere $WORKSPACE will refer to the workspace of the project application.war should be replaced with the name of the war file generated.
      Replace user@prodip with the user and internal ip of the prod server.
    8. Next we need to send email notifications to recipients after the build is deployed.
      Scroll to Post build action and select ‘Editable email notification‘ from the drop down.
      You can edit the fields or it will take the fields from ‘Configure system’ email settings of Jenkins.
      Click on ‘Advanced Settings’ -> ‘Add Trigger’  drop down and select ‘Always’ option. This is to get the email to recipient irrespective of build was successful or not.
      Also change the Send To to ‘Recepient List’ rather than developerspostemailaction
    9. Save and Apply
  7. Running of the Job

    Now that we have got the job setup, we can view the details and if required run the build on demand.

    1. Goto home page and you will be able to see the job listed there and it will show the details of the last successful build and failure.
    2. Click on the name of project
    3. On the left handside, you will be able to see different option for the project
      project-options
    4. You can click on ‘Build Now’ to generate a build on demand
    5. Also the ‘Git polling Log’ will give you the logs on the result of polling github for changes.
    6. Clicking on ‘Configure’ will take you to the configuration page of the project.
  8. Jenkins in action

    Based on the settings, Jenkins will poll github for changes to the specified branch and if there is a latest commit, it will take the branch, build using maven and then copy the war file to the specified webapps folder of the tomcat container.

    The Tomcat 8 container is capable of identifying changes to the war file and re-deploy without requiring a manual restart of the container.

Conclusion

Automating the build using Jenkins takes out a lot of burden from the deployment side and allows to concentrate  more on the business aspects.

Jenkins also provide a wide variety of configuration that can be applied to the build process. For eg: You can specify a maven goal to have all the tests run before building and stop if any of the tests are failed.

Hope this post has helped people who are starting with Jenkins and Continuous Integration. Please comment on the post for any queries and would be happy to help out.

 

regards
S

You may also like...

Leave a Reply

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