Files
deploy-test/dt.sh
2026-04-21 12:24:04 +07:00

253 lines
6.6 KiB
Bash
Executable File
Raw Permalink 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
# =============================================================================
# dt.sh — deploy-test 总控脚本
#
# 放置位置deploy-test 目录内。
#
# 功能:
# 1. 一键拉取 deploy-test 上级目录下所有 Git 工程的最新代码
# 2. 快捷调用 deploy-test 的编译、启动、停止、重启、状态检查
# 3. 快捷管理 deploy-test 前端服务
#
# 注意:
# - pull 只执行 fetch + pull --ff-only不会 git reset不会覆盖本地改动。
# - 如果某工程存在本地提交/本地改动导致无法快进,会跳过并提示手动处理。
# =============================================================================
set -euo pipefail
DEPLOY_TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$DEPLOY_TEST_DIR/.." && pwd)"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${BLUE}[INFO]${NC} $*"; }
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
err() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
usage() {
cat <<EOF
用法:
./deploy-test/dt.sh <command> [args...]
代码更新:
pull 拉取 deploy-test 上级目录下所有 Git 工程
deploy-test 后端/基础服务:
build 调用 deploy-test/04-build.sh
start 调用 deploy-test/05-start.sh
stop 调用 deploy-test/stop.sh
restart 调用 deploy-test/restart.sh
status 调用 deploy-test/status.sh
deploy-test 前端:
fe-start [project] 调用 deploy-test/07-start-frontend.sh [project](开发态前端)
fe-stop [project] 调用 deploy-test/stop-frontend.sh [project]
fe-build-static [project] 调用 deploy-test/08-build-static-frontend.sh [project]pc/cms/build-cms/build-down
fe-verify-static 调用 deploy-test/09-verify-static-frontends.sh
fe-publish [project] 构建静态前端 + 更新 Nginx + 校验(默认 all
常用组合:
up pull + build + restart
deploy pull + build + restart + status
其它:
help 显示帮助
示例:
./deploy-test/dt.sh pull
./deploy-test/dt.sh build
./deploy-test/dt.sh restart
./deploy-test/dt.sh fe-build-static pc
./deploy-test/dt.sh fe-verify-static
sudo ./deploy-test/dt.sh fe-publish
./deploy-test/dt.sh deploy
EOF
}
require_deploy_test() {
if [[ ! -d "$DEPLOY_TEST_DIR" ]]; then
err "未找到 deploy-test 目录: $DEPLOY_TEST_DIR"
exit 1
fi
}
require_root_for_publish() {
if [[ "$(id -u)" -ne 0 ]]; then
err "fe-publish 需要 root / sudo因为会执行 nginx 安装与重载"
echo "请执行: sudo ./deploy-test/dt.sh fe-publish${1:+ $1}"
exit 1
fi
}
run_deploy_test_script() {
local script="$1"
shift || true
require_deploy_test
local path="$DEPLOY_TEST_DIR/$script"
if [[ ! -f "$path" ]]; then
err "脚本不存在: $path"
exit 1
fi
if [[ ! -x "$path" ]]; then
chmod +x "$path" 2>/dev/null || true
fi
info "执行: ./deploy-test/$script $*"
(
cd "$ROOT_DIR"
"./deploy-test/$script" "$@"
)
}
current_branch() {
git symbolic-ref --quiet --short HEAD 2>/dev/null || true
}
update_one_repo() {
local repo_dir="$1"
local name
name="$(basename "$repo_dir")"
echo ""
info "更新工程: $name"
(
cd "$repo_dir"
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
warn "$name 不是有效 Git 工作区,跳过"
return 0
fi
local branch
branch="$(current_branch)"
if [[ -z "$branch" ]]; then
warn "$name 当前是 detached HEAD跳过自动 pull"
return 0
fi
local upstream
upstream="$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null || true)"
if [[ -z "$upstream" ]]; then
warn "$name 分支 $branch 未设置 upstream跳过自动 pull"
return 0
fi
if ! git fetch --all --prune; then
warn "$name git fetch 失败,跳过"
return 0
fi
if git diff --quiet && git diff --cached --quiet; then
if git pull --ff-only; then
ok "$name 已更新到最新 ($branch -> $upstream)"
else
warn "$name 无法 fast-forward请手动处理分支差异"
fi
else
warn "$name 存在本地未提交改动,只 fetch 不 pull避免覆盖你的修改"
local behind
behind="$(git rev-list --count "HEAD..$upstream" 2>/dev/null || echo "?")"
warn "$name 当前落后 upstream: $behind 个提交"
fi
)
}
update_all_repos() {
info "扫描 Git 工程: $ROOT_DIR"
local found=0
local repo
for repo in "$ROOT_DIR"/*; do
[[ -d "$repo/.git" ]] || continue
found=1
update_one_repo "$repo"
done
if [[ "$found" -eq 0 ]]; then
warn "未发现一级 Git 工程"
else
echo ""
ok "代码更新流程完成"
fi
}
main() {
local cmd="${1:-help}"
shift || true
case "$cmd" in
help|-h|--help)
usage
;;
pull|update)
update_all_repos
;;
build)
run_deploy_test_script "04-build.sh" "$@"
;;
start)
run_deploy_test_script "05-start.sh" "$@"
;;
stop)
run_deploy_test_script "stop.sh" "$@"
;;
restart)
run_deploy_test_script "restart.sh" "$@"
;;
status)
run_deploy_test_script "status.sh" "$@"
;;
fe-start)
run_deploy_test_script "07-start-frontend.sh" "$@"
;;
fe-stop)
run_deploy_test_script "stop-frontend.sh" "$@"
;;
fe-build-static)
run_deploy_test_script "08-build-static-frontend.sh" "$@"
;;
fe-verify-static)
run_deploy_test_script "09-verify-static-frontends.sh" "$@"
;;
fe-publish)
publish_target="${1:-all}"
require_root_for_publish "$publish_target"
run_deploy_test_script "08-build-static-frontend.sh" "$@"
run_deploy_test_script "00-init-tools.sh" nginx
run_deploy_test_script "09-verify-static-frontends.sh"
echo ""
ok "静态前端发布完成: ${publish_target}"
echo "已执行:"
echo " 1. 构建 dist"
echo " 2. 更新并重载 Nginx"
echo " 3. 校验静态站点入口"
echo "建议复查:"
echo " ./deploy-test/status.sh"
;;
up)
update_all_repos
run_deploy_test_script "04-build.sh"
run_deploy_test_script "restart.sh"
;;
deploy)
update_all_repos
run_deploy_test_script "04-build.sh"
run_deploy_test_script "restart.sh"
run_deploy_test_script "status.sh"
;;
*)
err "未知命令: $cmd"
echo ""
usage
exit 1
;;
esac
}
main "$@"