


How Gitlab-runner+Docker implements automatic deployment of SpringBoot projects
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.
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.
2. As shown below, you can see the two most critical information, which will be used when registering GitlabRunner later.
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:
- ##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 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.
Go back to the gitlab background Runner settings, refresh the page and you will see the newly added Runner:
4. 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
.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
点进去看,出现问题
这里提示我们的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后台,这里手动再执行一次:
最新的一次已通过 passed:
查看日志的方法,进第一个build查看日志,获取项目的路径(大家有更好的方法可以告诉下我)
cd到 项目的docker目录,使用
docker-compose logs -ft
测试接口:
6.常见问题
问题1:自动化部署build报错:
解决方法,执行命令:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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)

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 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).

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).

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

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".

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)

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]
