部署改为在服务器上构建:Actions 测试通过后 SSH 执行 deploy/update.sh
CI/CD / test (push) Canceled after 0s
CI/CD / deploy (push) Canceled after 0s

跨境上传 130 MB 镜像 40 分钟没传完。改为服务器从 origin(git.xuyue.cc)拉代码、
构建镜像、重启,等编译服务就绪;提交还没到 origin 时等待重试。
部署任务加 20 分钟超时,SSH 认证失败立即报错。去掉 deploy/deploy.sh。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-10-22 16:41:50 +08:00
1 parent 1cb2235c3d
commit a00622be15
6 files changed
+93 -92

No files matched your search

+8 -21
View File
@@ -1,4 +1,4 @@
# 每次推送和 PR 都跑测试;推送到 main 且测试通过后,构建镜像并经 SSH 部署到服务器。 # 每次推送和 PR 都跑测试;推送到 main 且测试通过后,SSH 到服务器执行 deploy/update.sh(服务器从 origin 拉代码、构建、重启)。
# 部署需要的 Secrets 见 deploy/README.md 的"自动部署"一节。 # 部署需要的 Secrets 见 deploy/README.md 的"自动部署"一节。
# 只在 github.com 上运行;仓库同时推到 git.xuyue.cc,那边的 Gitea Actions 也会读这个目录,用 if 跳过。 # 只在 github.com 上运行;仓库同时推到 git.xuyue.cc,那边的 Gitea Actions 也会读这个目录,用 if 跳过。
name: CI/CD name: CI/CD
@@ -51,6 +51,8 @@ jobs:
needs: test needs: test
if: github.server_url == 'https://github.com' && github.ref == 'refs/heads/main' && github.event_name != 'pull_request' if: github.server_url == 'https://github.com' && github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-24.04 runs-on: ubuntu-24.04
# 服务器构建约几分钟;卡住时及时失败,不要一直占着
timeout-minutes: 20
# 连续推送时排队部署,不会两次部署同时进行 # 连续推送时排队部署,不会两次部署同时进行
concurrency: concurrency:
group: deploy group: deploy
@@ -69,22 +71,6 @@ jobs:
echo "::notice::没有配置 DEPLOY_HOST 等 Secrets,跳过部署(配置方法见 deploy/README.md)" echo "::notice::没有配置 DEPLOY_HOST 等 Secrets,跳过部署(配置方法见 deploy/README.md)"
fi fi
- uses: actions/checkout@v4
if: steps.check.outputs.configured == 'true'
- uses: docker/setup-buildx-action@v3
if: steps.check.outputs.configured == 'true'
- name: 构建镜像(带缓存)
if: steps.check.outputs.configured == 'true'
uses: docker/build-push-action@v6
with:
context: .
load: true
tags: iot-sim:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: 配置 SSH - name: 配置 SSH
if: steps.check.outputs.configured == 'true' if: steps.check.outputs.configured == 'true'
env: env:
@@ -105,11 +91,12 @@ jobs:
IdentitiesOnly yes IdentitiesOnly yes
# 不预先登记服务器公钥,第一次连接时自动接受 # 不预先登记服务器公钥,第一次连接时自动接受
StrictHostKeyChecking accept-new StrictHostKeyChecking accept-new
# 认证失败立即报错,不等输入密码
BatchMode yes
ConnectTimeout 30
ServerAliveInterval 30 ServerAliveInterval 30
EOF EOF
- name: 部署 - name: 部署(在服务器上拉代码、构建、重启)
if: steps.check.outputs.configured == 'true' if: steps.check.outputs.configured == 'true'
env: run: ssh deploy-target "cd ${{ vars.DEPLOY_DIR || 'IOT_SIM' }} && deploy/update.sh ${{ github.sha }}"
SKIP_BUILD: '1'
run: deploy/deploy.sh deploy-target "${{ vars.DEPLOY_DIR || 'IOT_SIM' }}"
+1 -1
View File
@@ -37,7 +37,7 @@ bun test/load.ts 100 # 压力测试:同时运行 100 个学生程序
## 部署 ## 部署
Docker Compose,HTTPS 由服务器上的 Nginx Proxy Manager 负责。推送到 GitHub 的 main 后,GitHub Actions 跑完测试自动部署;也可以在开发机执行 `deploy/deploy.sh 用户@服务器`。详见 [deploy/README.md](deploy/README.md)。 Docker Compose,HTTPS 由服务器上的 Nginx Proxy Manager 负责。推送到 GitHub 的 main 后,GitHub Actions 跑完测试,SSH 到服务器执行 `deploy/update.sh`:服务器从 git.xuyue.cc 拉代码、构建镜像、重启。详见 [deploy/README.md](deploy/README.md)。
## 运行器的两种模式 ## 运行器的两种模式
+25 -12
View File
@@ -26,44 +26,57 @@
## 第一次部署 ## 第一次部署
1. 在服务器上准备目录和口令: 服务器上的 `~/IOT_SIM` 是从 origin(git.xuyue.cc)克隆的仓库,镜像在服务器上构建;配置只放在 `.env`(不进 git)。
1. 服务器要能拉取 origin:仓库是私有的话,在服务器上生成一把密钥(`ssh-keygen -t ed25519`),把公钥加到 git.xuyue.cc 仓库的部署密钥(只读)里。
2. 克隆仓库,准备口令:
```bash ```bash
mkdir -p ~/IOT_SIM && cd ~/IOT_SIM git clone git@git.xuyue.cc:xuyue/IOT_SIM.git ~/IOT_SIM
# 部署脚本会上传 .env.example;也可以直接新建 .env: cd ~/IOT_SIM
echo 'ACCESS_PASSWORD=换成你的口令' > .env echo 'ACCESS_PASSWORD=换成你的口令' > .env
chmod 600 .env chmod 600 .env
``` ```
2. 在本机(开发机)执行: `~/IOT_SIM` 已经存在(比如以前用脚本上传过文件)时,就地变成仓库,已有的 `.env` 会保留:
```bash ```bash
deploy/deploy.sh 用户@服务器 cd ~/IOT_SIM
git init -q && git remote add origin git@git.xuyue.cc:xuyue/IOT_SIM.git
git fetch origin && git checkout -f -B main origin/main
``` ```
脚本在本机构建镜像,通过 ssh 传到服务器并启动。日志最后应看到"C# 编译服务已就绪",且没有"沙箱无法启动"。 3. 构建并启动:
3. 在 Nginx Proxy Manager 里添加 Proxy Host: ```bash
deploy/update.sh
```
拉取代码、构建镜像(第一次要下载 .NET SDK、Bun 等镜像和依赖,之后有缓存)、启动,最后应看到"部署成功"。
4. 在 Nginx Proxy Manager 里添加 Proxy Host:
- Domain Names:已备案的域名 - Domain Names:已备案的域名
- Scheme:`http`,Forward Hostname / IP:`iot-sim`,Forward Port:`80` - Scheme:`http`,Forward Hostname / IP:`iot-sim`,Forward Port:`80`
- **打开 Websockets Support**(运行程序靠 WebSocket,不打开会一直连不上) - **打开 Websockets Support**(运行程序靠 WebSocket,不打开会一直连不上)
- SSL:申请证书,打开 Force SSL - SSL:申请证书,打开 Force SSL
4. 浏览器打开域名,输入口令进入。 5. 浏览器打开域名,输入口令进入。
## 自动部署(GitHub Actions) ## 自动部署(GitHub Actions)
`.github/workflows/ci.yml`: `.github/workflows/ci.yml`:
- 每次推送和 PR:编译 C# 运行时,跑服务端测试(含沙箱)、网页端测试和类型检查。 - 每次推送和 PR:编译 C# 运行时,跑服务端测试(含沙箱)、网页端测试和类型检查。
- 推送到 GitHub 的 main 且测试通过:构建镜像,经 SSH 传到服务器并重启,等到"编译服务已就绪"才算成功。**重启会结束正在运行的学生程序**,上课时间尽量不要推送 main。 - 推送到 GitHub 的 main 且测试通过:SSH 到服务器执行 `deploy/update.sh <提交号>`,服务器从 origin 拉代码、构建、重启,等到"编译服务已就绪"才算成功。**重启会结束正在运行的学生程序**,上课时间尽量不要推送 main。
- 服务器从 origin 拉代码,所以**两个仓库都要推送**(`git push origin main`、`git push github main`)。GitHub 先到时,脚本会等 origin 最多约 100 秒。
- 在 GitHub 的 Actions 页面点 Run workflow 可以手动重新部署。 - 在 GitHub 的 Actions 页面点 Run workflow 可以手动重新部署。
- 只在 github.com 上运行;推到 git.xuyue.cc 不会触发。 - 只在 github.com 上运行;推到 git.xuyue.cc 不会触发。
一次性配置(在本机执行,需要已登录的 `gh`): 一次性配置(在本机执行,需要已登录的 `gh`):
1. 服务器上的部署用户要能运行 docker(在 docker 组里),并按上面"第一次部署"第 1 步准备好 `~/IOT_SIM/.env`。 1. 服务器上的部署用户要能运行 docker(在 docker 组里),并按上面"第一次部署"准备好 `~/IOT_SIM`。
2. 生成部署专用的 SSH 密钥,公钥放到服务器,私钥和服务器信息存进仓库 Secrets: 2. 生成部署专用的 SSH 密钥(GitHub Actions 用它登录服务器),公钥放到服务器,私钥和服务器信息存进仓库 Secrets:
```bash ```bash
ssh-keygen -t ed25519 -N '' -C iot-sim-deploy -f /tmp/iot_deploy ssh-keygen -t ed25519 -N '' -C iot-sim-deploy -f /tmp/iot_deploy
@@ -82,7 +95,7 @@
| 做什么 | 命令 | | 做什么 | 命令 |
|---|---| |---|---|
| 更新到新版本 | 推送到 GitHub 的 main(自动部署),或在本机执行 `deploy/deploy.sh 用户@服务器` | | 更新到新版本 | 推送到两个仓库的 main(GitHub 测试通过后自动部署),或在服务器上执行 `deploy/update.sh` |
| 换口令 | 修改 `.env` 后 `docker compose up -d`(所有人要重新输入口令) | | 换口令 | 修改 `.env` 后 `docker compose up -d`(所有人要重新输入口令) |
| 看日志 | `docker compose logs -f app` | | 看日志 | `docker compose logs -f app` |
| 看资源占用 | `docker stats` | | 看资源占用 | `docker stats` |
-57
View File
@@ -1,57 +0,0 @@
#!/usr/bin/env bash
# 构建镜像,传到服务器后启动(服务器不需要访问 Docker Hub,也不用自己编译)。
# 本机手动部署和 GitHub Actions 自动部署都用这个脚本。
# 用法:deploy/deploy.sh 用户@服务器 [服务器上的目录,默认 ~/IOT_SIM]
# SKIP_BUILD=1 时不构建,直接用已有的 iot-sim:latest 镜像(Actions 里已带缓存构建好)
# 第一次部署前,先按 deploy/README.md 在服务器上准备好 .env
set -euo pipefail
host=${1:?用法:deploy/deploy.sh 用户@服务器 [目录]}
dir=${2:-IOT_SIM}
cd "$(dirname "$0")/.."
if [[ -z "${SKIP_BUILD:-}" ]]; then
echo "== 构建镜像"
docker compose build app
fi
docker pull -q caddy:2-alpine
echo "== 上传配置文件到 $host:$dir"
ssh "$host" "mkdir -p $dir/deploy"
scp -q compose.yaml .env.example "$host:$dir/"
scp -q deploy/Caddyfile "$host:$dir/deploy/"
echo "== 上传镜像(约 130 MB)"
docker save iot-sim:latest | gzip -1 | ssh "$host" 'gunzip | docker load'
if ! ssh "$host" 'docker image inspect caddy:2-alpine >/dev/null 2>&1'; then
docker save caddy:2-alpine | gzip -1 | ssh "$host" 'gunzip | docker load'
fi
echo "== 启动"
ssh "$host" bash -s -- "$dir" <<'EOF'
set -euo pipefail
cd "$1"
if [[ ! -f .env ]]; then
echo '服务器上还没有 .env:cp .env.example .env 后设置 ACCESS_PASSWORD'
exit 1
fi
docker compose up -d --no-build --remove-orphans
docker image prune -f >/dev/null
# 等编译服务就绪;沙箱自检失败时部署算失败
for _ in $(seq 60); do
logs=$(docker compose logs app 2>&1)
if grep -q '沙箱(bubblewrap)无法启动' <<<"$logs"; then
echo "$logs" | tail -5
exit 1
fi
if grep -q 'C# 编译服务已就绪' <<<"$logs"; then
echo '== 部署成功'
exit 0
fi
sleep 2
done
echo '== 120 秒内没有启动完成'
docker compose logs --tail 20 app
exit 1
EOF
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
# 在服务器上执行:拉取代码、构建镜像、重启,等到编译服务就绪才算成功。
# GitHub Actions 测试通过后通过 SSH 调用它,也可以登录服务器手动执行。
# 用法:deploy/update.sh [提交号,默认 origin/main]
# 第一次部署前,按 deploy/README.md 在服务器上克隆仓库、准备好 .env
set -euo pipefail
# 整个脚本包在函数里:git reset 会在运行中替换本文件,bash 先读完函数再执行,不受影响
main() {
cd "$(dirname "$0")/.."
local ref=${1:-origin/main}
if [[ ! -f .env ]]; then
echo '还没有 .env:cp .env.example .env 后设置 ACCESS_PASSWORD'
exit 1
fi
echo "== 拉取代码"
# 代码从 origin(git.xuyue.cc)拉取。Actions 由推送到 GitHub 触发,
# 两个仓库推送有先后,这个提交可能还没到 origin,多等一会儿
for attempt in $(seq 10); do
git fetch --quiet origin
if git rev-parse --quiet --verify "$ref^{commit}" >/dev/null; then break; fi
if [[ $attempt == 10 ]]; then
echo "origin 上没有 $ref:先 git push origin main,再重新运行部署"
exit 1
fi
echo "origin 上还没有 $ref,10 秒后重试"
sleep 10
done
# 服务器上不改代码,配置都在 .env(不进 git,不受影响)
git checkout --quiet -B main
git reset --quiet --hard "$ref"
git log --oneline -1
echo "== 构建镜像并重启"
docker compose up -d --build --remove-orphans
docker image prune -f >/dev/null
# 等编译服务就绪;沙箱自检失败时部署失败
for _ in $(seq 60); do
logs=$(docker compose logs app 2>&1)
if grep -q '沙箱(bubblewrap)无法启动' <<<"$logs"; then
echo "$logs" | tail -5
exit 1
fi
if grep -q 'C# 编译服务已就绪' <<<"$logs"; then
echo '== 部署成功'
exit 0
fi
sleep 2
done
echo '== 120 秒内没有启动完成'
docker compose logs --tail 20 app
exit 1
}
main "$@"
+1 -1
View File
@@ -174,7 +174,7 @@ IOT/
| 部署 | 沙箱 | bubblewrap:每次运行一个独立命名空间(断网、只读、看不到其他进程),加 .NET 堆上限、CPU 时间上限 | | 部署 | 沙箱 | bubblewrap:每次运行一个独立命名空间(断网、只读、看不到其他进程),加 .NET 堆上限、CPU 时间上限 |
| 部署 | HTTPS | Nginx Proxy Manager(服务器上已有),经外部网络 `npm_proxy` 转发 | | 部署 | HTTPS | Nginx Proxy Manager(服务器上已有),经外部网络 `npm_proxy` 转发 |
| 部署 | 反向代理 / 压缩 | Caddy(只收 HTTP),网页文件由 Bun 直接提供 | | 部署 | 反向代理 / 压缩 | Caddy(只收 HTTP),网页文件由 Bun 直接提供 |
| 部署 | 进程管理 | Docker Compose;镜像在开发机构建后经 ssh 传到服务器(`deploy/deploy.sh`) | | 部署 | 进程管理 | Docker Compose;GitHub Actions 测试通过后 SSH 到服务器,服务器从 git.xuyue.cc 拉代码并构建(`deploy/update.sh`)。不从国外传镜像:跨境上传 130 MB 镜像 40 分钟传不完 |
| 部署 | 备份 | 计划:定时 `sqlite3 .backup` + 异地拷贝 | | 部署 | 备份 | 计划:定时 `sqlite3 .backup` + 异地拷贝 |
## 安全与服务器资源 ## 安全与服务器资源