Home > Java > javaTutorial > Gitlab CI-CD method to automatically deploy SpringBoot projects

Gitlab CI-CD method to automatically deploy SpringBoot projects

PHPz
Release: 2023-05-11 17:31:16
forward
2227 people have browsed it

1. Overview

This article mainly records how to automatically deploy the SpringBoot project jar package through Gitlab CI/CD.

2. Early preparation

Prepare three CentOS7 servers and deploy the following services respectively:

Serial number System IP Service
1 CentOS7 192.168.56.10 Gitlab
2 CentOS7 192.168.56.11 Runner (install Docker)
3 CentOS7 192.168.56.12 SpringBoot project jar package (install jdk, maven, etc.)

The above services can also use only one CentOS7 and deploy all programs on the same machine, but it is more recommended to deploy them separately;

3. Overall architecture diagram

Gitlab CI-CD自动化部署SpringBoot项目的方法

Description:

  • Gitlab Server is used to deploy Gitlab remote warehouse. It has high CPU and memory requirements. It is recommended to have a 4-core CPU and more than 4GB of memory;

  • Runner Server is used to deploy and execute the stage defined in the .gitlab-ci.yml file; you need to have permission to access the Gitlab warehouse, you can download the code and implement it through registration (gitlab-runner register) ;

  • Your Laptop Server user deploys your application, here is the SpringBoot jar package, you need to install JDK and Maven in advance and configure the environment variables;

4. Environment setup

1. Environment preparation (optional)

Execute the following commands on three servers:

yum -y upgrade
yum -y install wget
yum -y install vim
Copy after login

2. Gitlab installation

Reference address:
https://about.gitlab.com/install/#centos-7
https://www.xieniao.com/article/188877.htm

(1) Install and configure necessary dependencies

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
Copy after login

(2) Install mail service

sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
Copy after login

(3) Add gitlab mirror

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.4.0-ce.0.el7.x86_64.rpm
Copy after login

(4) Install gitlab installation command

rpm -i gitlab-ce-13.4.0-ce.0.el7.x86_64.rpm --nodeps --force
Copy after login

Picture after successful installation:

Gitlab CI-CD自动化部署SpringBoot项目的方法

(5) Modify the gitlab configuration file to specify the server IP and custom port

vim  /etc/gitlab/gitlab.rb
Copy after login

(6) Reset and start GitLab

gitlab-ctl reconfigure
gitlab-ctl restart
Copy after login

The prompt "ok: run:" indicates successful startup

(7) Visit the GitLab page

If 502 is reported, wait for a while Try refreshing after a certain amount of time, usually about 1-2 minutes.

Gitlab CI-CD自动化部署SpringBoot项目的方法

The account set in this article: root, new password: 11112222

3. Install Runner

(1) Download one Binary file

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
Copy after login

(2) Modify execution permissions

sudo chmod a+x /usr/local/bin/gitlab-runner
Copy after login

(3) Create GitLab CI user

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
Copy after login

(4) Install and run as a service

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
Copy after login

If you encounter the prompt sudo: gitlab-runner: command not found, switch to the root user, you can remove sudo and execute the above command.

(5) Register Runner

Reference address: https://docs.gitlab.com/runner/register/index.html
Execute gitlab-runner register command:

[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!
Copy after login

Note: The docker method is selected here, so additional docker needs to be installed on the server

#!/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 <<-&#39;EOF&#39;
{
  "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
Copy after login

Register a globally shared Runner here (administrator Permissions, copy server address and Token), can be used by all projects, or you can register a separate Runner at the project level (enter the project Runner settings page, copy the address and Token).

Gitlab CI-CD自动化部署SpringBoot项目的方法

After successful registration, the registered Runner can be viewed in the Runner list

Gitlab CI-CD自动化部署SpringBoot项目的方法

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

Gitlab CI-CD自动化部署SpringBoot项目的方法

4. Install the application server environment

(1) Allow users to log in remotely (Optional)

vi /etc/ssh/sshd_config
修改:
PasswordAuthentication yes                      
PermitRootLogin yes

重启服务:
service sshd restart
Copy after login

(2) Install JDK1.8

(2) Decompress

tar -zxvf jdk-8u161-linux-x64.tar.gz
重命名:
mv jdk1.8.0_161 java1.8
Copy after login

(3) Configure environment variables

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
Copy after login

(3 ) Install Maven3.3.9

(2) Unzip

tar -zxvf apache-maven-3.3.9-bin.tar.gz 

重命名:
mv apache-maven-3.3.9 maven-3.3.9
Copy after login

(3) Configure environment variables

vi /etc/profile

添加以下内容:
export MAVEN_HOME=/usr/local/maven-3.3.9
export PATH=$MAVEN_HOME/bin:$PATH
保存退出

source /etc/profile
mvn -v
Copy after login

5. Create SpringBoot project

1. Use Gitlab Spring template quickly creates a SpringBoot project;

Gitlab CI-CD自动化部署SpringBoot项目的方法

Gitlab CI-CD自动化部署SpringBoot项目的方法

If an error is reported, delete this line in pom.xml

If you report this error:
[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

Modified version
1.5.9 .RELEASE

2、添加环境变量(登录应用服务器密码)

注: 其中 ssh_password 这个添加到环境变量中,取消勾选 Protect Branch (仅保护分支);修改和添加都是默认勾选,需要取消,否则,其他分支不能读取到该变量;

Gitlab CI-CD自动化部署SpringBoot项目的方法

先在应用服务器上创建一个目录,用于上传存放项目 jar 包:

mkdir gitlab-project
Copy after login

添加 .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 &"
Copy after login

这里使用了DockerHub上面的一个公共镜像(ictu/sshpass),主要是想使用启动自带的sshpass命令执行scp和ssh命令。
如果一切顺利的话,就会自动触发 CI/CD ;失败的话查看报错信息,可使用 Debug 模式执行调试命令 。

[root@localhost gitlab-project]# jps
22119 Jps
22073 demo-0.0.1-SNAPSHOT.jar
[root@localhost gitlab-project]# curl localhost:8080
Spring is here!
Copy after login

Gitlab CI-CD自动化部署SpringBoot项目的方法

可能遇到的问题总结:

  1. 权限问题:可以先使用 root 用户看看是不是权限问题导致,如果是的话,提升执行用户的权限;并发问题:这里没有修改 Runner 的并发数,可以修改同时可以进行的任务并发数;其他问题:读取不到配置的环境变量,取消勾选仅保护分支的选项;

  2. 未执行job:没有勾选未配置 tags 也执行选项;

The above is the detailed content of Gitlab CI-CD method to automatically deploy SpringBoot projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template