[3/3] Deploy the Spring Boot App Which is Using Docker and Docker Compose in Through “Github Actions” to Droplet

İlker Güldalı
5 min readJul 23, 2022

--

Implemented CI/CD process flow

👋 Hi everyone!
In this article, we will learn how to include a Spring Boot application running on Docker into CI — CD processes using Github Actions.

Things we will fully experience with this series of articles:
✔️[1/3] — Let’s Create a Spring Boot App With MySQL, Docker, Docker Compose
We will examine how to use Docker and Docker Compose on a Spring Boot — MySQL application and what adjustments are required.

✔️[2/3] — Push the Spring Boot App Docker Image to Docker Hub and Deploy to Digital Ocean Droplet
We’ll learn how to push previously locally created image to Docker Hub and simple way to deploy Digital Ocean Droplet.

👉[3/3] — Deploy the Spring Boot App which is using Docker and Docker Compose in through “Github Actions” to a Droplet
By implementing CI/CD processes into our project, we will experience making things a little easier.

In the previous article, we learned how can push the Docker image to Docker Hub and manually deploy to Droplet. In this article, we focus to the automate to manual steps through by Github Actions!

Let’s start!

Add Github Action to Spring Boot Project

First of all, in the main directory of project structure, we need to create file.github > .workflows > deploy.yml

Location of workflows directory

if we examine the deploy.yml file, after type action name — i.e Build & Deploy — it is determined in which situations the job should work. We can specify that job can work when a pull request merges into the master branch or it can triggered by manually.

on:  
push:
branches: [ master ]
workflow_dispatch:

Now we can start to steps section, first of all we must checks-out repository under $GITHUB_WORKSPACE with actions/checkout.

- uses: actions/checkout@v3

We first need to login as below to send the image to Docker Hub. Since we don’t want the username and password to appear in the repo, this information is kept in the “Action Secrets” of the repo.

- name: Login to DockerHub        
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

Github Actions — Secrets

Github allows us to keep the information that we do not want to appear in the repo for security reasons in the “Actions Secrets” section. Here, we will keep Docker Hub user info and Droplet info that we will connect via SSH. (For more detailed info: Secrets)

Secrets section in Github repository settings

DOCKERHUB_USERNAME: The username used when logging into Docker Hub.
DOCKERHUB_PASSWORD: The password used when logging into Docker Hub
SSH_HOST:Droplet’s IP info.
SSH_KEY: Droplet’s private ssh key info. (~/.ssh/id_rsa)
SSH_USER: Username info used when connecting to Droplet via ssh.

After defining the necessary information as secret, we can proceed to the step of building the app and sending it to Docker Hub.

With build-push-action, the build process is done over the Dockerfile location in the project files, and then the relevant image file is sent to Docker Hub. Since the Dockerfile is located in root, the context parameter is given a dot.

The information in the Dockerfile can be accessed from the first article of the article series.

- name: Docker Build and Push to DockerHub        
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ilkerguldali/boredapp:latest

Thus, up to this point, the application is built and the resulting image file is sent to Docker Hub. In the next step, we can examine how to make the final image file workable in Droplet.

After connecting to our droplet with ssh-action, we will make the final image file workable on docker with the script.

deploy_to_droplet:
runs-on: ubuntu-latest
needs: build_and_push
name: Deploy to Droplet
steps:
- name: Connect with SSH and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /boredapp
docker-compose stop bored-app
docker rm bored-spring
docker pull ilkerguldali/boredapp:latest
docker-compose up -d bored-app
docker ps -a

First, go to the folder where the docker-compose file is located and the related service is stopped with the docker-compose stop bored-app command. Then the latest image file is taken over Docker Hub and the related service is made run again with up command via Docker compose.

Pipeline view after deploy.yml is run
Bored App for bored people

🧿Conclusion

In this article series consisting of 3 articles, we examined how to dockerize a Spring Boot application, how to send the image file to Docker Hub and how to implement Github Actions. I hope it was helpful 🙏

Do not hesitate to let me know where you see missing or wrong 😸

You can access the Bored App project files below.

Thanks for reading, have fun coding!

--

--

İlker Güldalı
İlker Güldalı

No responses yet