This commit is contained in:
dekun
2026-05-22 13:15:12 +08:00
parent 3f0f77d450
commit 5ccf39022a
5 changed files with 166 additions and 117 deletions
+38
View File
@@ -0,0 +1,38 @@
#!/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
}