Files
springboot-cici-demo/doc/devops/gitlab/workflow-guide.md
yangzelin 10fcba4f90
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 15s
refactor: 整理 Docker 资产并补充可复制 CI/CD 部署文档
2026-12-04 14:53:04 +08:00

148 lines
5.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Gitea Actions Workflow 编写指南
文件位置:`.gitea/workflows/cicd.yaml`。Gitea Actions 兼容 GitHub Actions 语法,由 `act_runner` 执行。
## 一、完整工作流(当前版本)
```yaml
name: Build and Push Docker Image
on:
push:
branches: [master, main]
workflow_dispatch:
env:
REGISTRY: registry.cn-hangzhou.aliyuncs.com
IMAGE: registry.cn-hangzhou.aliyuncs.com/lightjob/springboot-cici-demo
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Login to registry
run: echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "$REGISTRY" -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
- name: Build image (Maven verify runs inside Dockerfile)
run: |
SHORT_SHA=$(echo "$GITHUB_SHA" | cut -c1-7)
docker build -f doc/devops/docker/Dockerfile -t "$IMAGE:$SHORT_SHA" -t "$IMAGE:latest" .
- name: Push image
run: |
SHORT_SHA=$(echo "$GITHUB_SHA" | cut -c1-7)
docker push "$IMAGE:$SHORT_SHA"
docker push "$IMAGE:latest"
```
## 二、逐段说明
### 1. 触发器 `on`
- `push.branches: [master, main]`:只有推到这两个分支才触发。
- `workflow_dispatch`:允许手动触发,便于调试(本文后面给了 API 调用方式)。
### 2. 全局变量 `env`
`REGISTRY` / `IMAGE` 对所有 step 可见,step 内用 shell 变量 `$REGISTRY`、`$IMAGE` 引用。
> 曾尝试写成 `${{ env.REGISTRY }}`,为兼容 Gitea Actions 的上下文实现,改为更稳妥的 shell 环境变量方式。
### 3. Job 与 runner 匹配
```yaml
runs-on: ubuntu-latest
```
这是 **runner 标签**,不是机器型号。注册 runner 时必须把标签设为 `ubuntu-latest`(见 `gitea-act-runner.md`)。标签不匹配时 job 会一直 `queued`,不会报错但永远不会执行。
### 4. Step 1:检出代码
`actions/checkout@v4` 需要 runner 内有 Node.js。自定义镜像 `gitea/act_runner:0.6.1-ci` 已安装。
### 5. Step 2:登录镜像仓库
- `${{ secrets.REGISTRY_USERNAME }}` / `${{ secrets.REGISTRY_PASSWORD }}` 来自 Gitea 仓库 Secrets。
- 通过 `--password-stdin` 传入,密码不出现在命令行/日志。
- `docker login` 依赖 runner 内 docker CLI 与挂载的 `/var/run/docker.sock`。
### 6. Step 3:构建镜像(含 Maven 编译)
```bash
SHORT_SHA=$(echo "$GITHUB_SHA" | cut -c1-7)
docker build -f doc/devops/docker/Dockerfile -t "$IMAGE:$SHORT_SHA" -t "$IMAGE:latest" .
```
- `$GITHUB_SHA` 由 Gitea Actions 注入,是本次提交的完整 commit SHA;取前 7 位作为镜像 tag,保证每次提交可追溯。
- `-f doc/devops/docker/Dockerfile`:Dockerfile 已整理到 `doc/devops/docker/`,构建 context 仍是仓库根 `.`。
- **Maven 编译不写在 workflow 里**,而在 Dockerfile 的 builder 阶段:
```dockerfile
FROM maven:3.9-eclipse-temurin-21 AS builder
COPY .mvn .mvn
COPY mvnw pom.xml ./
COPY src src
RUN ./mvnw -B clean verify
```
`verify` 会执行编译和单元测试(当前 2 个用例);测试失败则 Docker build 失败,后续 push 不执行。
- 镜像ENTRYPOINT 为生产启动脚本 `/app/bin/java-run.sh`,支持环境变量传参(见 `doc/devops/docker/README.md`)。
### 7. Step 4:推送镜像
把短 SHA 与 `latest` 两个 tag 都推送。重复执行是幂等的。
## 三、配置 Secrets
### 方式一:Gitea UI
仓库 → Settings → Secrets → Actions → 新建:
- `REGISTRY_USERNAME`
- `REGISTRY_PASSWORD`
### 方式二:API(本次实际使用)
```bash
curl -u '<GITEA_USER>:<PASSWORD>' -X PUT \
-H 'Content-Type: application/json' \
-d '{"data":"<SECRET_VALUE>"}' \
https://gitea-verify.lightjob.cn/api/v1/repos/poc/springboot-cici-demo/actions/secrets/REGISTRY_USERNAME
```
**注意**:请求体字段必须是 `data`;写成 `{"value":"..."}` 会返回 `HTTP 422 {"message":"[Data]: Required"}`。成功后返回 201,列表接口只显示 secret 名称,不回显值。
## 四、常用操作 API
```bash
BASE=https://gitea-verify.lightjob.cn/api/v1/repos/poc/springboot-cici-demo
# 手动触发(workflow_dispatch),成功返回 204
curl -u "$USER:$PASS" -X POST -H 'Content-Type: application/json' \
-d '{"ref":"master"}' "$BASE/actions/workflows/cicd.yaml/dispatches"
# 查看最近运行
curl -u "$USER:$PASS" "$BASE/actions/runs?limit=5"
# 查看某个 run 的 job
curl -u "$USER:$PASS" "$BASE/actions/runs/<RUN_ID>/jobs"
# 查看 job 日志(排障最重要)
curl -u "$USER:$PASS" "$BASE/actions/jobs/<JOB_ID>/logs"
```
当前版本 Gitea 不支持 `POST /actions/runs/{id}/rerun`(返回 404),需要重新 push 或用 `workflow_dispatch` 触发。
## 五、换环境时的最小改动清单
1. `on.push.branches`:改成新仓库的实际分支。
2. `env.IMAGE`:改成新的镜像仓库与命名空间。
3. 仓库 Secrets:按上文重新配置。
4. runner:按 `gitea-act-runner.md` + `custom-runner-image.md` 在新 Docker 环境重建。
5. 确认新环境能访问 Maven Central、Docker Hub、阿里云 Registry。
## 六、GitLab CI 对照
`.gitlab-ci.yml` 使用 `docker:27.3.1-cli` + `docker:27.3.1-dind`,同样执行 `docker build -f doc/devops/docker/Dockerfile ...`,Maven 编译仍在 Dockerfile 内完成。GitLab 侧只需在项目 Variables 配置 `REGISTRY_USERNAME`、`REGISTRY_PASSWORD`(建议勾选 Masked)。