一、概述
本文主要记录如何通过Gitlab CI/CD自动部署SpringBoot项目jar包。
二、前期准备
准备三台 CentOS7服务器,分别部署以下服务:
序号 | 系统 | IP | 服务 |
---|---|---|---|
1 | CentOS7 | 192.168.56.10 | Gitlab |
2 | CentOS7 | 192.168.56.11 | Runner (安装Docker) |
3 | CentOS7 | 192.168.56.12 | SpringBoot 项目 jar 包(安装jdk、maven等) |
上述服务也可以只用一台CentOS7,将所有程序都部署在同一机器上,但是更建议分开部署;
三、总体架构图
说明:
- Gitlab Server 用于部署Gitlab远程仓库,对CPU和内存要求比较高,建议4核CPU,4GB以上内存;
- Runner Server 用于部署执行.gitlab-ci.yml 文件中定义的 stage(阶段);需要具有访问 Gitlab 仓库的权限,可以下载代码,通过注册方式(gitlab-runner register)实现;
- Your Laptop Server 用户部署你的应用程序,这里就是SpringBoot的 jar 包,需要提前安装 JDK 和 Maven 并配置好环境变量;
四、环境搭建
1、环境准备(可选)
三台服务器执行以下命令:
yum -y upgrade
yum -y install wget
yum -y install vim
2、Gitlab安装
参考地址:
https://about.gitlab.com/install/#centos-7
https://blog.csdn.net/duyusean/article/details/80011540
(1)安装并配置必要的依赖
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
(2)安装邮件服务
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
(3)添加 gitlab 镜像
参考地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.4.0-ce.0.el7.x86_64.rpm
(4)安装 gitlab 安装命令
rpm -i gitlab-ce-13.4.0-ce.0.el7.x86_64.rpm --nodeps --force
安装成功后图片:
(5)修改gitlab配置文件指定服务器ip和自定义端口
vim /etc/gitlab/gitlab.rb
(6)重置并启动GitLab
gitlab-ctl reconfigure
gitlab-ctl restart
提示 "ok: run:"表示启动成功
(7)访问 GitLab页面
如果报502,等待一段时间后再刷新试试,一般1-2分钟左右。
本文设置的账号:root ,新密码:11112222
3、安装 Runner
参考官方文档:https://docs.gitlab.com/runner/install/linux-manually.html#install-gitlab-runner-manually-on-gnulinux 中的 Using binary file Install
(1)下载一个二进制文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64