一、概述
本文主要记录如何通过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、环境准备(可选)
三台服务器执行以下命令:
|-------|---------------------------------------------------------------------|
| 1 2 3 | yum -y upgrade
yum -y ``install
wget
yum -y ``install
vim
|
2、Gitlab安装
参考地址:
https://about.gitlab.com/install/#centos-7
CentOS8.1搭建Gitlab服务器详细教程_Linux_脚本之家
(1)安装并配置必要的依赖
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | 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)安装邮件服务
|-------|---------------------------------------------------------------------------------------------------------|
| 1 2 3 | sudo
yum ``install
postfix
sudo
systemctl ``enable
postfix
sudo
systemctl start postfix
|
(3)添加 gitlab 镜像
参考地址:Index of /gitlab-ce/yum/el7/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
|---|------------------------------------------------------------------------------------------------------------|
| 1 | wget https:``//mirrors``.tuna.tsinghua.edu.cn``/gitlab-ce/yum/el7/gitlab-ce-13``.4.0-ce.0.el7.x86_64.rpm
|
(4)安装 gitlab 安装命令
|---|----------------------------------------------------------------|
| 1 | rpm -i gitlab-ce-13.4.0-ce.0.el7.x86_64.rpm --nodeps --force
|
安装成功后图片:
(5)修改gitlab配置文件指定服务器ip和自定义端口
|---|---------------------------------|
| 1 | vim ``/etc/gitlab/gitlab``.rb
|
(6)重置并启动GitLab
|-----|-----------------------------------------------|
| 1 2 | 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)下载一个二进制文件
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 | sudo
curl -L --output ``/usr/local/bin/gitlab-runner
https:``//gitlab-runner-downloads``.s3.amazonaws.com``/latest/binaries/gitlab-runner-linux-amd64
|
(2)修改执行权限
|---|-----------------------------------------------------|
| 1 | sudo
chmod
a+x ``/usr/local/bin/gitlab-runner
|
(3)创建 GitLab CI 用户
|---|--------------------------------------------------------------------------------------------------|
| 1 | sudo
useradd
--comment ``'GitLab Runner'
--create-home gitlab-runner --shell ``/bin/bash
|
(4)安装并作为服务运行
|-----|--------------------------------------------------------------------------------------------------------------------------------|
| 1 2 | sudo
gitlab-runner ``install
--user=gitlab-runner --working-directory=``/home/gitlab-runner
sudo
gitlab-runner start
|
如果遇到提示 sudo: gitlab-runner: command not found,切换到 root 用户,可以去掉 sudo 执行上面命令。
(5)注册 Runner
参考地址:Registering runners | GitLab
执行 gitlab-runner register 命令:
|-------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@localhost bin]``# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=21527 revision=4e1f20da version=13.4.0
Running ``in
system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https:``//gitlab``.com/):
http:``//192``.168.56.10/
Please enter the gitlab-ci token ``for
this runner:
PwF1sZPX_zsB-xChSKjH
Please enter the gitlab-ci description ``for
this runner:
[localhost.localdomain]: ``test
ci ``cd
desc
Please enter the gitlab-ci tags ``for
this runner (comma separated):
my-tag,other-tag
Registering runner... succeeded runner=PwF1sZPX
Please enter the executor: ``ssh``, virtualbox, parallels, shell, docker-``ssh``, docker+machine, docker-``ssh``+machine, kubernetes, custom, docker:
docker
Please enter the default Docker image (e.g. ruby:2.6):
maven:3.3.9-jdk-8
Runner registered successfully. Feel ``free
to start it, but ``if
it's running already the config should be automatically reloaded!
|
注:这里选择的docker方式,所以服务器上还需要额外多安装docker
|----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #!/bin/bash
# 移除掉旧的版本
sudo
yum remove docker \
``docker-client \
``docker-client-latest \
``docker-common \
``docker-latest \
``docker-latest-logrotate \
``docker-logrotate \
``docker-selinux \
``docker-engine-selinux \
``docker-engine
# 删除所有旧的数据
sudo
rm
-rf ``/var/lib/docker
# 安装依赖包
sudo
yum ``install
-y yum-utils \
``device-mapper-persistent-data \
``lvm2
# 添加源,使用了阿里云镜像
sudo
yum-config-manager \
``--add-repo \
``http:``//mirrors``.aliyun.com``/docker-ce/linux/centos/docker-ce``.repo
# 配置缓存
sudo
yum makecache fast
# 安装最新稳定版本的docker
sudo
yum ``install
-y docker-ce
# 配置镜像加速器
sudo
mkdir
-p ``/etc/docker
sudo
tee
/etc/docker/daemon``.json <<-``'EOF'
{
``"registry-mirrors"``: [``"http://hub-mirror.c.163.com"``]
}
EOF
# 启动docker引擎并设置开机启动
sudo
systemctl start docker
sudo
systemctl ``enable
docker
# 配置当前用户对docker的执行权限
sudo
groupadd docker
sudo
gpasswd -a ${USER} docker
sudo
systemctl restart docker
|
这里注册一个全局共享的 Runner(管理员权限,复制服务器地址和 Token),所有项目都可以使用,或者也可以注册项目级别单独的 Runner (进入项目 Runner 设置页面,复制地址和 Token)。
注册成功后,Runner 列表可以查看到注册的 Runner
勾选:Run untagged jobs Indicates whether this runner can pick jobs without tags
4、安装应用服务器环境
(1)允许用户远程登录(可选)
|---------------|-------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | vi
/etc/ssh/sshd_config
修改:
PasswordAuthentication ``yes
PermitRootLogin ``yes
重启服务:
service sshd restart
|
(2)安装JDK1.8
(1)下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(2)解压
|-------|----------------------------------------------------------------------------------|
| 1 2 3 | tar
-zxvf jdk-8u161-linux-x64.``tar``.gz
重命名:
mv
jdk1.8.0_161 java1.8
|
(3)配置环境变量
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 | vi
/etc/profile
添加以下内容:
export
JAVA_HOME=``/usr/local/java1``.8
export
PATH=$JAVA_HOME``/bin``:$PATH
export
CLASSPATH=.:$JAVA_HOME``/lib/dt``.jar:$JAVA_HOME``/lib/tools``.jar
保存退出
source
/etc/profile
java -version
|
(3) 安装 Maven3.3.9
(1)下载地址:http://maven.apache.org/download.cgi
(2)解压
|---------|------------------------------------------------------------------------------------------------|
| 1 2 3 4 | tar
-zxvf apache-maven-3.3.9-bin.``tar``.gz
重命名:
mv
apache-maven-3.3.9 maven-3.3.9
|
(3)配置环境变量
|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | vi
/etc/profile
添加以下内容:
export
MAVEN_HOME=``/usr/local/maven-3``.3.9
export
PATH=$MAVEN_HOME``/bin``:$PATH
保存退出
source
/etc/profile
mvn -``v
|
五、创建 SpringBoot 项目
1、使用Gitlab Spring 模板快速创建一个 SpringBoot 项目;
如果报错,删除pom.xml中的这行
报这个错的话:
[FATAL] Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out (Connection timed out) and 'parent.relativePath' points at wrong local POM @ line 14, column 10
修改版本
1.5.9.RELEASE
2、添加环境变量(登录应用服务器密码)
注: 其中 ssh_password 这个添加到环境变量中,取消勾选 Protect Branch (仅保护分支);修改和添加都是默认勾选,需要取消,否则,其他分支不能读取到该变量;
先在应用服务器上创建一个目录,用于上传存放项目 jar 包:
|---|--------------------------|
| 1 | mkdir
gitlab-project
|
添加 .gitlab-ci.yml 文件时,可以先再 CI/CD Pipeline 中 的 CI Lint 中检验 .gitlab-ci.yml 文件格式
|-------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # 定义一些变量, 下面各阶段会使用
variables:
``server_ip: 192.168.56.12
``jar_name: demo-0.0.1-SNAPSHOT.jar
``java_path: ``/usr/local/java1``.8``/bin
``upload_path: ``/usr/local/gitlab-project
# 定义执行的各个阶段及顺序
stages:
``- build
``- upload
``- deploy
# 使用 maven 镜像打包项目
maven-build:
``stage: build
``image: maven:3.5.0-jdk-8
``script:
``- mvn package -B -Dmaven.``test``.skip=``true
``cache:
``key: m2-repo
``paths:
``- .m2``/repository
``artifacts:
``paths:
``- target/$jar_name
# 上传生成的 jar 包到你的应用服务器,这里使用 ictu/sshpass 这个镜像,是为了使用 sshpass 命令
upload-jar:
``stage: upload
``image: ictu``/sshpass
``script:
``- ``ls
-l target/
``- sshpass -p $ssh_password ``scp
-o UserKnownHostsFile=``/dev/null
-o StrictHostKeyChecking=no target/$jar_name root@$server_ip:$upload_path/$jar_name
# 启动 SpringBoot jar包
deploy-``test``:
``stage: deploy
``image: ictu``/sshpass
``script:
``- sshpass -p $ssh_password ``ssh
-o UserKnownHostsFile=``/dev/null
-o StrictHostKeyChecking=no root@$server_ip ``"nohup $java_path/java -jar $upload_path/$jar_name >/dev/null 2>&1 &"
|
这里使用了DockerHub上面的一个公共镜像(ictu/sshpass),主要是想使用启动自带的sshpass命令执行scp和ssh命令。
如果一切顺利的话,就会自动触发 CI/CD ;失败的话查看报错信息,可使用 Debug 模式执行调试命令 。
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | [root@localhost gitlab-project]``# jps
22119 Jps
22073 demo-0.0.1-SNAPSHOT.jar
[root@localhost gitlab-project]``# curl localhost:8080
Spring is here!
|
可能遇到的问题总结:
- 权限问题:可以先使用 root 用户看看是不是权限问题导致,如果是的话,提升执行用户的权限;并发问题:这里没有修改 Runner 的并发数,可以修改同时可以进行的任务并发数;其他问题:读取不到配置的环境变量,取消勾选仅保护分支的选项;
- 未执行job:没有勾选未配置 tags 也执行选项;
六、总结
使用GitLab自带的CICD功能部署SpringBoot项目非常方便,前期环境搭建可能需要花一点时间学习,但是后期部署项目可以省去很多人为操作失误,对于小型团队来说,Gitlab自带的CICD功能比Jenkins更加简单,总体思想步骤是:
搭建GitLab服务;搭建GitLab Runner服务;搭建应用服务;编写gitlab-ci.yml;
对于SpringBoot项目来说,一般分为:
(1)maven 打包;
(2)上传jar包;
(3)启动jar包;