#!/usr/bin/env bash set -euo pipefail REPO_URL="${REPO_URL:-http://127.0.0.1:3000/orion/filecodebox.git}" TOKEN_FILE="${TOKEN_FILE:-/root/.cache/gitea-filecodebox-token}" 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}" CONFIRM="${CONFIRM:-}" log() { printf '\033[1;32m[deploy-prod-master]\033[0m %s\n' "$*"; } err() { printf '\033[1;31m[deploy-prod-master]\033[0m %s\n' "$*" >&2; } command -v git >/dev/null 2>&1 || { err "missing command: git"; exit 1; } 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 < /app/data 如确认执行,请使用: CONFIRM=YES deploy-filecodebox-prod-master EOF exit 2 fi log "prepare production source: $REPO_URL#$BRANCH" AUTH_REPO_URL="$REPO_URL" if [[ -f "$TOKEN_FILE" && "$REPO_URL" == http://127.0.0.1:3000/* ]]; then TOKEN="$(tr -d '\n' < "$TOKEN_FILE")" AUTH_REPO_URL="http://orion:${TOKEN}@127.0.0.1:3000/orion/filecodebox.git" fi if [[ ! -d "$PROD_REPO_DIR/.git" ]]; then rm -rf "$PROD_REPO_DIR" git clone "$AUTH_REPO_URL" "$PROD_REPO_DIR" fi cd "$PROD_REPO_DIR" git remote set-url origin "$AUTH_REPO_URL" git fetch origin "$BRANCH" git checkout -B "$BRANCH" "origin/$BRANCH" git reset --hard "origin/$BRANCH" commit="$(git rev-parse HEAD)" log "production source commit: $commit" 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-master.$TS" if [[ -f data/filecodebox.db ]]; then cp -a data/filecodebox.db "backups/filecodebox.db.before-prod-master.$TS" fi log "write production compose: build from $PROD_REPO_DIR" python3 - </dev/null log "build $IMAGE" docker compose build filecodebox log "recreate production container" docker compose up -d --no-build filecodebox log "wait for http://127.0.0.1:$PORT/" for i in $(seq 1 45); do if curl -fsS "http://127.0.0.1:$PORT/" >/tmp/filecodebox-prod-index.html; then break fi sleep 1 if [[ "$i" == "45" ]]; 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 "生产 master 环境包含测试增强脚本注入,已停止" exit 4 fi docker ps --filter name=filecodebox --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}' log "OK: production is built from $BRANCH@$commit and ready at http://127.0.0.1:$PORT/"