Files
font2pic/deploy.sh
2026-02-10 13:47:16 +08:00

87 lines
2.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
# -----------------------------------------------------------------------------
# deploy.sh
#
# 用途
# - 一键执行本地推送 + 远端两台服务器拉取代码A/B
# - 默认部署分支main。
#
# 执行流程
# 1) 运行本地推送脚本(默认 ./gitrun.sh
# 2) 服务器 A 执行 git fetch + checkout + pull --ff-only
# 3) 服务器 B 执行 git fetch + checkout + pull --ff-only
#
# 前置条件
# - 本机已配置 SSH 免密登录到 A/B 两台服务器。
# - 远端目录已存在 Git 仓库(默认 ~/font2svg
# - 本地 GITRUN_SCRIPT 可执行。
#
# 可覆盖环境变量
# - GITRUN_SCRIPT 本地推送脚本路径(默认 ./gitrun.sh
# - DEPLOY_BRANCH 部署分支(默认 main
# - REMOTE_WORKDIR 远端项目目录(默认 ~/font2svg
# - SERVER_A 服务器 A默认 gavin@mac.biboer.cn
# - SERVER_A_PORT 服务器 A SSH 端口(默认 22
# - SERVER_B 服务器 B默认 gavin@biboer.cn
# - SERVER_B_PORT 服务器 B SSH 端口(默认 21174
#
# 示例
# - 使用默认配置:
# bash deploy.sh
# - 指定分支:
# DEPLOY_BRANCH=release bash deploy.sh
# - 指定远端目录:
# REMOTE_WORKDIR=~/apps/font2svg bash deploy.sh
#
# 失败策略
# - set -euo pipefail任一步失败立即退出避免部分成功导致状态不一致。
# -----------------------------------------------------------------------------
set -euo pipefail
# 部署配置(可通过环境变量覆盖)
GITRUN_SCRIPT="${GITRUN_SCRIPT:-./gitrun.sh}"
DEPLOY_BRANCH="${DEPLOY_BRANCH:-main}"
REMOTE_WORKDIR="${REMOTE_WORKDIR:-~/font2svg}"
SERVER_A="${SERVER_A:-gavin@mac.biboer.cn}"
SERVER_A_PORT="${SERVER_A_PORT:-22}"
SERVER_B="${SERVER_B:-gavin@biboer.cn}"
SERVER_B_PORT="${SERVER_B_PORT:-21174}"
SSH_OPTS=(
-o BatchMode=yes
-o ConnectTimeout=8
-o StrictHostKeyChecking=accept-new
)
log_info() {
echo "[INFO] $1"
}
run_local_push() {
if [[ ! -x "$GITRUN_SCRIPT" ]]; then
echo "[ERROR] git 推送脚本不可执行: $GITRUN_SCRIPT"
echo "请先执行: chmod +x $GITRUN_SCRIPT"
exit 1
fi
log_info "执行本地推送脚本: $GITRUN_SCRIPT"
"$GITRUN_SCRIPT"
}
run_remote_pull() {
local host="$1"
local port="$2"
log_info "远程拉取: $host:$port ($DEPLOY_BRANCH)"
ssh "${SSH_OPTS[@]}" -p "$port" "$host" \
"cd $REMOTE_WORKDIR && git fetch origin && git checkout $DEPLOY_BRANCH && git pull --ff-only origin $DEPLOY_BRANCH"
}
main() {
run_local_push
run_remote_pull "$SERVER_A" "$SERVER_A_PORT"
run_remote_pull "$SERVER_B" "$SERVER_B_PORT"
log_info "部署完成"
}
main "$@"