GitLab Runner 通过 Pipeline 流水线实现持续集成 CI

news/2024/10/22 11:00:05/

文章目录

  • 1、基础环境
  • 2、安装 Docker
  • 3、安装 GitLab
  • 4、安装 JDK
  • 5、安装 Maven
  • 6、安装 GitLab Runner
  • 7、注册 GitLab Runner
  • 8、上传 GitLab
  • 9、配置 Pipeline

1、基础环境

本次演示搭建,我使用的是阿里云服务器,配置如下:

  • 服务器1:
    • 配置:4核8G Ubuntu 22.04
    • 内网IP:172.16.0.182
    • 公网IP:8.141.25.194
    • 软件:GitLab
  • 服务器2:
    • 配置:4核8G Ubuntu 22.04
    • 内网IP:172.16.0.183
    • 公网IP:8.141.25.50
    • 软件:GitLab Runner + Maven 3.8.4 + JDK 21

部署架构图如下:
在这里插入图片描述

2、安装 Docker

两台服务器上,都需要安装好 Docker 环境,参考该链接:
https://blog.csdn.net/weixin_46594796/article/details/142757626

3、安装 GitLab

在服务器1上,执行下述命令安装Gitlab:

# 创建挂载卷
docker volume create gitlab-etc
docker volume create gitlab-log
docker volume create gitlab-opt# 安装Gitlab
# hostname按理应使用内网ip,我用公网ip因为阿里云内网ip无法和我电脑本地通信,虚拟机就没这个烦恼
docker run --name gitlab \
--hostname 8.141.25.194 \
--restart=always \
-p 80:80 \
-p 443:443 \
-v gitlab-etc:/etc/gitlab \
-v gitlab-log:/var/log/gitlab \
-v gitlab-opt:/var/opt/gitlab \
-d gitlab/gitlab-ce

GitLab安装完毕后,需要通过执行下述命令查看Gitlab登陆密码,默认账号是root:

sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

在这里插入图片描述

4、安装 JDK

在服务器2上,执行下述命令:

cd /usr/local
# https://www.oracle.com/java/technologies/downloads/#java21
wget https://download.java.net/java/GA/jdk21.0.1/415e3f918a1f4062a0074a2794853d0d/12/GPL/openjdk-21.0.1_linux-x64_bin.tar.gz -O openjdk-21.0.1_linux-x64_bin.tar.gz
tar -xzvf openjdk-21.0.1_linux-x64_bin.tar.gz
cat >> /etc/profile <<-'EOF'
export JAVA_HOME=/usr/local/jdk-21.0.1
export PATH=$PATH:$JAVA_HOME/bin
EOFsource /etc/profile
java -version

5、安装 Maven

在服务器2上,执行下述命令:

cd /usr/local
wget --no-check-certificate wget  https://xuzhibin-bucket.oss-cn-beijing.aliyuncs.com/devops/apache-maven-3.8.6-bin.tar.gz
tar -xzvf apache-maven-3.8.6-bin.tar.gz
cat >> /etc/profile <<-'EOF'
export PATH=$PATH:/usr/local/apache-maven-3.8.6/bin
EOF
source /etc/profile
mvn -v

6、安装 GitLab Runner

在服务器2上,执行下述命令:

# 添加 GitLab Runner 仓库地址
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash# 安装 GitLab Runner
sudo apt-get install -y gitlab-runner# 查看所有用户
cut -d: -f1 /etc/passwd# 删除gitlab-runner
sudo gitlab-runner uninstall # 安装并设置--user(设置为root)
gitlab-runner install --working-directory /home/gitlab-runner --user root # 重启gitlab-runner
systemctl daemon-reload
systemctl restart gitlab-runner 
systemctl enable gitlab-runner

7、注册 GitLab Runner

在 GitLab 页面上,进行按照下图进行点击:
在这里插入图片描述
将下图中的命令,在服务器2上运行,进行注册操作:
在这里插入图片描述
按照下图方式进行输入、Enter操作:
在这里插入图片描述
最后可以看到注册成功了!
在这里插入图片描述

8、上传 GitLab

首先,在GitLab上创建仓库:
在这里插入图片描述
然后,将测试项目上传到GitLab仓库中:
在这里插入图片描述
这样就可以在Gitlab Master分支上看到刚上传的项目了:
在这里插入图片描述
本次测试项目结构如图:
在这里插入图片描述
关键的配置文件.gitlab-ci.yml、Dockerfile、pom.xml,内容细节在下方:

# 3个流程
stages:- build- build-image- run# Step 1:Maven 打包 JAR
build:stage: buildrules:- if: '$CI_COMMIT_TAG'allow_failure: false- when: nevertags:- sharedscript:- mvn clean- mvn packageartifacts:paths:- target/*.jar# Step 2:Dockerfile构建镜像
build-image:stage: build-imagetags:- sharedrules:- if: '$CI_COMMIT_TAG'allow_failure: false- when: neverscript:- docker build -t my-project:$CI_COMMIT_TAG .# Step 3:启动 JAR
# 注意:通常这一步会把 Docker 镜像推送到镜像仓库,这里就不这么麻烦,直接启动就好
run:stage: runrules:- if: '$CI_COMMIT_TAG'allow_failure: false- when: nevertags:- sharedscript:- docker stop my-project && docker rm my-project- docker run --name my-project -p 8080:8080 -d my-project:$CI_COMMIT_TAG
FROM openjdk:21
ADD ./target/my-project.jar /usr/local/
CMD ["java", "-jar", "/usr/local/my-project.jar"]
EXPOSE 8080
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.9</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>my-project</artifactId><version>0.0.1-SNAPSHOT</version><name>my-project</name><description>my-project</description><url/><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>my-project</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

9、配置 Pipeline

Pipeline 流水线触发是通过 Tag 标签来实现,所以按照下图操作创建 Tag:
在这里插入图片描述
在这里插入图片描述
最后,出现下图这个情况,就说明整个Pipeline执行成功:
在这里插入图片描述在这里插入图片描述


http://www.ppmy.cn/news/1538388.html

相关文章

SpringSecirity(四)——用户退出

因为JWT是无状态的&#xff0c;去中心化的&#xff0c;在服务器端无法清除&#xff0c;服务器一旦进行颁发&#xff0c;就只能等待自动过期 才会失效&#xff0c;所以需要redis配合才能完成登录状态的记录。 实现思路&#xff1a; 登录后在redis中添加一个白名单&#xff0c;把…

LeetCode23. 合并 K 个升序链表(2024秋季每日一题 36)

给你一个链表数组&#xff0c;每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中&#xff0c;返回合并后的链表。 示例 1&#xff1a; 输入&#xff1a;lists [[1,4,5],[1,3,4],[2,6]] 输出&#xff1a;[1,1,2,3,4,4,5,6] 解释&#xff1a;链表数组如下&#…

GS-LRM: Large Reconstruction Modelfor 3D Gaussian Splatting 论文解读

目录 一、概述 二、相关工作 1、多视图的三维重建 2、前馈重建 三、LRM 1、编码器 2、解码器 3、NeRF渲染 四、GS-LRM 1、输入处理 2、Transformer 3、损失函数 五、实验 六、局限 一、概述 该论文提出了一种利用稀疏输入图像高效预测3D高斯原语的方法&#xff…

Qt C++设计模式->中介者模式

中介者模式&#xff08;Mediator Pattern&#xff09;是一种行为型设计模式&#xff0c;定义了一个对象用于封装一系列对象之间的交互。中介者使得对象之间不再需要显式地相互引用&#xff0c;减少了对象之间的依赖关系&#xff0c;从而使系统更加松散耦合&#xff0c;并且可以…

前端框架对比与选择:详尽分析

1. 引言 随着互联网技术的飞速发展,前端开发技术也得到了迅猛提升。无论是大型企业还是中小型开发团队,使用前端框架来简化开发过程、提升开发效率已成为一种普遍现象。如今,市场上有众多的前端框架可供选择,如React、Vue.js、Angular等,如何在这些框架中进行选择成为了开…

牛客编程初学者入门训练——BC8 牛牛的字符菱形

BC8 牛牛的字符菱形 描述&#xff1a; 牛牛尝试用键盘读入一个字符&#xff0c;然后在屏幕上显示一个用这个字符填充的对角线长5个字符&#xff0c;倾斜放置的菱形。 输入描述&#xff1a; 输入一个char类型字符 输出描述&#xff1a; 输出一个用这个字符填充的对角线长5…

【ShuQiHere】 K-means 聚类算法详解:公式、代码与实战

&#x1f9e0; 【ShuQiHere】 &#x1f393; 目录 &#x1f4dc; K-means 简介K-means 工作原理理论基础 3.1 目标函数3.2 距离度量 K-means 算法步骤 4.1 具体代码实现 K-means 的优缺点如何选择正确的 K 值K-means 的改进与变体案例分析&#xff1a;如何使用 K-means 聚类&…

HTML,JavaScript,PHP,CSS,XML,SQL的区别和联习

HTML超文本标记语言——HyperText Markup Language&#xff09;是构成 Web 世界的一砖一瓦。它定义了网页内容的含义和结构。除 HTML 以外的其他技术则通常用来描述一个网页的表现与展示效果&#xff08;如 CSS&#xff09;&#xff0c;或功能与行为&#xff08;如 JavaScript&…