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!
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.
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.
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
For Debian /Ubuntu/Mint
sudo apt-get install gitlab-ci-multi-runner
For RHEL/CentOS/Fedora
sudo yum -y install gitlab-ci-multi-runner
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 service URL of gitlab
Enter the token, refer to the picture above
Enter a description for the runner
Enter optional maintenance note
Go back to the gitlab background Runner settings, refresh the page and you will see the newly added Runner:
4. Make a script! Important!!!
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 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
点进去看,出现问题
这里提示我们的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
测试接口:
问题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!