Home  >  Article  >  Java  >  How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects

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

王林
王林forward
2023-05-20 18:55:201796browse

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

For RHEL/CentOS/Fedora

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

2. Command installation:

For Debian /Ubuntu/Mint

sudo apt-get install gitlab-ci-multi-runner

For RHEL/CentOS/Fedora

sudo yum -y install gitlab-ci-multi-runner

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

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

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

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

升级最新版git后:

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

回到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

测试接口:

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

问题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

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:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete