From 47199347b47a8ab5e62a5b7bd9a0102f3eb7822d Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Sat, 17 May 2025 20:17:32 +0800 Subject: [PATCH] =?UTF-8?q?:green=5Fheart:=20feat:=20=E6=B7=BB=E5=8A=A0git?= =?UTF-8?q?lab=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 49 ++++++ auth-api/Dockerfile => Dockerfile | 0 Gitlab安装.md | 256 ++++++++++++++++++++++++++++++ 3 files changed, 305 insertions(+) create mode 100644 .gitlab-ci.yml rename auth-api/Dockerfile => Dockerfile (100%) create mode 100644 Gitlab安装.md diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ade5a0d --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,49 @@ +# 定义CI/CD流水线的阶段 +stages: + - build # 第一阶段:构建应用程序 + - build-docker # 第二阶段:构建Docker镜像 + - deploy # 第三阶段:部署应用程序 + +# 定义全局变量 +variables: + CONTAINER_NAME: "bunny-auth-server" # Docker容器名称 + DOCKER_TAG: "4.0.0" # Docker镜像标签版本 + +# 构建任务 +build-job: + stage: build # 指定此任务属于build阶段 + script: + # 打印编译开始信息 + - echo "Compiling the code..." + # 使用Maven编译Java项目,跳过测试 + - mvn clean package -DskipTests + # 打印编译完成信息 + - echo "Compile complete." + # 从Docker Hub拉取OpenJDK基础镜像 + - docker pull openjdk:24-ea-17-jdk-oraclelinux9 + # 打印拉取完成信息 + - echo "docker pull complete." + # 使用Dockerfile构建Docker镜像,并打上标签 + - docker build -f Dockerfile -t $CONTAINER_NAME:$DOCKER_TAG . + # 打印构建成功信息 + - echo "Application successfully deployed." + +# 部署任务 +deploy-job: + stage: deploy # 指定此任务属于deploy阶段 + environment: production # 指定部署环境为production + script: + # 打印部署开始信息 + - echo "Deploying application..." + # 停止正在运行的容器(如果存在),|| true确保命令失败不会中断脚本 + - docker stop $CONTAINER_NAME || true + # 删除容器(如果存在) + - docker rm $CONTAINER_NAME || true + # 运行新的Docker容器 + # -d: 后台运行 + # -p: 端口映射(7070和8000) + # --name: 容器名称 + # --restart always: 总是自动重启 + - docker run -d -p 7070:7070 -p 8000:8000 --name $CONTAINER_NAME --restart always $CONTAINER_NAME:$DOCKER_TAG + # 打印部署成功信息 + - echo "Application successfully deployed." \ No newline at end of file diff --git a/auth-api/Dockerfile b/Dockerfile similarity index 100% rename from auth-api/Dockerfile rename to Dockerfile diff --git a/Gitlab安装.md b/Gitlab安装.md new file mode 100644 index 0000000..aafa181 --- /dev/null +++ b/Gitlab安装.md @@ -0,0 +1,256 @@ +# Gitlab安装 + +完全笔记《安装GitLabel》 :https://www.yuque.com/bunny-6ixda/bgxtva/wtw4x4r8kbvxwgac?singleDoc# + +## Docker安装 + +- docker镜像: + +- https://hub.docker.com/r/gitlab/gitlab-ee/tags?name=17.9.6 +- https://hub.docker.com/r/gitlab/gitlab-runner/tags?name=17.11.0 + +```bash +sudo apt-get remove docker docker-engine docker.io containerd runc +sudo apt update +sudo apt upgrade +sudo apt-get install ca-certificates curl gnupg lsb-release +# 添加Docker官方GPG密钥 +sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - +sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" +# 安装docker +sudo apt-get install docker-ce docker-ce-cli containerd.io +# 默认情况下,只有root用户和docker组的用户才能运行Docker命令。我们可以将当前用户添加到docker组,以避免每次使用Docker时都需要使用sudo,设置完成后退出当前用户之后再进入既可 +sudo usermod -aG docker $USER +# 运行docker +sudo systemctl start docker +# 安装工具 +sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common +# 重启docker +sudo service docker restart +# 重启终端生效 +exit +``` + +配置镜像源 + +```bash +# 创建目录 +sudo mkdir -p /etc/docker +# 写入配置文件 +sudo tee /etc/docker/daemon.json <<-'EOF' +{ + "registry-mirrors": [ + "https://docker-0.unsee.tech", + "https://docker-cf.registry.cyou", + "https://docker.1panel.live" + ] +} +EOF + +# 重启docker服务 +sudo systemctl daemon-reload && sudo systemctl restart docker +``` + +## 环境搭建 + +### 安装JDK21 + +```bash +# 安装JDK21 +wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb +sudo dpkg -i jdk-21_linux-x64_bin.deb +java --version +``` + +### Maven 3.8.8安装 + +#### 安装 + +```bash +# 安装maven +wget https://archive.apache.org/dist/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz +sudo mkdir -p /opt/maven +sudo tar -xzf apache-maven-3.8.8-bin.tar.gz -C /opt/maven +sudo mv /opt/maven/apache-maven-3.8.8 /opt/maven/maven-3.8.8 + +# 修改镜像配置 +cd /opt/maven/maven-3.8.8/conf +# 赋予权限修改 +sudo chmod 666 settings.xml + +# 编写配置 +sudo vim /etc/profile + +# 添加以下内容 +# export PATH=$PATH:/opt/maven/maven-3.8.8/bin + +# 刷新配置 +source /etc/profile +mvn -V +``` + +#### maven的镜像 + +```xml + + aliyun + Aliyun Maven Mirror + https://maven.aliyun.com/repository/public + central + +``` + +## 安装Gitlab + +```bash +# Ubuntu +wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/focal/main/g/gitlab-ce/gitlab-ce_18.0.0-ce.0_amd64.deb + +# dpkg +sudo dpkg -i gitlab-ce_18.0.0-ce.0_amd64.deb +``` + +### 编辑配置 + +```bash +# 编辑站点 +sudo vim /etc/gitlab/gitlab.rb +``` + +修改下面内容 + +```bash +external_url 'http://192.168.95.134:3001' +``` + +应用配置 + +```bash +# 应用配置 +sudo gitlab-ctl reconfigure +``` + +### 常用命令 + +```bash +# 服务控制 +sudo gitlab-ctl start +sudo gitlab-ctl status +sudo gitlab-ctl stop + +# 应用配置 +sudo gitlab-ctl reconfigure + +# 重启 +sudo gitlab-ctl restart +``` + +### 查看密码 + +```bash +# 24 小时后自动删除 +sudo cat /etc/gitlab/initial_root_password +``` + +## 安装Gitlab-Runner + +```bash +# 需要 gitlab-runner-helper-images +wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner-helper-images/gitlab-runner-helper-images_18.0.1-1_all.deb +sudo dpkg -i gitlab-runner-helper-images_18.0.1-1_all.deb + +# 之后安装 gitlab-runner +wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner/gitlab-runner_18.0.1-1_amd64.deb +sudo dpkg -i gitlab-runner_18.0.1-1_amd64.deb +``` + +### 先下载后安装 + +```bash +# Ubuntu +wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner-helper-images/gitlab-runner-helper-images_18.0.1-1_all.deb +wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner/gitlab-runner_18.0.1-1_amd64.deb + +# dpkg +sudo dpkg -i gitlab-runner-helper-images_18.0.1-1_all.deb +sudo dpkg -i gitlab-runner_18.0.1-1_amd64.deb +``` + +### 配置Gitlab-Runner用户 + +> [!NOTE] +> +> 如果有需要清理缓存:`sudo rm -rf /opt/maven/maven-3.8.8/conf/builds/**`** +> +> gitlab-ce:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/focal/main/g/gitlab-ce/ +> +> gitlab-runner-helper-images:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner-helper-images/ +> +> gitlab-runner:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/ubuntu/pool/focal/main/g/gitlab-runner/ + +```bash +sudo gitlab-runner uninstall +sudo gitlab-runner install --working-directory /home/gitlab-runner --user root +sudo systemctl restart gitlab-runner +``` + +### 检查 GitLab Runner 配置 + +```bash +sudo vim /etc/gitlab-runner/config.toml +``` + +修改文件 + +```bash +[[runners]] + name = "my-runner" + executor = "shell" + shell = "bash" + user = "gitlab-runner" # 确保用户有权限 + working_directory = "/home/gitlab-runner" +``` + +### 检查 Maven 安装目录权限 + +```bash +sudo chmod 777 -R /opt/maven/maven-3.8.8 +sudo chmod 777 -R /opt/maven/maven-3.8.8/ +sudo chown -R gitlab-runner:gitlab-runner /opt/maven/maven-3.8.8/ +``` + +## CI/CD脚本示例 + +如果构建出现`pending`情况大部分情况下,是文件写错了,要么是`Gitlab-Runner`标签没写对 + +```yml +stages: + - build + - build-docker + - deploy + +variables: + CONTAINER_NAME: "bunny-auth-server" + DOCKER_TAG: "4.0.0" + +build-job: + stage: build + script: + - echo "Compiling the code..." + - mvn clean package -DskipTests + - echo "Compile complete." + - docker pull openjdk:24-ea-17-jdk-oraclelinux9 + - echo "docker pull complete." + - docker build -f Dockerfile -t $CONTAINER_NAME:$DOCKER_TAG . + - echo "Application successfully deployed." + +deploy-job: + stage: deploy + environment: production + script: + - echo "Deploying application..." + - docker stop $CONTAINER_NAME || true + - docker rm $CONTAINER_NAME || true + - docker run -d -p 7070:7070 -p 8000:8000 --name $CONTAINER_NAME --restart always $CONTAINER_NAME:$DOCKER_TAG + - echo "Application successfully deployed." +``` \ No newline at end of file