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

文章目录

  • 1、基础环境
  • [2、安装 Docker](#2、安装 Docker)
  • [3、安装 GitLab](#3、安装 GitLab)
  • [4、安装 JDK](#4、安装 JDK)
  • [5、安装 Maven](#5、安装 Maven)
  • [6、安装 GitLab Runner](#6、安装 GitLab Runner)
  • [7、注册 GitLab Runner](#7、注册 GitLab Runner)
  • [8、上传 GitLab](#8、上传 GitLab)
  • [9、配置 Pipeline](#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:

shell 复制代码
# 创建挂载卷
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:

shell 复制代码
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

4、安装 JDK

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

shell 复制代码
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
EOF

source /etc/profile
java -version

5、安装 Maven

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

shell 复制代码
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上,执行下述命令:

shell 复制代码
# 添加 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,内容细节在下方:

yml 复制代码
# 3个流程
stages:
  - build
  - build-image
  - run

# Step 1:Maven 打包 JAR
build:
  stage: build
  rules:
    - if: '$CI_COMMIT_TAG'
      allow_failure: false
    - when: never
  tags:
    - shared
  script:
    - mvn clean
    - mvn package
  artifacts:
    paths:
      - target/*.jar

# Step 2:Dockerfile构建镜像
build-image:
  stage: build-image
  tags:
    - shared
  rules:
    - if: '$CI_COMMIT_TAG'
      allow_failure: false
    - when: never
  script:
    - docker build -t my-project:$CI_COMMIT_TAG .

# Step 3:启动 JAR
# 注意:通常这一步会把 Docker 镜像推送到镜像仓库,这里就不这么麻烦,直接启动就好
run:
  stage: run
  rules:
    - if: '$CI_COMMIT_TAG'
      allow_failure: false
    - when: never
  tags:
    - shared
  script:
    - docker stop my-project && docker rm my-project
    - docker run --name my-project -p 8080:8080 -d my-project:$CI_COMMIT_TAG
shell 复制代码
FROM openjdk:21
ADD ./target/my-project.jar /usr/local/
CMD ["java", "-jar", "/usr/local/my-project.jar"]
EXPOSE 8080
xml 复制代码
<?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执行成功:

相关推荐
小张是铁粉1 分钟前
docker学习二天之镜像操作与容器操作
学习·docker·容器
烟雨书信14 分钟前
Docker文件操作、数据卷、挂载
运维·docker·容器
IT成长日记18 分钟前
【Docker基础】Docker数据卷管理:docker volume prune及其参数详解
运维·docker·容器·volume·prune
这儿有一堆花24 分钟前
Docker编译环境搭建与开发实战指南
运维·docker·容器
LuckyLay24 分钟前
Compose 高级用法详解——AI教你学Docker
运维·docker·容器
Uluoyu32 分钟前
redisSearch docker安装
运维·redis·docker·容器
IT成长日记5 小时前
【Docker基础】Docker数据持久化与卷(Volume)介绍
运维·docker·容器·数据持久化·volume·
热爱生活的猴子5 小时前
阿里云服务器正确配置 Docker 国内镜像的方法
服务器·阿里云·docker
物联网老王7 小时前
Ubuntu Linux Cursor 安装与使用一
linux·运维·ubuntu
FrankYoou9 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker