Table of Contents
1. Environmental requirements
My current service version
2. Main process
3.GitlabRunner installation and registration
3.1 Installation
Summary: There are 2 steps in total:
1. Add the gitlab official library:
2. Command installation:
3.2 Start registration
After submitting the code, how should we use Shell scripts to control the build and deployment? Gitlab has provided us with a template, which is to create a file named: .gitlab-ci.yml in the root directory of the warehouse
Create a Docker folder in the root directory,
The local idea submits the code once to the dev branch, and it can be Running
6.常见问题
Home Java javaTutorial How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

May 20, 2023 pm 06:55 PM
docker springboot runner

1. Environmental requirements

The operating systems of the following servers are all Centos7

  • Server A: Gitlab

  • Server B: GitlabRunner, Docker, docker-compose, Java1.8, maven3.6.3, git

ps: Here you can put GitlabRunner, Java1.8, maven3.6.3, git of server B It is proposed separately and deployed independently. The reason why Java is needed is maven, which is used for packaging.

For the convenience of demonstration, application server B only needs to use docker and docker-compose.

For related service construction tutorials, please refer to official documents or other third-party reliable blog tutorials!

My current service version

  • Gitlab: 14.9

  • GitlabRunner: 14.10.0

  • docker: 20.10.14

  • docker-compose: 1.29.2

  • Git: 2.31.1, check beforehand Check whether Git is the latest version, otherwise errors will occur later.

2. Main process

Our main process is to manage the code through Gitlab, and then use Gitlab's own Gitlab CI/CD function to register and bind with GitlabRunner.

Once the binding is successful, GitlabRunner will be activated every time the code is submitted and the corresponding script will be executed for automatic deployment.

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

3.GitlabRunner installation and registration

3.1 Installation

Summary: There are 2 steps in total:

1. Add the gitlab official library:

For Debian/Ubuntu/Mint

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
Copy after login

For RHEL/CentOS/Fedora

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
Copy after login

2. Command installation:

For Debian /Ubuntu/Mint

sudo apt-get install gitlab-ci-multi-runner
Copy after login

For RHEL/CentOS/Fedora

sudo yum -y install gitlab-ci-multi-runner
Copy after login

3.2 Start registration

1. Open the warehouse that Gitlab needs to automatically deploy, select Setting-》CI/CD -》Runners.

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

2. As shown below, you can see the two most critical information, which will be used when registering GitlabRunner later.

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

3.GitlabRunner registration:

Basic command:

  • Registration: gitlab-runner register

  • View registered Runners: gitlab-runner list

  • Cancel all registrations: gitlab-runner unregister --all-runners

Return to the server B. Execute the command gitlab-runner register Detailed registration process:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

  • ##Enter the GitLab instance URL (for example, https: //gitlab.com/):

    Enter the service URL of gitlab

  • Enter the registration token:

    Enter the token, refer to the picture above

  • Enter a description for the runner:

    Enter a description for the runner

  • Enter tags for the runner (comma-separated)

Enter a tag for this gitlab-runner. This tag is very important. You need to use this tag to specify gitlab-runner in the subsequent use process (yml file, the job selects the specified Runner by setting the tags label)

  • Enter optional maintenance note for the runner:

    Enter optional maintenance note

  • ##Enter an executor: docker machine, docker-ssh machine, custom, docker -windows, docker-ssh, ssh, kubernetes, docker, parallels, shell, virtualbox:
  • Press Enter to end, the config.
will be automatically generated in the installation gitlab-runner folder.

Go back to the gitlab background Runner settings, refresh the page and you will see the newly added Runner:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects4. Make a script! Important!!!

After submitting the code, how should we use Shell scripts to control the build and deployment? Gitlab has provided us with a template, which is to create a file named: .gitlab-ci.yml in the root directory of the warehouse

Here we take a springboot project as an example, and add 3 files to the original project ( .gitlab-ci.yml, Dockerfile, docker-compose.yml), the location and content of the file are shown below. Just modify the content according to your needs. You need to be familiar with some basic file structures and syntax of Dockerfile and docker-compose.yml. :

3 file location map

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects.gitlab-ci.yml

stages:
  - build
  - deploy_dev
  - clean
# 打包镜像
build:
  stage: build
  only:
    - dev
  script:
    - $MAVEN_HOME/bin/mvn clean package '-Dmaven.test.skip=true'
    - cp ./XXX/target/XXX.jar docker/
    - cd docker
    - docker build -t dev/XXX:v1.0.0 .
  tags:
    - maven
# 部署开发服务器
deploy_dev:
  stage: deploy_dev
  only:
    - dev
  script:
    - cd docker
    - docker-compose down
    - docker-compose up -d
  tags:
    - maven
#清理虚悬镜像
clean:
  stage: clean
  only:
    - dev
  script:
    - docker rmi $(docker images -q -f dangling=true)
  tags:
    - maven
Copy after login

Create a Docker folder in the root directory,

Create Dockerfile:

#FROM java:8 #java:8这个镜像体积有643MB,打包镜像后体积太大了,不建议使用
# openjdk:8-jre-alpine 体积最小,只有85MB
FROM openjdk:8-jre-alpine
# 以下2个RUN解决 java验证码接口报错的问题,
RUN echo -e "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main\n\
https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/community" > /etc/apk/repositories
RUN apk --update add curl bash ttf-dejavu && \
      rm -rf /var/cache/apk/*
COPY . /opt/app
WORKDIR /opt/app
Copy after login

Create docker-compose.yml:

version: '3.1'
services:
  education-course:
    container_name: dev-XXX-server
    restart: always
    image: dev/XXX:v1.0.0
    network_mode: "host"
    volumes:
      - /opt/XXX-server/logs:/XXX/logs
    command: java -jar -Xms1024m -Xmx1024m -Duser.timezone=GMT+08 xxx.jar --spring.profiles.active=dev
Copy after login

5. Test

The local idea submits the code once to the dev branch, and it can be Running

点进去看,出现问题

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

这里提示我们的git版本太低了

当前git版本:

[root@localhost local]# git --version
git version 1.8.3.1
Copy after login

升级最新版git后:

[root@localhost yum.repos.d]# git --version
git version 2.24.4
Copy after login

回到gitlab后台,这里手动再执行一次:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

最新的一次已通过 passed:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

查看日志的方法,进第一个build查看日志,获取项目的路径(大家有更好的方法可以告诉下我)

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

cd到 项目的docker目录,使用

docker-compose logs -ft
Copy after login

测试接口:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

6.常见问题

问题1:自动化部署build报错:

How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

解决方法,执行命令:

sudo chmod 777 /var/run/docker.sock
Copy after login

问题2:docker apline temporary error (try again later) 错误
没有dns导致的

在本机系统上创建daemon文件添加dns,然后重启docker

vim /etc/docker/daemon.json
{
"dns": ["8.8.8.8"]
}
sudo service docker restart
Copy after login

The above is the detailed content of How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

See all articles