Deploy Spring Boot application to AWS EC2 using GitHub Actions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to my channel integration ninjas in today's video tutorial we are going to deploy spring boot application to an AWS ec2 instance using Docker and GitHub actions in today's digital landscape deploying applications efficiently and securely is a crucial aspect of modern software development by combining the power of springboard Docker AWS ec2 and the automation capabilities of GitHub actions we can streamline the process ensuring scalability reliability and maintainability for our applications throughout this tutorial you will learn step by step how to leverage this Technologies to deploy your spring boot application to a live environment whether you are a seasoned developer looking to ensure deployment skills or a newcomer seeking to learn the ropes of modern application deployment practices this tutorial is designed to provide you with practical knowledge that you can apply to your projects so let's dive in an embark on this journey to deploy your springboot application with confidence and ease if you are new to Docker as well let me just explain in short that what is Docker with example think of Docker as a container service for your software just like shipping container hold all necessary piece of a software application like code dependencies and still event settings this container makes it easy to transport your software from one place to another and it ensures the software runs the same way no matter where it is used I have explained it really well in my previous video of deploying node.js app to ec2 you can refer that as well as part of this workflow we have I have created a workflow chart what we are going to perform I have a springboot 3 application which will be running on Java 17 the spring would say minimum requirement is Java 17 so we are going to write a Docker file for our springboot application and our workflow we will create where we will create two jobs build and deploy as part of build job which will be running on getting Foster Runner we are going to build the docker image and will publish it to Docker Hub once that build job is successful then in the next deploy job which will be running on AWS instance uh where we will register our repository self-hosted Runner GitHub self hosted Runner to AWS ec2 it will run on that instance there we will be going to pull the docker image from the docker hub and will run that Docker image as a container service on ec2 instance so let's move to the Hands-On practice okay I have this spring boot example here so I will create a branch deploy to ec2 okay so this application is nothing just it is we will just hit the end point and we'll get some result like this integration ninjas name and the email address the email address of my username this simple API we can see the code what we have done as part of this you can move to controller package and see what is the code which is this API cap mapping API on the direct URL access we're going to provide this details name and email address as part of response okay Okay so first let me configure this check out this branch okay and this is Master branch I will check out the branch and we'll pull the latest changes I have and will move to this deploy to ec2 branch okay so let's start doing this mean delete this existing files I will create a new Docker file for this applications how we are going to do is we will just okay we are going to create a new Docker file here we will be defining the base image which we are going to build so we are going to create a our Docker image based on Java so we are going to use a jdk we will use Eclipse demure in jdk okay so we will use the 17 jdk Alpine here so inside Docker we will use this jdk to run our project as a Java okay and now we'll Define our work directory that will be slash app we will name it as inside container will have this directory and now we are going to copy this jar file that we are going to build as part of this project spring boot hyphen example dot jar to the container so and this is inside Target then springboot example.jar we are going to give the same name in the container as well and now we are going to expose the port 8080 since our application is running on 8080 inside Docker container we are going to expose this port now we will run this jar how we can run simply Java hyphen jar we can use Java python jar and the file name which is bring boot hyphen example dot jar okay so this is our Docker file and now we are going to write a workflow file so we are going to write a new workflow file we are going to name it as CI CD and where it will be triggered on event we need to Define where it will be triggered on push event it will be triggered correct and on which branch it will be triggered we need to Define that as well we will Define deploy to ec2 whenever we are going to commit and push anything to this Branch this job will be triggered and it will build and deploy to AWS ec2 for that we'll write two jobs okay job okay which will be running on the GitHub host returner Ubuntu latest Ubuntu letters is provided by GitHub this is Runner it is free to use for a limit in this image we have pre-installed Java and Maven and Docker so we don't need to install we just need to configure the Java environment variable to Java 17. for that we will configure the Java it will not be downloaded it will be cached okay so first of all we need of this code to be present inside the GitHub questioner for that we will check out this code check out source and will use the action which is provided by GitHub to check out the source code actions checkout version 3. all right now we will be considering the Java okay we'll name it as setup Java for that we will be using one another action which is provided by get out action setup Java version 3 with distribution this and this so we can reuse the Sim you reuse the same okay so for that this is action slash checkout not check out it will be set up hyphen Java at the rate version 3 and with the these variables the distribution we need to provide which will be tamurin and Java hyphen version so these are basically the inputs accepted by this setup Java these are the inputs now what we are going to do is we are going to install build our project or install the dependencies you can see build project we will run this command to build our project mvn clean install okay so after doing this we will log into the docker Hub container to push the image for that we can write another step log into Docker hub so for that we will use this command Docker login hyphen U username and hyphen p and password okay so yeah this is one thing so username and password how we are going to use we will be I'm not going to expose it here directly we'll use secret variables we can Define secret variables in our repository settings if you go to the settings we will not expose it in our yaml file but we will be able to access from the secret variables under security and secretary variables click on actions under Secrets I have added this Docker username and password this is easy you can configure it like this click on new depository Secret and click on test give some many test name and test value and just click on ADD secret that's it a repository secret will be added like this so how we are going to access this okay we are going to access this like this dollar and double curly processes and Secrets dot the variable name okay so we are going to do it same for the passwords Secrets Dot Docker underscore password which is my variable for password all right now what I'm going to do is I'm going to build the docker image so my current code and will publish it to Docker hub for that I am going to use this command docker build hyphenique and the image name I'm giving image name as integration ninja slash bring both hyphen example okay which will be created from this directory okay now I am going to publish this image to Docker hub for that I will run this command Docker push and the same image name entering Creations yeah the type of mistakes it apples and leave it Developers Docker push integration ninjas and the latest tag of my image I am going to push okay so this is one job now we are going to you define another job which will be the deploy job once this process will be finished it will create another job deploy a job when this will be triggered we will Define this that this needs build job to be processed first then this will be triggered okay so and where it will run runs on we need to define a runner here I will be using a self-hosted runner here I will register that runner on AWS ec2 instance okay so for that you can simply go to your AWS ec2 instance and you can click on connect and simply if you click on connect you will be able to access the terminal AWS terminal it depends on you can login into a putty or you can log in it from the SSH also from the terminal okay now how will register the self-hosted runner where click on Runners action and Runners then click on new self hosted Runner and click on the Linux because our machine is Linux AWS Linux machine we have we are going to perform these steps okay so let's configure the runner self-hosted Channel we have created a new directory actions Runner there we will download this action binary okay after that we are going to extract the GitHub installer after that we are going to configure it okay so let's configure this okay enter the name of the runner group I'm having it as a default I am giving the name to my Runner AWS ec2 and giving the same label name AWS ec2 and the work folder and setting it as default so if you see the runner is settings configuration settings is successfully saved after that you need to run the runner self-hostile runner for that you can just use this command run dot as such you can execute this top file so it is showing connected to GitHub okay we can see it here in our Runners page AWS hyphen ec2 we will be using this Runner now our repository will have and pipeline will have access to AWS ec2 and spans okay so there we can perform these steps that we can pull the image from Docker hub okay and with this following command docker pull and I will use this image name with the latest image which is available okay I am going to use this and after that I will I will going to run it as container run Docker container okay for that I will execute this command Docker run hyphen D hyphen p i will Define the ports that I have used 8080 I'm going to use to access this application now I will give this container a name spring boot hyphen example hyphen container this I will use if it is already exist I will remove and then again if suppose if I am deploying again and again so I will remove the existing one so that's why I'm giving the name okay and we'll use the image Docker run and this image so we can add one more step here that if this already exists since we will deploy frequently sometimes after committing some changes delete old container okay so there we can run this command Docker RM hyphen f and spring boot example container all right so this is the workflow complete workflow let's see how it goes if any mistake is there we will correct it okay let's comment this changes hello deploy to ec2 now we are going to push this changes and we'll see if this is working properly or not you can see the event has been triggered the build and the deploy jump let's monitor this this is in progress build project mvn clean install okay there is some issue the push refers to repository integration ninjas okay there is some issue with this requested access to Resource is denied we can see to the docker Hub and I'm going to login to Docker Hub with my credentials and see foreign we are going to use we can correct it here if it is strong okay we can build this is my correct username there is some typo in the username so we can correct and reuse yeah the username was incorrect going right so now we are going to run this now we can monitor this so again the brain is in progress this we need because we need to build the jar and create the jar and that jar will be used while creating the image since this job is successful there was a typo in the username now we can see okay the deploy job is also successful okay great so now let's see if we can access that our application or not we will use the AWS instance IP address okay and we have used the port 8080 so we will try to access with that okay this is not accessible because we need to enable the inbound rules to for this Security Group as well for the cc2 we'll click on this Security Group and edit the inbound rules okay and there I will add a rule which will be having the custom TCP and the port 8080 and I will give this range okay I will give a custom range of this zero zero zero and we'll save this rules and now I will try to access this yes you can see this is accessible now this is how now if you comment every time it will go to that specific Branch deployed to receive to Branch you can give any branch name you will be able to deploy every 10 once your changes will be deployed to that particular Branch this is how you can deploy your spring boot application to AWS ec2 instance by containerizing your application and deploy with the power of GitHub actions please comment and let me know like if you like my video please share and subscribe if anything is needed you just tell me I will improve and also I will try to have to deployment of approach on ECR and ECS in my upcoming videos thank you everyone
Info
Channel: Integration Ninjas
Views: 7,446
Rating: undefined out of 5
Keywords: spring boot, spring boot docker, java, spring boot deployment in aws, spring boot deployment, spring boot 3, spring boot microservices, how to deploy spring boot application in aws, how to deploy spring boot application, how to deploy spring boot application in docker, how to deploy spring boot app on server, spring boot github actions, spring boot ci/cd, spring boot example, github actions, github actions cicd, github actions tutorial, java docker file, java docker container
Id: 21V0w6pVYLY
Channel Id: undefined
Length: 25min 33sec (1533 seconds)
Published: Sat Aug 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.