- 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.
- 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.
- 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
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>
So as you can see a new change from the above screenshot. You can also try the way in which I followed.
Thanks