Files
springboot-cici-demo/doc/devops/gitea/gitea-act-runner.md
yangzelin bceed9e859
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 5m14s
feat: 构建 amd64/arm64 多架构镜像并迁移 DevOps 文档目录
2026-12-04 18:24:44 +08:00

122 lines
4.5 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 Act Runner(可复制步骤)
Act Runner 是 Gitea Actions 的执行器。本次环境中 Gitea 与 runner 都通过 Docker 部署,且位于同一 Docker network `1panel-network`(由 1Panel 创建)。
## 一、前置准备
1. 登录 Gitea → 管理员设置 → Actions → Runners → 创建新 Runner,复制生成的 token(下文用 `<TOKEN>` 代替)。
2. 确认 Gitea 容器别名可访问:在任意同网络容器内执行
```bash
wget -qO- http://gitea:3000/api/healthz
```
应返回 `{"status":"pass",...}`。
3. 准备好阿里云镜像仓库账号(随后写入 Gitea 仓库 Secrets,不写入 runner 配置)。
## 二、1Panel 方式(本次实际采用)
应用目录:`/opt/1panel/apps/act_runner/ubuntu-latest/`
### 1. `.env` 配置
```env
CONTAINER_NAME='ubuntu-latest'
RUNNER_LABELS='ubuntu-latest'
RUNNER_REGISTRATION_TOKEN='<TOKEN>'
GITEA_INSTANCE_URL='http://gitea:3000'
RUNNER_NAME='ubuntu-latest'
MEMORY_LIMIT=0
CPUS=0
```
关键点:
- `GITEA_INSTANCE_URL` **必须**写 `http://gitea:3000`。本次最初写成宿主机 IP `http://172.31.3.223:3000`,runner 报 `Cannot ping the Gitea instance server ... connect: connection timed out`,注册失败且 `.runner` 文件不生成。Gitea 只在该 Docker 网络内监听,宿主机 IP 从 runner 容器不可达。
- `RUNNER_LABELS` 必须与 workflow 的 `runs-on` 一致。本次最初是 `act_runner`,而 workflow 写 `ubuntu-latest`,job 会一直 `queued` 无人接单。
- 修改 `.env` 后需要重建容器(`docker compose up -d --force-recreate`),仅 restart 不会重新读取环境变量。
### 2. `docker-compose.yml`
```yaml
networks:
1panel-network:
external: true
services:
ubuntu-latest:
container_name: ${CONTAINER_NAME}
deploy:
resources:
limits:
cpus: ${CPUS}
memory: ${MEMORY_LIMIT}
environment:
- GITEA_INSTANCE_URL=${GITEA_INSTANCE_URL}
- GITEA_RUNNER_REGISTRATION_TOKEN=${RUNNER_REGISTRATION_TOKEN}
- GITEA_RUNNER_NAME=${RUNNER_NAME}
- GITEA_RUNNER_LABELS=${RUNNER_LABELS}
image: gitea/act_runner:0.6.1-ci
labels:
createdBy: Apps
networks:
- 1panel-network
restart: always
volumes:
- ./data/data:/data
- /var/run/docker.sock:/var/run/docker.sock
```
- `/var/run/docker.sock` 挂载是必需的:workflow 里的 `docker build` / `docker push` 由 runner 调用宿主机 Docker 完成。
- `./data/data:/data` 保存 `.runner` 注册文件,容器重建后无需重新注册。
- 镜像 `gitea/act_runner:0.6.1-ci` 是自定义镜像,见 `custom-runner-image.md`。首次搭建可先用官方镜像完成注册,再替换为自定义镜像。
### 3. 启动与验证
```bash
cd /opt/1panel/apps/act_runner/ubuntu-latest
docker compose up -d --force-recreate
docker logs -f ubuntu-latest
```
成功日志应包含:
```text
level=debug msg="Successfully pinged the Gitea instance server"
level=info msg="Runner registered successfully."
level=info msg="Starting runner daemon"
level=info msg="runner: ubuntu-latest, with version: v0.6.1, with labels: [ubuntu-latest], declare successfully"
```
数据目录出现 `.runner` 文件:
```bash
ls -l /opt/1panel/apps/act_runner/ubuntu-latest/data/data/.runner
```
## 三、纯 Docker 方式(无 1Panel 时)
```bash
docker network create cicd-net
docker run -d --name ubuntu-latest \
--network cicd-net \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /opt/act_runner/data:/data \
-e GITEA_INSTANCE_URL=http://gitea:3000 \
-e GITEA_RUNNER_REGISTRATION_TOKEN='<TOKEN>' \
-e GITEA_RUNNER_NAME=ubuntu-latest \
-e GITEA_RUNNER_LABELS=ubuntu-latest \
gitea/act_runner:0.6.1-ci
```
此时 Gitea 容器也必须加入 `cicd-net`(或把 runner 加入 Gitea 所在网络),否则无法通过 `gitea` 别名解析。
## 四、Runner 自检清单
| 检查项 | 命令 / 预期 |
|---|---|
| Gitea 可达 | `docker exec ubuntu-latest wget -qO- http://gitea:3000/api/healthz` → `status: pass` |
| 注册文件 | `docker exec ubuntu-latest ls -l /data/.runner` 存在 |
| 标签匹配 | 日志 `with labels: [ubuntu-latest]`,与 workflow `runs-on` 一致 |
| Docker CLI | `docker exec ubuntu-latest docker version --format '{{.Client.Version}}'` 有输出 |
| Node.js | `docker exec ubuntu-latest node --version` 有输出(checkout action 依赖) |
| 接单 | push 代码后 Gitea Actions run 状态从 `queued` 变为 `in_progress` |