CI/CD:如何使用 GitLab 执行 SpringBoot 前后端分离项目的持续集成与持续交付(持续部署)?
GitLab Runner 是开源的且用 Go 编写,它可以作为单个二进制文件运行且没有特定于编程语言的要求。
文章共2,272字 · 阅读需要大约8分钟
一键AI生成摘要,助你高效阅读
问答
·
文章目录
个人记录
1. GitLab Runner 安装
官网各系统 & 各方式安装说明:https://docs.gitlab.com/runner/install/
本文基于阿里云 CentOS 安装 GitLab Runner (Linux Shell 方式,非 Docker 方式)
1.1 GitLab Runner 介绍
GitLab Runner 是开源的且用 Go 编写,它可以作为单个二进制文件运行且没有特定于编程语言的要求。
1.2 Install GitLab Runner in CentOS7
- CURL 拉取 RPM
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash
- 安装:y n 连
yum install gitlab-runner
1.3 Runner 注册
-
注册 Runner
- 官网注册说明:https://docs.gitlab.com/runner/register/index.html
- Install it on a server separate than where GitLab is installed(GitLabRunner 与 GitLab 安装在不同位置):Done
- Obtain a token(获取一个 token):https://docs.gitlab.com/ee/ci/runners/register_runner.html
- 获取 token:For a project runner (针对某个项目的 runner)
-
找到某个项目(以 SpringBoot 后端项目为例):在项目主页点击 设置(Settings)-CI/CD,选择
New project runner
-
选择 OS,填写 runner 信息
-
点击上图 Create runner
-
- 注册 Runner:下图序上,根据下面截图的步骤处理即可
- 第一步:复制粘贴如下命令到安装了 Runner 的命令行以注册 runner
- 第二步:选择 Runner 执行器
- 第三步:手动确认 runner 可以拉取 jobs (pick up jobs):出现警告,检查 .ssh 配置公钥到 gitlab(见附录)
- 官网注册说明:https://docs.gitlab.com/runner/register/index.html
-
去 runner 页:查看已配置的项目 runner
2. CI/CD:SpringBoot 后端项目部署
- 在 Gitlab Runner 所在服务器上安装 Runner 环境:略
- git 安装(git clone 仓库项目记得 SSH 免密登录配置,在本文最后一部分有 SSH 免密登录配置)
- maven 安装
- 在需部署 SpringBoot 后端项目的服务器上(本文后端服务器与 Runner 所在服务器一致)安装 JDK:略
- 在 Gitlab 的 SpringBoot 后端项目 CI/CD 处创建项目 runner (1.3 已完成)
- 在后端项目根目录下编写
.gitlab-ci.yml
文件 (gitlab ),内容如下
variables:
server: 127.0.0.1
appPath: apps
jarPath: oldp/api/target/api-0.0.1-SNAPSHOT.jar
stages: # List of stages for jobs, and their order of execution
- build
- test
- deploy
# project build
build: # This job runs in the build stage, which runs first.
stage: build
tags: # 指定要运行的 runner,指定的是 runner 的 tag
- master
script:
- echo "Packaging project..."
- echo $CI_BUILDS_DIR
# - mvn clean # 清理
- mvn -o -Dmaven.test.skip=true package # -Dmaven.test.skip=true:打包(跳过测试);-o:离线
- pwd
- mkdir -p /home/gitlab-runner/$appPath
- cp -rf ../ /home/gitlab-runner/$appPath
- echo "Package complete!"
# test
test: # This job runs in the test stage. # 没有指定要运行的 runner,就在可以运行的 runner 上选择一台运行
stage: test # It only starts when the job in the build stage completes successfully.
# tags:
# - master
needs:
- build
script:
- echo "Running unit tests... This will take about 30 seconds."
- sleep 10
- echo "Code coverage is 90%"
# deploy
deploy: # This job runs in the deploy stage.
stage: deploy
tags:
- master
needs:
- test
script:
- set +e # set -e 表示有报错即退出, set +e [errexit ]表示关闭这种设置
- kill -9 $(ps -aux | grep api | grep -v grep | awk '{print $2}') # could be: kill -9, without pid: kill usage;
# - $(netstat -nap | grep 12313 | awk '{print $7}' | awk -F "/" '{print $1}') # (No info could be read for "-p": geteuid()=996 but you should be root.)
- echo "Deploying application..."
- echo /home/gitlab-runner/$appPath/$jarPath
- java -Xms64m -Xmx128m -jar /home/gitlab-runner/$appPath/$jarPath > ./oldp.log 2>&1 & # 2>&1:标准错误输出重定向到标准输出;&:后台运行
- echo "Application successfully deployed."
- Commit 代码,触发流水线,部署成功
Done !
3. CI/CD:Vue 前端项目部署
- 在 Gitlab Runner 所在服务器上安装 Runner 环境
- node.js 安装:yum install -y nodejs npm (非离线安装)
- vue 安装:npm install -g vue-cli(若使用 cnpm,先安装 cnpm:npm install -g cnpm --registry=https://registry.npm.taobao.org
)
- 在 Gitlab 的 Vue 前端仓库 CI/CD 处创建项目 runner (同 1.3)
- 在前端项目根目录下编写
.gitlab-ci.yml
文件 (gitlab ),内容如下
variables:
srcServer: 127.0.0.1 # the server where the gitlab-runner is at
desServer: 127.0.0.1 # the server where this project to be deployed
desPath: http_server_html_storage_path
stages:
- install
- build
- deploy
install: # 依赖安装
stage: install
script:
- echo "Installing the dependency..."
- pwd
- rm -rf node_modules
- npm install # --offline:离线模式
- echo "Installed!"
build: # 前端文件编译
stage: build
needs:
- install
script:
- echo "Building application..."
- pwd
- npm run build
- echo "Builded!"
deploy: # 前端部署
stage: deploy
needs:
- build
script:
- echo "Deploying application..."
- mv -rf ../ /home/gitlab-runner/apps
- scp -rf -P 22 /home/gitlab-runner/apps/oldp-web/dist root@$desServer:$desPath
- echo "Application successfully deployed."
- Commit 代码,触发流水线,服务器性能原因,npm install 未完成就被 kill (CPU 彪满,哎),暂时没条件验证上面的 .yml
Done !
X. SSH 免密登录:解决 502 Bad Gateway (Gitlab-Runner 服务器请求 Gitlab 服务器失败)
本部分用于解决出现的如下问题:
Runner WARNING:Checking for jobs... failed runner=rPbMnmqED status=502 Bad Gateway
即 Gitlab-Runner 所处的服务器请求 Gitlab 服务器失败(本文 Gitlab 服务器没有私有化部署,请求的就是 Gitlab 官方服务器)
解决:502 Bad Gateway。
- 生成 SSH 公钥:若有,则可以不执行
- 复制 id_rsa.pub 的内容到 gitlab 并点击 add key
- 重新执行 gitlab-runner run
更多推荐
已为社区贡献1条内容
所有评论(0)