Deploy a Spring Boot application to Heroku using Github and CI/CD pipeline

Deploy Spring Boot to Heroku

Deploy a Spring Boot application to Heroku using Github and CI/CD pipeline

Did you create a Spring Boot application on your local machine and always wondered how would I show it to peers or other developers ? The simplest option is to host it in a cloud platform but there are many options and what is the best way to determine a cloud provider who gives it for free of cost ?

In this post, we are going to see how to create a DevOps pipeline where we would push our Spring Boot code into Github repository and from there deploy it to Heroku.

A small note about CI/CD here which stands for Continuous Integration and Continuous Deployment.

CI stands for building and testing code after every implemented change. Code changes from different developers are automatically integrated into the project

CD is the constant and automated production deployment of every change made to the code. The changes should be automated, without human intervention.

Table Of Contents:-

Prerequisites

Commit Code into Github

Deploy from Github to Heroku


1. Prerequisites

We should have a Spring Boot application that is up and running locally on our machine.

Must have a Github account where the code has to be pushed (if not, sign up for free at www.github.com )

Add a file called as system.properties by including this line as Heroku expects this line to find out the Java version.

java.runtime.version=11
#could be any java version which is used in application

Similarly make sure that the Maven version is indicated properly in pom.xml else we might see issues during the build process.

Commit Code into Github

Below are the steps to push our application from local to Github repository

Step 1: Create a new repository in Github by clicking on "+" icon like in below image

1.create repo.png

Step 2: In Eclipse/STS, right-click on your project and choose Show in Local Terminal -> “Git Bash”) If its Intellij, then right click and Open in -> Terminal.

For some reason, if Git Bash is not showing then need to install using link https://git-scm.com/downloads. May have to restart the IDE once its installed

Step 3: Initialise a local Git repository using the below command in the terminal (Mac users) or command prompt (windows users) or can do via the IDE as well

git init
#initialising

Step 4:

Add the project files & folders to the local git repository. This stages files for first/initial commit

git add .
#adding

Step 5:

Then commit the files/folders from your workspace repository to the local repository

git commit -m "The first commit"
#committing, the comment under quotes could be anything, here it is just to signify that its for very first time

Step 6:

Go to Github repository and click on the drop-down under code which would give a link for the remote repository url. It ends in .git like in below image

2. github copy.png

Step 7:

Need to add the remote repository url that was copied in the previous step and verify the remote repository by using commands

git remote add origin <remote-repository-url>
git remote -v
# verifies the remote url

Step 8:

Final step is to push the files to remote github repository using the command below

git push origin main
#push local code to main branch in remote

We need to go to GitHub repository and verify if the files have been committed properly.

Deploy from Github to Heroku

Step 1:

Login to Heroku account and create an App. Just specify the name of the app and click on Create like in below image

heroku create.png

Step 2:

In the deployment method, click on 2nd option which is Github, have to authenticate since Heroku is trying to access the repositories present in Github account

github select in heroku.png

Step 3:

Enter the name of the Github repository and click on search

heroku repo connect.png

Step 4:

Once it finds the repository, Click on Connect

Step 5:

We are going to see the Manual deploy process where we have to choose the branch and then click on Deploy Branch like in below image

heorku deploy.png

Step 6:

Once the deployment is successful, then we will see a link “View”. Click on it and can view the application live like in image

heroku app success.png