Gitlab CI-CD自動化部署SpringBoot項目的方法步驟
一、概述
本文主要記錄如何通過Gitlab CI/CD自動部署SpringBoot項目jar包。
二、前期準(zhǔn)備
準(zhǔn)備三臺 CentOS7服務(wù)器,分別部署以下服務(wù):
| 序號 | 系統(tǒng) | IP | 服務(wù) |
|---|---|---|---|
| 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等) |
上述服務(wù)也可以只用一臺CentOS7,將所有程序都部署在同一機(jī)器上,但是更建議分開部署;
三、總體架構(gòu)圖

說明:
- Gitlab Server 用于部署Gitlab遠(yuǎn)程倉庫,對CPU和內(nèi)存要求比較高,建議4核CPU,4GB以上內(nèi)存;
- Runner Server 用于部署執(zhí)行.gitlab-ci.yml 文件中定義的 stage(階段);需要具有訪問 Gitlab 倉庫的權(quán)限,可以下載代碼,通過注冊方式(gitlab-runner register)實現(xiàn);
- Your Laptop Server 用戶部署你的應(yīng)用程序,這里就是SpringBoot的 jar 包,需要提前安裝 JDK 和 Maven 并配置好環(huán)境變量;
四、環(huán)境搭建
1、環(huán)境準(zhǔn)備(可選)
三臺服務(wù)器執(zhí)行以下命令:
yum -y upgrade yum -y install wget yum -y install vim
2、Gitlab安裝
參考地址:
https://about.gitlab.com/install/#centos-7
http://www.dbjr.com.cn/article/188877.htm
(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)安裝郵件服務(wù)
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配置文件指定服務(wù)器ip和自定義端口
vim /etc/gitlab/gitlab.rb
(6)重置并啟動GitLab
gitlab-ctl reconfigure gitlab-ctl restart
提示 "ok: run:"表示啟動成功
(7)訪問 GitLab頁面
如果報502,等待一段時間后再刷新試試,一般1-2分鐘左右。

本文設(shè)置的賬號:root ,新密碼:11112222
3、安裝 Runner
參考官方文檔:https://docs.gitlab.com/runner/install/linux-manually.html#install-gitlab-runner-manually-on-gnulinux 中的 Using binary file Install
(1)下載一個二進(jìn)制文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
(2)修改執(zhí)行權(quán)限
sudo chmod a+x /usr/local/bin/gitlab-runner
(3)創(chuàng)建 GitLab CI 用戶
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
(4)安裝并作為服務(wù)運(yùn)行
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
如果遇到提示 sudo: gitlab-runner: command not found,切換到 root 用戶,可以去掉 sudo 執(zhí)行上面命令。
(5)注冊 Runner
參考地址:https://docs.gitlab.com/runner/register/index.html
執(zhí)行 gitlab-runner register 命令:
[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方式,所以服務(wù)器上還需要額外多安裝docker
參考:https://zhuanlan.zhihu.com/p/54147784
#!/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
# 刪除所有舊的數(shù)據(jù)
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
# 安裝最新穩(wěn)定版本的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引擎并設(shè)置開機(jī)啟動
sudo systemctl start docker
sudo systemctl enable docker
# 配置當(dāng)前用戶對docker的執(zhí)行權(quán)限
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker
這里注冊一個全局共享的 Runner(管理員權(quán)限,復(fù)制服務(wù)器地址和 Token),所有項目都可以使用,或者也可以注冊項目級別單獨的 Runner (進(jìn)入項目 Runner 設(shè)置頁面,復(fù)制地址和 Token)。

注冊成功后,Runner 列表可以查看到注冊的 Runner

勾選:Run untagged jobs Indicates whether this runner can pick jobs without tags

4、安裝應(yīng)用服務(wù)器環(huán)境
(1)允許用戶遠(yuǎn)程登錄(可選)
vi /etc/ssh/sshd_config 修改: PasswordAuthentication yes PermitRootLogin yes 重啟服務(wù): service sshd restart
(2)安裝JDK1.8
(1)下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(2)解壓
tar -zxvf jdk-8u161-linux-x64.tar.gz 重命名: mv jdk1.8.0_161 java1.8
(3)配置環(huán)境變量
vi /etc/profile 添加以下內(nèi)容: 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)解壓
tar -zxvf apache-maven-3.3.9-bin.tar.gz 重命名: mv apache-maven-3.3.9 maven-3.3.9
(3)配置環(huán)境變量
vi /etc/profile 添加以下內(nèi)容: export MAVEN_HOME=/usr/local/maven-3.3.9 export PATH=$MAVEN_HOME/bin:$PATH 保存退出 source /etc/profile mvn -v
五、創(chuàng)建 SpringBoot 項目
1、使用Gitlab Spring 模板快速創(chuàng)建一個 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、添加環(huán)境變量(登錄應(yīng)用服務(wù)器密碼)
注: 其中 ssh_password 這個添加到環(huán)境變量中,取消勾選 Protect Branch (僅保護(hù)分支);修改和添加都是默認(rèn)勾選,需要取消,否則,其他分支不能讀取到該變量;

