Gitlab CI-CD自動化部署SpringBoot項目的方法步驟
本文主要記錄如何通過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 upgradeyum -y install wgetyum -y install vim2、Gitlab安裝
參考地址:https://about.gitlab.com/install/#centos-7https://www.jb51.net/article/188877.htm
(1)安裝并配置必要的依賴
sudo yum install -y curl policycoreutils-python openssh-serversudo systemctl enable sshdsudo systemctl start sshdsudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo systemctl reload firewalld
(2)安裝郵件服務
sudo yum install postfixsudo systemctl enable postfixsudo 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 reconfiguregitlab-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
(2)修改執行權限
sudo chmod a+x /usr/local/bin/gitlab-runner
(3)創建 GitLab CI 用戶
sudo useradd --comment ’GitLab Runner’ --create-home gitlab-runner --shell /bin/bash
(4)安裝并作為服務運行
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runnersudo gitlab-runner start
如果遇到提示 sudo: gitlab-runner: command not found,切換到 root 用戶,可以去掉 sudo 執行上面命令。
(5)注冊 Runner
參考地址:https://docs.gitlab.com/runner/register/index.html執行 gitlab-runner register 命令:
[root@localhost bin]# gitlab-runner registerRuntime platform arch=amd64 os=linux pid=21527 revision=4e1f20da version=13.4.0Running 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-xChSKjHPlease enter the gitlab-ci description for this runner:[localhost.localdomain]: test ci cd descPlease enter the gitlab-ci tags for this runner (comma separated):my-tag,other-tagRegistering runner... succeeded runner=PwF1sZPXPlease enter the executor: ssh, virtualbox, parallels, shell, docker-ssh, docker+machine, docker-ssh+machine, kubernetes, custom, docker:dockerPlease enter the default Docker image (e.g. ruby:2.6):maven:3.3.9-jdk-8Runner registered successfully. Feel free to start it, but if it’s running already the config should be automatically reloaded!
注:這里選擇的docker方式,所以服務器上還需要額外多安裝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# 刪除所有舊的數據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# 安裝最新穩定版本的dockersudo yum install -y docker-ce# 配置鏡像加速器sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-’EOF’{ 'registry-mirrors': ['http://hub-mirror.c.163.com']}EOF# 啟動docker引擎并設置開機啟動sudo systemctl start dockersudo systemctl enable docker# 配置當前用戶對docker的執行權限sudo groupadd dockersudo gpasswd -a ${USER} dockersudo systemctl restart docker
這里注冊一個全局共享的 Runner(管理員權限,復制服務器地址和 Token),所有項目都可以使用,或者也可以注冊項目級別單獨的 Runner (進入項目 Runner 設置頁面,復制地址和 Token)。
注冊成功后,Runner 列表可以查看到注冊的 Runner
勾選:Run untagged jobs Indicates whether this runner can pick jobs without tags
(1)允許用戶遠程登錄(可選)
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)解壓
tar -zxvf jdk-8u161-linux-x64.tar.gz重命名:mv jdk1.8.0_161 java1.8
(3)配置環境變量
vi /etc/profile添加以下內容:export JAVA_HOME=/usr/local/java1.8export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar保存退出source /etc/profilejava -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)配置環境變量
vi /etc/profile添加以下內容:export MAVEN_HOME=/usr/local/maven-3.3.9export PATH=$MAVEN_HOME/bin:$PATH保存退出source /etc/profilemvn -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 包:
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# 定義執行的各個階段及順序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 模式執行調試命令 。
[root@localhost gitlab-project]# jps22119 Jps22073 demo-0.0.1-SNAPSHOT.jar[root@localhost gitlab-project]# curl localhost:8080Spring 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包;
到此這篇關于Gitlab CI-CD自動化部署SpringBoot項目的方法步驟的文章就介紹到這了,更多相關Gitlab CI-CD自動化部署SpringBoot 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. AJAX實現省市縣三級聯動效果2. Java基于redis和mysql實現簡單的秒殺(附demo)3. 如何用python識別滑塊驗證碼中的缺口4. IntelliJ IDEA設置編碼格式的方法5. Kotlin + Flow 實現Android 應用初始化任務啟動庫6. SpringBoot+SpringCache實現兩級緩存(Redis+Caffeine)7. php讀取xml中某個元素的內容(PHP5以上才支持)8. IDEA SpringBoot 項目配置Swagger2的詳細教程9. Docker究竟是什么 為什么這么流行 它的優點和缺陷有哪些?10. ASP.NET MVC視圖頁使用jQuery傳遞異步數據的幾種方式詳解
