Introduction to GitHub Actions. Making a pipeline for deploying a Spring Boot project on a VPS using Docker
In this article, I will show you how to build a simple pipeline for deploying a Spring Boot project on a server using GitHub Actions and Docker.
Content:
What do we have
Rented VPS with Docker and docker-compose installed
repository basic Spring Boot project with Dockerfile and docker-compose.yml
Runner setup
We will deploy on the server. To do this, you need to configure the runner.
Login to VPS. The runner cannot be run as the root user, so you will have to create a new one, remembering to give it permission to use Docker.
Creating a User and Granting Permissions for Docker
useradd -m user
paswwd user
groupadd docker
usermod -aG docker user
chsh -s /bin/bash user
su user
In the repository go to Settings → Actions → Runners → New self-hosted runner

Specify the parameters of our VPS. I have it Linux. Processor architecture – x64. After that, auto-generated scripts will appear to configure the runner.

Enter the first group of scripts.


Run the configuration script. Of all the default values, I only changed the name of the runner.


Now you can start the runner. I prefer to do it in the background with nohup.
nohup ./run.sh > runner.logs &
Let’s check the status of the runner in GitHub.

Creating a pipeline.
All pipeline scripts must be in a special folder {project}/.github/workflows .
Let’s create a script deploy-job.yml and put it in this folder.

In the first line, specify the name of the pipeline. It will show up on GitHub.
name: Deploy buy runner
Then we specify when to run this pipeline – in our case, let it be when pushing to the branch master or develop.
on:
push:
branches: [ "master", "develop" ]
Then we will start describing jobs.
The first thing we will do is run the tests.
jobs:
test: # Название джобы
runs-on: ubuntu-latest # Где должна выполняться джоба.
# Здесь укажем GitHub-hosted runner - среду выполнения, которую предоставляет сам GitHub.
steps: # Перечень шагов, которые необходимо выполнить в рамках джобы.
- name: cd into repo # Название шага
uses: actions/checkout@v3 # Ключевое слово uses используется для запуска заранее
# созданного набора команд. В данном случае используем дефолтную команду
# для обновления проекта до последнего коммита.
- name: set up Java and Maven # Настраиваем Java и Maven.
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: run tests
run: mvn test # Запускаем тесты.
Then we will prepare the environment – remove the old containers and images.
prepare-environment:
runs-on: self-hosted # Запускаем на раннере, который настроили до этого.
needs: test # Запускаем только если джоба test прошла успешно
steps:
- name: Stop and remove containers, networks
run: docker-compose down
- name: Remove unused data
run: docker system prune -a -f
After that, we will run the project using docker-compose.
deploy:
runs-on: self-hosted # Запускаем на раннере, который настроили до этого.
needs: prepare-environment # Запускаем только если джоба prepare-environment прошла успешно
steps:
- name: checkout repo
uses: actions/checkout@v3
- name: docker-compose
run: docker-compose up -d
Final file:
file
name: Deploy buy runner
on:
push:
branches: [ "master", "develop" ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v3
- name: set up Java and Maven
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: run tests
run: mvn test
prepare-environment:
runs-on: self-hosted
needs: test
steps:
- name: Stop and remove containers, networks
run: docker-compose down
- name: Remove unused data
run: docker system prune -a -f
deploy:
runs-on: self-hosted
needs: prepare-environment
steps:
- name: checkout repo
uses: actions/checkout@v3
- name: docker-compose
run: docker-compose up -d
Commit, push, wait, look at the result.

Everything is working. Now we can create a badge and push it into README.md. The steps are marked in the picture:

After that, a window with badge settings will appear.

I didn’t change the default settings. Copy the generated link, paste it into README.md and get the following:

Mobile Alerts.
I would like to note the function of notifications about the work of pipelines in the GitHub mobile application. You can enable it in the settings. It looks something like this:

In the case of a file, you can even see the logs.

Conclusion
In this article, I described the process of creating a simple GitHub Actions pipeline for deploying a Spring Boot project using Docker.
The only thing in this article that is strongly tied to the software implementation is the testing job in the created pipeline. All other aspects of creating and configuring CI / CD are universal for any projects using Docker.