Files
IOT_SIM/.github/workflows/ci.yml
T
xuyueandClaude Opus 5.5 4c3ce31fe1
Deploy / deploy (push) Successful in 1m38s
部署:push origin 就由开发机编好、测过再传到服务器,服务器不再自己编,push 后一两分钟上线。照 OJ2 的做法,开发机上的 gitea-runner(和 OJ2 共用,标签 iot-deploy,一次只跑一个任务)跑 .gitea/workflows/deploy.yml:编 C# 运行时(Release)、网页和 server.js,跑服务端测试(用要上线的 Release 版运行时)、网页端测试和类型检查,产物 rsync 到服务器的 prebuilt/(按内容比、传完才换上、断了重试),服务器上 deploy/update.sh --prebuilt 用现成产物构建镜像,只剩几条 COPY。以前 GitHub 上测试约 5 分钟、服务器 2 核再编约一分钟(编的时候学生程序会卡),push 后 6 分钟多才上线。host 模式用开发机自己的 ssh 和 git 访问,不需要 secret。Dockerfile 加构建参数 ARTIFACTS(build 默认在镜像里编 / prebuilt 用现成产物),服务器自己编的能力保留;prebuilt/COMMIT 和要部署的提交对不上时 update.sh 直接失败。服务端测试同时跑:文件之间 --parallel,评分测试标 describe.concurrent(--max-concurrency=8),本机 4 分钟降到半分钟,连跑三遍都通过。GitHub 的 ci.yml 改成 PR 跑测试、手动触发才部署(服务器自己编),开发机不在时用,push 不再两边各部署一遍。顺手:update.sh 每次部署拉一下 caddy:2-alpine(up 不会自己拉,一直是第一次部署时那份);app 和 caddy 的容器日志限 10MB × 3。部署说明、README、设计方案同步
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 12:08:14 +08:00

104 lines
3.9 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.
# 备用路线。平时 push origin(git.xuyue.cc)后,由开发机上的 gitea-runner 测试、编好、部署(.gitea/workflows/deploy.yml),
# 这里 push 不再触发,免得每次推送在两边各测一遍、各部署一遍。
# - PR:只跑测试。
# - 手动触发(Actions → CI/CD → Run workflow):测试通过后 SSH 到服务器执行 deploy/update.sh,服务器从 origin 拉代码、
# **自己编**、重启。开发机不在、又急着上线时用。部署需要的 Secrets 见 deploy/README.md 的"自动部署"一节。
# 只在 github.com 上运行(Gitea 有了 .gitea/workflows 后不再读这个目录,if 再保一道)。
name: CI/CD
on:
pull_request:
workflow_dispatch:
jobs:
test:
if: github.server_url == 'https://github.com'
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.4
- uses: actions/setup-dotnet@v6
with:
dotnet-version: 10.0.x
# 测试里学生程序在 bubblewrap 沙箱中运行;Ubuntu 24.04 默认禁止非特权用户命名空间,要先放开
- name: 准备沙箱
run: |
sudo apt-get update -q
sudo apt-get install -y -q bubblewrap
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- run: bun install --frozen-lockfile
- name: 编译 C# 运行时
run: bun run build:runtime
# 文件之间同时跑,grading 里的测试也同时跑(describe.concurrent),见 .gitea/workflows/deploy.yml 同名一步
- name: 服务端测试(编译并运行示例、沙箱、编译检查)
working-directory: server
run: bun test --parallel --max-concurrency=8
- name: 网页端测试和类型检查
working-directory: web
run: |
bun test src
bunx vue-tsc --noEmit
deploy:
needs: test
if: github.server_url == 'https://github.com' && github.event_name == 'workflow_dispatch'
runs-on: ubuntu-24.04
# 服务器构建约几分钟;卡住时及时失败,不要一直占着
timeout-minutes: 20
# 连续推送时排队部署,不会两次部署同时进行
concurrency:
group: deploy
cancel-in-progress: false
environment: production
steps:
# Secrets 还没配置时跳过部署(只跑测试),不算失败
- name: 检查部署配置
id: check
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
run: |
if [ -n "$DEPLOY_HOST" ]; then
echo "configured=true" >> "$GITHUB_OUTPUT"
else
echo "::notice::没有配置 DEPLOY_HOST 等 Secrets,跳过部署(配置方法见 deploy/README.md)"
fi
- name: 配置 SSH
if: steps.check.outputs.configured == 'true'
env:
SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
run: |
mkdir -p ~/.ssh && chmod 700 ~/.ssh
printf '%s\n' "$SSH_KEY" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
cat > ~/.ssh/config <<EOF
Host deploy-target
HostName $DEPLOY_HOST
User $DEPLOY_USER
Port ${DEPLOY_PORT:-22}
IdentityFile ~/.ssh/deploy_key
IdentitiesOnly yes
# 不预先登记服务器公钥,第一次连接时自动接受
StrictHostKeyChecking accept-new
# 认证失败立即报错,不等输入密码
BatchMode yes
ConnectTimeout 30
ServerAliveInterval 30
EOF
- name: 部署(在服务器上拉代码、构建、重启)
if: steps.check.outputs.configured == 'true'
run: ssh deploy-target "cd ${{ vars.DEPLOY_DIR || 'IOT_SIM' }} && deploy/update.sh ${{ github.sha }}"