93 lines
3.1 KiB
Bash
93 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
REPO_URL="${REPO_URL:-https://git.orionc.me/orion/filecodebox.git}"
|
|
PROD_REPO_DIR="${PROD_REPO_DIR:-/opt/src/filecodebox-production}"
|
|
PROD_DIR="${PROD_DIR:-/opt/1panel/apps/filecodebox/filecodebox}"
|
|
BRANCH="${BRANCH:-master}"
|
|
IMAGE="${IMAGE:-local/filecodebox:master}"
|
|
PORT="${PORT:-40157}"
|
|
|
|
log() { printf '[FileCodeBox更新] %s\n' "$*"; }
|
|
fail() { printf '[FileCodeBox更新失败] %s\n' "$*" >&2; exit 1; }
|
|
|
|
command -v git >/dev/null 2>&1 || fail '缺少 git'
|
|
command -v docker >/dev/null 2>&1 || fail '缺少 docker'
|
|
command -v curl >/dev/null 2>&1 || fail '缺少 curl'
|
|
[[ -d "$PROD_DIR" ]] || fail "生产目录不存在:$PROD_DIR"
|
|
|
|
log "拉取公开仓库 $REPO_URL 的 $BRANCH 分支"
|
|
if [[ ! -d "$PROD_REPO_DIR/.git" ]]; then
|
|
rm -rf "$PROD_REPO_DIR"
|
|
git clone "$REPO_URL" "$PROD_REPO_DIR"
|
|
fi
|
|
cd "$PROD_REPO_DIR"
|
|
git remote set-url origin "$REPO_URL"
|
|
git fetch origin "$BRANCH"
|
|
git checkout -B "$BRANCH" "origin/$BRANCH"
|
|
git reset --hard "origin/$BRANCH"
|
|
COMMIT="$(git rev-parse HEAD)"
|
|
log "当前提交:$COMMIT"
|
|
|
|
cd "$PROD_DIR"
|
|
mkdir -p backups
|
|
TS="$(date +%Y%m%d%H%M%S)"
|
|
cp -a docker-compose.yml "backups/docker-compose.yml.before-update.$TS"
|
|
[[ -f data/filecodebox.db ]] && cp -a data/filecodebox.db "backups/filecodebox.db.before-update.$TS"
|
|
|
|
log "同步 1Panel Compose 为本地 master 构建"
|
|
python3 - <<PY
|
|
from pathlib import Path
|
|
p = Path('docker-compose.yml')
|
|
s = p.read_text()
|
|
lines = []
|
|
for line in s.splitlines():
|
|
stripped = line.strip()
|
|
if stripped.startswith('image:'):
|
|
indent = line[:len(line) - len(line.lstrip())]
|
|
lines.append(indent + 'image: $IMAGE')
|
|
lines.append(indent + 'build:')
|
|
lines.append(indent + ' context: $PROD_REPO_DIR')
|
|
elif stripped.startswith('build:') or stripped.startswith('context:'):
|
|
continue
|
|
else:
|
|
lines.append(line)
|
|
p.write_text('\n'.join(lines) + '\n')
|
|
PY
|
|
|
|
log "构建并重建生产容器"
|
|
docker compose config >/dev/null
|
|
docker compose up -d --build filecodebox
|
|
|
|
log "等待服务恢复"
|
|
for i in $(seq 1 60); do
|
|
if curl -fsS "http://127.0.0.1:$PORT/" >/tmp/filecodebox-update-index.html; then
|
|
break
|
|
fi
|
|
sleep 1
|
|
if [[ "$i" == "60" ]]; then
|
|
docker logs --tail 120 filecodebox || true
|
|
fail '服务未恢复'
|
|
fi
|
|
done
|
|
|
|
if grep -q 'fcb-analytics.js' /tmp/filecodebox-update-index.html; then
|
|
fail '生产页面检测到测试增强脚本,已停止'
|
|
fi
|
|
|
|
log "同步 1Panel 安装实例记录"
|
|
python3 - <<'PY'
|
|
import sqlite3
|
|
from pathlib import Path
|
|
DB = '/opt/1panel/db/agent.db'
|
|
compose = Path('/opt/1panel/apps/filecodebox/filecodebox/docker-compose.yml').read_text()
|
|
conn = sqlite3.connect(DB)
|
|
conn.execute("update app_installs set docker_compose=?, status='Running', container_name='filecodebox', service_name='filecodebox', http_port=40157, updated_at=datetime('now','localtime') where name='filecodebox'", (compose,))
|
|
conn.commit()
|
|
conn.close()
|
|
PY
|
|
|
|
(systemctl restart 1panel-agent >/dev/null 2>&1 || true)
|
|
log "完成:生产已更新为 $BRANCH@$COMMIT,数据目录保持不变"
|
|
docker ps --filter name=filecodebox --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'
|