Files
CloudLab/.gitea/workflows/ci.yml
T
CloudLabandWorkBuddy 54876a5f26
CloudLab CI/CD / 门禁 doctor (push) Failing after 30s
CloudLab CI/CD / 单元测试 (push) Skipped
CloudLab CI/CD / 归档完整性 (push) Skipped
CloudLab CI/CD / 构建站点与归档 (push) Skipped
CloudLab CI/CD / 部署到 lab.hdtime.space(rsync) (push) Skipped
CloudLab CI/CD / 构建镜像并推送 Gitea 容器仓库 (push) Skipped
CloudLab CI/CD / 容器部署到 lab.hdtime.space (push) Skipped
CloudLab CI/CD / 发布版本(Tag) (push) Skipped
fix(deploy): 容器部署改用 8080 端口,避开被占用的 80
- docker-compose / deploy-container.sh / CI deploy-container 均改宿主机发布端口 80→8080
  (容器内 nginx 仍监听 80);端口可用 CLOUDLAB_SITE_PORT 覆盖
- VERSION 3.7.3;同步 ARCHITECTURE/CONTRIBUTING/CHANGELOG

Co-Authored-By: WorkBuddy <noreply@workbuddy.ai>
2026-11-11 07:59:40 +08:00

255 lines
11 KiB
YAML
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.
# CloudLab CI/CD —— Gitea Actions(与 GitHub Actions YAML 兼容)
#
# 触发:push 到 master / 推送 v* 标签 / 任意 PR / 手动
# 设计原则(与控制面一致):纯标准库、零联网取数、不装第三方依赖、不持有任何凭据。
# - CI:门禁(gate) → 测试 → 完整性 → 构建站点+归档产物
# - CD(二选一,由仓库变量 DEPLOY_MODE 切换,默认 rsync 文件部署):
# · rsync 文件部署 → 服务器主机 nginx 提供静态站点
# · 容器部署 → 构建镜像推送 Gitea 容器仓库,服务器以 nginx 容器运行
# · 发布 Release → v* 标签经 Gitea API 上传归档包
#
# 需要的仓库 Secrets(Settings → Actions → Secrets):
# DEPLOY_SSH_KEY 部署机 SSH 私钥(ed25519/rsa),用于 rsync 或容器部署
# DEPLOY_HOST 可选,默认 root@121.43.150.236
# DEPLOY_REMOTE_DIR 可选,默认 /var/www/cloudlab(仅 rsync 部署用)
# GITEA_TOKEN 容器部署 / 发布 Release 需要(含 packages:write 或个人 token)
# GITEA_API_URL 可选,默认 https://git.hdtime.space/api/v1
# 需要的仓库变量(Settings → Actions → Variables,可选):
# DEPLOY_MODE 设为 container 时切换到容器部署(默认空 = rsync 文件部署)
#
# 前置:仓库需在 Gitea 侧启用 Actions 并至少有一个标签为 ubuntu-latest 的 runner 在线;
# 容器部署还要求该 runner 与服务器均可访问 docker(runner 需 docker 权限)。
name: CloudLab CI/CD
on:
push:
branches: [master]
tags: ['v*']
pull_request:
branches: [master]
workflow_dispatch:
concurrency:
group: cloudlab-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
env:
PYTHON_VERSION: '3.11'
jobs:
# ──────────────────────────── CI:门禁 ────────────────────────────
gate:
name: 门禁 doctor
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: 控制面自检门禁(语法/硬编码/密钥/门户/完整性/索引卫生)
run: python3 cloudlab.py doctor
# ──────────────────────────── CI:测试 ────────────────────────────
test:
name: 单元测试
needs: gate
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: unittest(tests/,28 项)
run: python3 cloudlab.py test
# ────────────────────────── CI:完整性 ────────────────────────────
verify:
name: 归档完整性
needs: gate
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: 字节级完整性校验(快照 vs 修订登记)
run: python3 cloudlab.py verify
# ────────────────────────── CI:构建产物 ──────────────────────────
build:
name: 构建站点与归档
needs: [gate, test, verify]
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: 构建静态站点(可复现,规则来自 cloudlab_core)
run: CLOUDLAB_SITE_DIR="$PWD/CloudLab-Site" python3 cloudlab.py build site
- name: 构建全量归档包
run: python3 cloudlab.py build archive
- name: 上传站点产物
uses: actions/upload-artifact@v4
with:
name: cloudlab-site
path: CloudLab-Site/
retention-days: 14
if-no-files-found: error
- name: 上传归档产物
uses: actions/upload-artifact@v4
with:
name: cloudlab-archive
path: |
CloudLab_全量归档_v3.6_20261110.zip
CloudLab_全量归档_v3.6_20261110.zip.sha256.txt
retention-days: 14
if-no-files-found: error
# ─────────────── CD(默认):rsync 文件部署到主机 nginx ───────────────
deploy:
name: 部署到 lab.hdtime.space(rsync)
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && vars.DEPLOY_MODE != 'container' && secrets.DEPLOY_SSH_KEY != ''
runs-on: ubuntu-latest
timeout-minutes: 30
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_REMOTE_DIR: ${{ secrets.DEPLOY_REMOTE_DIR }}
steps:
- uses: actions/checkout@v4
- name: 下载站点产物
uses: actions/download-artifact@v4
with:
name: cloudlab-site
path: CloudLab-Site
- name: 安装 SSH 客户端并写入密钥
run: |
sudo apt-get update -y && sudo apt-get install -y openssh-client rsync
mkdir -p ~/.ssh
printf '%s\n' "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "${DEPLOY_HOST#*@}" >> ~/.ssh/known_hosts
- name: 部署(rsync,无 --delete;不删远端既有文件)
run: |
CLOUDLAB_SSH_HOST="$DEPLOY_HOST" \
CLOUDLAB_REMOTE_DIR="${DEPLOY_REMOTE_DIR:-/var/www/cloudlab}" \
CLOUDLAB_SITE_DIR="$PWD/CloudLab-Site" \
bash deploy/deploy.sh
# ─────────────── CD(可选):容器镜像构建并推送 Gitea 仓库 ───────────────
docker-build:
name: 构建镜像并推送 Gitea 容器仓库
needs: [gate, test, verify]
if: vars.DEPLOY_MODE == 'container' && secrets.GITEA_TOKEN != ''
runs-on: ubuntu-latest
timeout-minutes: 90
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
REGISTRY: git.hdtime.space
OWNER: hm
IMAGE: cloudlab
steps:
- uses: actions/checkout@v4
- name: 解析版本与引用标签
id: meta
run: |
VERSION=$(python3 -c "import cloudlab_core.registry as r; print(r.VERSION)")
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "ref=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"
- name: 构建镜像(多阶段:容器内部 build site → nginx 服务)
run: |
docker build -t "$REGISTRY/$OWNER/$IMAGE:${VERSION}" \
-t "$REGISTRY/$OWNER/$IMAGE:${GITHUB_REF_NAME}" \
-f deploy/Dockerfile .
- name: 登录并推送 Gitea 容器仓库
run: |
echo "$GITEA_TOKEN" | docker login "$REGISTRY" -u "$OWNER" --password-stdin
docker push "$REGISTRY/$OWNER/$IMAGE:${VERSION}"
docker push "$REGISTRY/$OWNER/$IMAGE:${GITHUB_REF_NAME}"
# ─────────────── CD(可选):容器部署到服务器 ───────────────
deploy-container:
name: 容器部署到 lab.hdtime.space
needs: docker-build
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && vars.DEPLOY_MODE == 'container' && secrets.DEPLOY_SSH_KEY != '' && secrets.GITEA_TOKEN != ''
runs-on: ubuntu-latest
timeout-minutes: 30
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: 安装 SSH 客户端并写入密钥
run: |
sudo apt-get update -y && sudo apt-get install -y openssh-client
mkdir -p ~/.ssh
printf '%s\n' "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "${DEPLOY_HOST#*@}" >> ~/.ssh/known_hosts
- name: 拉取镜像并以 nginx 容器运行(端口 80)
run: |
ssh "$DEPLOY_HOST" bash -s <<EOF
set -e
echo '$GITEA_TOKEN' | docker login git.hdtime.space -u hm --password-stdin
docker pull git.hdtime.space/hm/cloudlab:master
docker rm -f cloudlab-site 2>/dev/null || true
docker run -d --name cloudlab-site -p 8080:80 --restart unless-stopped git.hdtime.space/hm/cloudlab:master
EOF
# ─────────────────────────── CD:发布 Release ──────────────────────
release:
name: 发布版本(Tag)
needs: build
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
timeout-minutes: 30
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
GITEA_API_URL: ${{ secrets.GITEA_API_URL || 'https://git.hdtime.space/api/v1' }}
steps:
- uses: actions/checkout@v4
- name: 解析仓库坐标(来自 origin)
id: repo
run: |
url=$(git remote get-url origin)
owner=$(echo "$url" | sed -E 's#.*[:/]([^/]+)/([^/]+)\.git#\1#')
name=$(echo "$url" | sed -E 's#.*[:/]([^/]+)/([^/]+)\.git#\2#')
echo "owner=$owner" >> "$GITHUB_OUTPUT"
echo "name=$name" >> "$GITHUB_OUTPUT"
echo "tag=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"
- name: 下载归档产物
uses: actions/download-artifact@v4
with:
name: cloudlab-archive
path: dist
- name: 创建/复用 Release 并上传归档(Gitea API)
run: |
API="$GITEA_API_URL"
owner="${{ steps.repo.outputs.owner }}"
name="${{ steps.repo.outputs.name }}"
tag="${{ steps.repo.outputs.tag }}"
auth="-H Authorization:token $GITEA_TOKEN"
rel_id=$(curl -sS $auth "$API/repos/$owner/$name/releases/tags/$tag" \
| sed -nE 's/.*"id":([0-9]+).*/\1/p' | head -1)
if [ -z "$rel_id" ]; then
rel_id=$(curl -sS -X POST "$API/repos/$owner/$name/releases" \
$auth -H "Content-Type: application/json" \
-d "{\"tag_name\":\"$tag\",\"name\":\"$tag\",\"body\":\"CloudLab $tag 自动发布\"}" \
| sed -nE 's/.*"id":([0-9]+).*/\1/p' | head -1)
fi
echo "release id = $rel_id"
for f in dist/*; do
echo "uploading $(basename "$f")"
curl -sS -X POST "$API/repos/$owner/$name/releases/$rel_id/assets?name=$(basename "$f")" \
$auth -H "Content-Type: application/octet-stream" \
--data-binary @"$f"
done