🧰 添加测试与生产部署脚本
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
PROD_DIR="${PROD_DIR:-/opt/1panel/apps/filecodebox/filecodebox}"
|
||||
IMAGE="${IMAGE:-lanol/filecodebox:beta}"
|
||||
PORT="${PORT:-40157}"
|
||||
CONFIRM="${CONFIRM:-}"
|
||||
|
||||
log() { printf '\033[1;32m[deploy-prod-official]\033[0m %s\n' "$*"; }
|
||||
err() { printf '\033[1;31m[deploy-prod-official]\033[0m %s\n' "$*" >&2; }
|
||||
|
||||
command -v docker >/dev/null 2>&1 || { err "missing command: docker"; exit 1; }
|
||||
command -v curl >/dev/null 2>&1 || { err "missing command: curl"; exit 1; }
|
||||
|
||||
if [[ "$CONFIRM" != "YES" ]]; then
|
||||
cat >&2 <<'EOF'
|
||||
这是生产环境脚本,只做“官方镜像重建/更新”,不会部署测试分支改动。
|
||||
|
||||
如确认执行,请使用:
|
||||
CONFIRM=YES deploy-filecodebox-prod-official
|
||||
EOF
|
||||
exit 2
|
||||
fi
|
||||
|
||||
cd "$PROD_DIR"
|
||||
mkdir -p backups
|
||||
TS="$(date +%Y%m%d%H%M%S)"
|
||||
log "backup compose and database"
|
||||
cp -a docker-compose.yml "backups/docker-compose.yml.before-prod-official.$TS"
|
||||
if [[ -f data/filecodebox.db ]]; then
|
||||
cp -a data/filecodebox.db "backups/filecodebox.db.before-prod-official.$TS"
|
||||
fi
|
||||
|
||||
log "ensure official image in compose: $IMAGE"
|
||||
python3 - <<PY
|
||||
from pathlib import Path
|
||||
p=Path('docker-compose.yml')
|
||||
s=p.read_text()
|
||||
lines=[]
|
||||
changed=False
|
||||
for line in s.splitlines():
|
||||
if line.strip().startswith('image:'):
|
||||
indent=line[:len(line)-len(line.lstrip())]
|
||||
lines.append(indent+'image: $IMAGE')
|
||||
changed=True
|
||||
else:
|
||||
lines.append(line)
|
||||
if not changed:
|
||||
raise SystemExit('compose image line not found')
|
||||
p.write_text('\n'.join(lines)+'\n')
|
||||
PY
|
||||
|
||||
docker compose config >/dev/null
|
||||
log "pull official image if available"
|
||||
docker pull "$IMAGE" || true
|
||||
|
||||
log "recreate production container"
|
||||
docker compose up -d --no-build
|
||||
|
||||
log "wait for http://127.0.0.1:$PORT/"
|
||||
for i in $(seq 1 30); do
|
||||
if curl -fsS "http://127.0.0.1:$PORT/" >/tmp/filecodebox-prod-index.html; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
if [[ "$i" == "30" ]]; then
|
||||
err "production environment did not become ready"
|
||||
docker logs --tail 100 filecodebox || true
|
||||
exit 3
|
||||
fi
|
||||
done
|
||||
|
||||
if grep -q 'fcb-analytics.js' /tmp/filecodebox-prod-index.html; then
|
||||
err "生产环境仍包含测试增强脚本注入,已停止"
|
||||
exit 4
|
||||
fi
|
||||
|
||||
docker ps --filter name=filecodebox --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
|
||||
log "OK: production is official image and ready at http://127.0.0.1:$PORT/"
|
||||
Reference in New Issue
Block a user