先在應(yīng)用服務(wù)器上創(chuàng)建一個目錄,用于上傳存放項目 jar 包:
mkdir gitlab-project
添加 .gitlab-ci.yml 文件時,可以先再 CI/CD Pipeline 中 的 CI Lint 中檢驗 .gitlab-ci.yml 文件格式
# 定義一些變量, 下面各階段會使用
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
# 定義執(zhí)行的各個階段及順序
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 包到你的應(yīng)用服務(wù)器,這里使用 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命令執(zhí)行scp和ssh命令。
如果一切順利的話,就會自動觸發(fā) CI/CD ;失敗的話查看報錯信息,可使用 Debug 模式執(zhí)行調(diào)試命令 。
[root@localhost gitlab-project]# jps 22119 Jps 22073 demo-0.0.1-SNAPSHOT.jar [root@localhost gitlab-project]# curl localhost:8080 Spring is here!

可能遇到的問題總結(jié):
- 權(quán)限問題:可以先使用 root 用戶看看是不是權(quán)限問題導(dǎo)致,如果是的話,提升執(zhí)行用戶的權(quán)限;并發(fā)問題:這里沒有修改 Runner 的并發(fā)數(shù),可以修改同時可以進(jìn)行的任務(wù)并發(fā)數(shù);其他問題:讀取不到配置的環(huán)境變量,取消勾選僅保護(hù)分支的選項;
- 未執(zhí)行job:沒有勾選未配置 tags 也執(zhí)行選項;
六、總結(jié)
使用GitLab自帶的CICD功能部署SpringBoot項目非常方便,前期環(huán)境搭建可能需要花一點時間學(xué)習(xí),但是后期部署項目可以省去很多人為操作失誤,對于小型團(tuán)隊來說,Gitlab自帶的CICD功能比Jenkins更加簡單,總體思想步驟是:
搭建GitLab服務(wù);搭建GitLab Runner服務(wù);搭建應(yīng)用服務(wù);編寫gitlab-ci.yml;
對于SpringBoot項目來說,一般分為:
(1)maven 打包;
(2)上傳jar包;
(3)啟動jar包;
到此這篇關(guān)于Gitlab CI-CD自動化部署SpringBoot項目的方法步驟的文章就介紹到這了,更多相關(guān)Gitlab CI-CD自動化部署SpringBoot 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Jmeter接口測試實戰(zhàn)-Cookies
這篇文章主要介紹了關(guān)于Jmeter接口測試實戰(zhàn)-Cookies問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼
這篇文章主要介紹了SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
RabbitMQ的Direct Exchange模式實現(xiàn)的消息發(fā)布案例(示例代碼)
本文介紹了RabbitMQ的DirectExchange模式下的消息發(fā)布和消費的實現(xiàn),詳細(xì)說明了如何在DirectExchange模式中進(jìn)行消息的發(fā)送和接收,以及消息處理的基本方法,感興趣的朋友跟隨小編一起看看吧2024-09-09
看動畫學(xué)算法之Java實現(xiàn)doublyLinkedList
這篇文章主要介紹Java實現(xiàn)doublyLinkedList,LinkedList:doublyLinkedList相對比較復(fù)雜,今天就來簡單學(xué)習(xí)一下doublyLinkedList的基本操作和概,感興趣的小伙伴可以參考下面具體文章內(nèi)容2021-10-10

