Skip to main content

How to build and deploy a web project using BitBucket + Ubuntu Shell Script


  • In General, we deployed a web project using Jenkins or Pipelines (BitBucket) or many DevOps other tools but what we could do when we have a limited budget or resource for server-side. Is another way to build and deploy your web project automatically with a schedule? I will say, yes it is possible to do that. So let’s try to understand a process.
Step1:
  • Here I will use Ubuntu Shell Script with a cronjob, Spring Boot (Hello! World), bitbucket, and AWS E2 Instance where we will deploy a web application.
  • Here I created one Spring boot HelloWorld.


  • As you can see above the screen-shot, I created a package `com.spring_boot.controller.HelloWorldController` and rest controller inside it.






  • And commit and push code in a bitbucket repository.
Step 2:
  •  let’s connect to AWS EC2 instance Ubuntu and install requirement SDK like Open JDK 8, Maven, and Apache Tomcat 8.
  • If you didn't install SDK tools then you can follow the below commands for ubuntu.
    • sudo add-apt-repository ppa:openjdk-r/pp
    • sudo apt-get update
    • sudo apt-get install openjdk-8-jdk
    • sudo apt-get install maven

Step 3:
  • Now I will configure `SSH` keys in the AWS EC2 instance by following Bitbucket Documentation
(https://confluence.atlassian.com/x/X4FmKw).

Step 4:
  • Once you configured `SSH` Keys with AWS EC2 Ubuntu instance and Bitbucket.
  • Create a folder for a project and take the clone of the project in AWS EC2 Ubuntu instance.
  • Now I will take the clone using `SSH` as you can see below the screenshot.











      • mkdir your_source_dir_name
      • cd your_source_dir_name
      • git clone bitbucket_url


      Step 5:  
      • Now let's create an Ubuntu Shell Script which builds and deploys your code in tomcat
        • Here I used the `VI` editor of Ubuntu, follow the below command to create a shell script
        • vi .sh,  if you don't know about `VI` then you can easily find out from google or any search engine or any other tutorial.  
        • To run your shell script type: sh ./<your_script_name>.sh
        • Build commands are given below:
              
       #!/bin/bash
      
      # First Go to your tomcat dir and shutdown your tomcat
      cd /opt/tomcat/apache-tomcat-8.5.47/bin/
      sh ./shutdown.sh
      sleep 5
      
      # Now, Move your project dir  
      cd /opt/spring_hello_world/spring_boot_hello_world/
      sleep 5
      
      # Take a pull of current branch
      git pull origin master
      sleep 5
      
      # Make a war of Project 
      mvn clean compile package
      sleep 5
      
      # Now copy a war file to tomcat webapp dir 
      cp -i /opt/spring_hello_world/spring_boot_hello_world/target/spring_hello_world.war /opt/tomcat/apache-tomcat-8.5.47/webapps/
      sleep 5
      
      # remove target folder after copy war
      rm -r target/
      sleep 5
      
      # Finally Start Your Tomcat and check out log file
      cd /opt/tomcat/apache-tomcat-8.5.47/bin/
      sh ./startup.sh
      
              
          



       

      Step 6: Now Let's add some changes in `HelloController` and push that into a server.




      Step 7: once you pushed your code build your shell script again by the following a command sh ./<your_script_name>.sh



      So as you can see a new change from the above screenshot. You can also try the way in which I followed.

      Thanks  

      Popular posts from this blog

      Replace Email Template Using Java Pattern and Matcher Class

      Let's say you have some simple email template that contains details like email address, phone/mobile or activation link, etc and you want to generate an email template without any free marker dependency using Java Pattern and Matcher class. So let's try to make that and also download a link is given bottom side. Step 1:    Here I created one email template which HTML code looks below. Hi {{user.first_name}} {{user.last_name}}, Welcome and thank you for subscribing. Now that you’ve subscribed, you’ll be receiving our emails {{freq.duration}}. These emails will contain our most recent article or news. You will also receive exclusive offers, promotions, and information on events. Our team is happy to have you on board. If you have any questions or comments, please don’t hesitate to contact us. You can simply reply to our emails or contact me directly using the contact information below. Thanks again! Step 2:  As you can see...

      Java interview questions and answers for experienced (Core Java, Spring Boot, Hibernate, JPA)

       Here is a list of interview questions for the experienced guy who had more than 5 years of experience in Java, Spring Boot, Spring MVC, Hibernate, JPA. Please follow the questions bank for it.   @component vs @service vs @repository @Component: It is to mark a bean as a Spring-managed component. It is a generic stereotype for any spring-managed component. Spring will only pick up and register beans with @component and doesn't look for @service and @Repository @service:  The @Service annotation represents that our bean holds some business logic. @Repository: @Repository is a stereotype for the persistence layer and it is also a type of component. Its job is to catch all persistence-related exceptions and rethrow them as Spring DataAccessExceptions. Explain spring Bean Lifecycle A Spring bean needs to be instantiated when the container starts, based on JAVA or XML bean definition. ...

      IELTS - General Training - Essay Writing Task 20

      Do you believe that experimentation on animals for scientific purposes is justified. Are there any alternatives to animal experimentation? You should write at least 250 words. Answer :  Animals are indeed used for any research activities. I believe that experiments or any research on animals should be banned because animals are part of nature and It is ethically unacceptable but there are few alternatives to animal experimentation. Although, there is some reason that animals are used as an experimental object. Firstly, a human is the most important being on the planet. So any research which can affect human life should be done on animals to make sure human safety and survival. Researchers enable to test on animals to make sure the safety of the medication and other substances. Another that, Some bacteria or virus usually found in animals bodies. Those bacteria or viruses live as inactive in their bodies. For Instance, Plague is an infectious disease caused by bacteria, usually foun...