Files
Binance_Altcoin_Monitor/deploy/lib.sh
T
2026-05-22 13:18:14 +08:00

72 lines
2.1 KiB
Bash
Raw 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
# 部署脚本公共函数(LF 换行)
# 安全拉取远程代码:有本地修改时先 stash,pull 失败不中断部署
git_update_safe() {
local repo_dir="${1:?}"
cd "$repo_dir"
if [ ! -d .git ]; then
return 0
fi
if [ "${DEPLOY_SKIP_GIT_PULL:-}" = "1" ]; then
echo " 已跳过 git pull (DEPLOY_SKIP_GIT_PULL=1)"
return 0
fi
local stashed=0
if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then
echo " 检测到未提交修改,暂存到 git stash..."
if git stash push -m "deploy-auto-$(date +%Y%m%d%H%M%S)"; then
stashed=1
else
echo " 警告: stash 失败,将尝试直接 pull"
fi
fi
if git pull --rebase; then
echo " 代码已更新"
else
echo " 警告: git pull 失败,使用当前目录代码继续部署"
fi
if [ "$stashed" = 1 ]; then
echo " 恢复本地修改 (git stash pop)..."
git stash pop || echo " 提示: stash 恢复冲突时可手动处理: git stash list"
fi
}
# 创建 .venv 并安装依赖(不依赖系统 pip)
ensure_python_venv() {
local repo_dir="${1:?}"
cd "$repo_dir"
local venv_python="${repo_dir}/.venv/bin/python"
local venv_pip="${repo_dir}/.venv/bin/pip"
if [ ! -x "$venv_python" ]; then
echo " 创建虚拟环境 .venv ..."
if ! python3 -m venv .venv 2>/dev/null; then
echo " 安装 python3-venv(需要 root..."
if command -v apt-get &>/dev/null; then
sudo apt-get update -qq
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq python3-venv python3-pip
elif command -v yum &>/dev/null; then
sudo yum install -y python3-pip
elif command -v dnf &>/dev/null; then
sudo dnf install -y python3-pip
else
echo "错误: 无法创建 venv,请手动安装 python3-venv 或 python3-pip"
exit 1
fi
python3 -m venv .venv
fi
fi
echo " 安装 Python 依赖到 .venv ..."
"$venv_pip" install -U pip -q
"$venv_pip" install -r backend/requirements.txt -q
echo " 使用: $("$venv_python" --version 2>&1) ($venv_python)"
}