99 lines
6.8 KiB
Markdown
99 lines
6.8 KiB
Markdown
# Spring Boot 4 + Gitea/GitLab CI/CD POC — 计划与复盘
|
||
|
||
## 一、需求摘要
|
||
|
||
- 在 `supply-controll-tower/backend` 目录创建 Spring Boot 4 项目,提供返回字符串的简单接口。
|
||
- 使用 Gitea 托管代码,提交后由 CI/CD 自动编译、构建 Docker 镜像并推送至阿里云容器镜像仓库 `registry.cn-hangzhou.aliyuncs.com/lightjob`。
|
||
- 同时提供 GitLab CI 流水线定义。
|
||
- 全程记录过程与结果,便于复盘。
|
||
|
||
## 二、实施计划
|
||
|
||
| 步骤 | 内容 | 产出 |
|
||
|---|---|---|
|
||
| 1 | 确认环境(JDK/Maven/Docker/网络)并制定计划 | 本文件 |
|
||
| 2 | 创建 Spring Boot 4 Maven 工程(含 Maven Wrapper)与 `GET /api/hello` 接口 | `pom.xml`、`src/`、`mvnw` |
|
||
| 3 | 编写接口测试并本地编译、执行单元测试通过 | `*Test.java` + 测试报告 |
|
||
| 4 | 编写多阶段 Dockerfile、Gitea Actions 与 GitLab CI 流水线 | `Dockerfile`、`.gitea/workflows/ci.yaml`、`.gitlab-ci.yml` |
|
||
| 5 | 本地验证镜像构建;初始化 Git 仓库并推送代码到 Gitea,触发/核查流水线 | Git 提交记录、镜像/流水线状态 |
|
||
| 6 | 汇总过程、结果、遇到的问题与遗留事项 | 本文件「复盘」章节 |
|
||
|
||
## 三、设计决策
|
||
|
||
- Spring Boot `4.2.0`(2026-12 稳定版),Java 21(本机 JDK 21,Boot 4 基线要求 JDK 17+)。
|
||
- 构建工具 Maven 3.9.12 Wrapper:CI 无需预装 Maven,统一版本。
|
||
- 接口返回纯文本字符串,路径 `GET /api/hello`。
|
||
- 镜像 tag 使用 `commit 短 SHA`,同时打 `latest`;凭据通过 CI Secrets/Variables 注入,不写入代码仓库。
|
||
- Dockerfile 构建阶段执行 `./mvnw verify`(含单元测试),测试不通过则不产出镜像。
|
||
- Gitea 使用 Actions(`.gitea/workflows/`),GitLab 使用 `.gitlab-ci.yml`(Docker-in-Docker)。
|
||
|
||
## 四、执行记录
|
||
|
||
### 1. 环境确认(2026-12-04)
|
||
|
||
- 本机 JDK `21.0.2`,Docker `27.1.1`(colima),无全局 Maven;使用 Maven Wrapper `3.9.12`。
|
||
- Wrapper 由 `mvn -N wrapper:wrapper -Dmaven=3.9.12` 生成;自动生成的 `distributionUrl` 指向内网 Nexus,已手动改为 `https://repo.maven.apache.org/maven2/...`,保证 CI/Docker 内可用。
|
||
- Spring Boot 选择 `4.2.0`(Maven Central 当前稳定版)。
|
||
|
||
### 2. 工程与接口
|
||
|
||
- `pom.xml`:`spring-boot-starter-parent:4.2.0`、`spring-boot-starter-web`、`spring-boot-starter-test`、`spring-boot-maven-plugin`。
|
||
- `GET /api/hello` 返回纯文本 `Hello from Spring Boot 4 CI/CD Demo!`(`HelloController`)。
|
||
- 测试:`HelloControllerTest`(MockMvc standalone,校验接口)与 `SpringbootCiciDemoApplicationTests`(Spring 上下文 + Bean 注入)。
|
||
|
||
### 3. 本地编译、单元测试与接口验证(通过)
|
||
|
||
```
|
||
$ ./mvnw -B clean verify
|
||
[INFO] Tests run: 1, Failures: 0, Errors: 0 -- in com.lightjob.demo.SpringbootCiciDemoApplicationTests
|
||
[INFO] Tests run: 1, Failures: 0, Errors: 0 -- in com.lightjob.demo.controller.HelloControllerTest
|
||
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
|
||
[INFO] BUILD SUCCESS
|
||
```
|
||
|
||
- 产出 `target/springboot-cici-demo-0.0.1-SNAPSHOT.jar`(约 19 MB)。
|
||
- `java -jar target/springboot-cici-demo-0.0.1-SNAPSHOT.jar` 启动后,`curl http://localhost:8080/api/hello` 返回 `Hello from Spring Boot 4 CI/CD Demo!`。
|
||
|
||
### 4. CI/CD 文件
|
||
|
||
- `Dockerfile`:多阶段构建。builder 阶段执行 `./mvnw -B clean verify`(测试失败则不产出镜像),runtime 阶段使用 `eclipse-temurin:21-jre`,非 root 用户运行,暴露 8080。
|
||
- `.gitea/workflows/cicd.yaml`:push 到 `master`/`main` 或手动触发;checkout → docker login(Secrets)→ docker build(短 SHA + `latest`)→ docker push。
|
||
- `.gitlab-ci.yml`:`docker:27.3.1-cli` + `docker:27.3.1-dind`,push 事件触发,使用 CI Variables 登录并推送同样两个 tag。
|
||
- `.dockerignore` / `.gitignore`:排除构建产物、IDE 文件及含明文凭据的 `task_list.md`。
|
||
|
||
### 5. Gitea 仓库与流水线触发
|
||
|
||
- 本地 `git init -b master`,共 3 次提交:`5c7ee32`(工程+流水线)、`a0d05c3`(过程与复盘)、`80cbd82`(workflow 中 registry 改用 shell 环境变量)。
|
||
- 推送至 `https://gitea-verify.lightjob.cn/poc/springboot-cici-demo`(master),Gitea 侧 commit/push secret 扫描均提示 `no secrets found`。
|
||
- 每次 push 均触发 Gitea Actions:`cicd.yaml@refs/heads/master`,event=push,job=`build-and-push`;早期 run 因新提交入队被 cancelled,最近一次 run 状态 `queued`(见「复盘」中的 runner 说明)。
|
||
- 已通过 Gitea API 配置仓库 Secrets:`REGISTRY_USERNAME`、`REGISTRY_PASSWORD`(仅名称入库,值为任务清单提供的阿里云仓库账号;仓库代码中无任何明文凭据)。
|
||
|
||
### 6. 本地镜像构建验证(未完成,环境受限)
|
||
|
||
- 执行 `docker build -t springboot-cici-demo:local .` 两次:本机 Docker 守护进程配置了镜像加速 `https://docker.1ms.run/`,首次 DNS 解析失败,第二次基础镜像拉取速度约 1 MB/分钟,构建被中止,未生成本地镜像。
|
||
- 该问题只影响本地验证;CI 流水线中的 `docker build` 由 Runner 在服务器端执行。网络正常时可直接执行上述命令复核。
|
||
|
||
## 五、复盘
|
||
|
||
### 已完成
|
||
|
||
1. Spring Boot 4 工程 + 简单字符串接口 + 2 个单元测试,本地 `verify` 全绿、接口实测通过。
|
||
2. Dockerfile 与两套 CI 定义(Gitea Actions / GitLab CI),镜像按 commit 短 SHA 与 `latest` 双 tag 推送。
|
||
3. 代码已推送 Gitea 仓库并成功触发 Actions;仓库 Secrets 已配置。
|
||
4. 含明文密码的 `task_list.md` 未进入 Git 仓库或 Docker 构建上下文。
|
||
|
||
### 问题与处理
|
||
|
||
| 问题 | 处理 |
|
||
|---|---|
|
||
| Wrapper 生成时带入内网 Nexus 地址 | 改为公网 Maven Central,CI/Docker 不依赖内网 |
|
||
| Gitea Secrets API 使用 `value` 字段返回 422 | 改用 Gitea 要求的 `data` 字段,201 创建成功 |
|
||
| 本机 Docker 镜像加速不可用/极慢 | 停止本地构建,改由 CI Runner 执行;保留完整验证命令 |
|
||
| Actions job 长时间 `queued` | 服务器当前没有在线 runner 接单(见下) |
|
||
|
||
### 待办 / 后续验证
|
||
|
||
1. **Gitea Runner**:最新 run 已排队但无 runner 接单。需在 Gitea 管理员页面生成 Runner token,并在 `47.131.161.231`(Docker 宿主机)注册 `act_runner`,标签包含 `ubuntu-latest` 且宿主可执行 `docker`;注册后重新运行该 workflow。本机 SSH 免密连接被拒,用任务清单中密码尝试连接 root 亦被拒,故本次未代注册。
|
||
2. **GitLab 侧**:如启用 `.gitlab-ci.yml`,在 GitLab 项目 → Settings → CI/CD → Variables 添加掩码变量 `REGISTRY_USERNAME`、`REGISTRY_PASSWORD`。
|
||
3. **首次成功构建后**:在阿里云镜像仓库 `registry.cn-hangzhou.aliyuncs.com/lightjob/springboot-cici-demo` 检查短 SHA 与 `latest` 两个 tag;可用 `docker run -p 8080:8080 <镜像>` 冒烟验证。
|