Files
IOT_SIM/deploy/deploy.sh
T
xuyueandClaude Opus 5.5 1cb2235c3d
CI/CD / test (push) Canceled after 0s
CI/CD / deploy (push) Canceled after 0s
GitHub Actions:测试和自动部署
- 每次推送和 PR:编译 C# 运行时,跑服务端测试(含 bubblewrap 沙箱)、网页端测试和类型检查
- 推送到 main 且测试通过:带缓存构建镜像,经 SSH 传到服务器并重启;Secrets 未配置时跳过部署
- 只在 github.com 上运行,git.xuyue.cc 上跳过
- deploy.sh:SKIP_BUILD 跳过构建;启动后等编译服务就绪,沙箱自检失败时部署失败;
  服务器目录默认 ~/IOT_SIM

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 15:56:09 +08:00

58 lines
1.8 KiB
Bash
Executable File
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.
#!/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