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!

How does Java alleviate platform-specific problems? Java implements platform-independent through JVM and standard libraries. 1) Use bytecode and JVM to abstract the operating system differences; 2) The standard library provides cross-platform APIs, such as Paths class processing file paths, and Charset class processing character encoding; 3) Use configuration files and multi-platform testing in actual projects for optimization and debugging.

Java'splatformindependenceenhancesmicroservicesarchitecturebyofferingdeploymentflexibility,consistency,scalability,andportability.1)DeploymentflexibilityallowsmicroservicestorunonanyplatformwithaJVM.2)Consistencyacrossservicessimplifiesdevelopmentand

GraalVM enhances Java's platform independence in three ways: 1. Cross-language interoperability, allowing Java to seamlessly interoperate with other languages; 2. Independent runtime environment, compile Java programs into local executable files through GraalVMNativeImage; 3. Performance optimization, Graal compiler generates efficient machine code to improve the performance and consistency of Java programs.

ToeffectivelytestJavaapplicationsforplatformcompatibility,followthesesteps:1)SetupautomatedtestingacrossmultipleplatformsusingCItoolslikeJenkinsorGitHubActions.2)ConductmanualtestingonrealhardwaretocatchissuesnotfoundinCIenvironments.3)Checkcross-pla

The Java compiler realizes Java's platform independence by converting source code into platform-independent bytecode, allowing Java programs to run on any operating system with JVM installed.

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.


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

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
