fix:优化生产部署脚本

This commit is contained in:
Daniel
2026-04-28 18:39:55 +08:00
parent f47453a656
commit 6de7e782fc

View File

@@ -9,12 +9,12 @@ set -Eeuo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT" cd "$PROJECT_ROOT"
BRANCH="${1:-main}" BRANCH_INPUT="${1:-}"
REMOTE="${2:-origin}" REMOTE="${2:-origin}"
echo "==> [prod] Project: $PROJECT_ROOT" echo "==> [prod] Project: $PROJECT_ROOT"
echo "==> [prod] Remote: $REMOTE" echo "==> [prod] Remote: $REMOTE"
echo "==> [prod] Branch: $BRANCH" echo "==> [prod] Branch: ${BRANCH_INPUT:-<auto>}"
if [[ ! -d ".git" ]]; then if [[ ! -d ".git" ]]; then
echo "Error: current directory is not a git repository." echo "Error: current directory is not a git repository."
@@ -29,6 +29,38 @@ fi
echo "==> [prod] Fetching latest code" echo "==> [prod] Fetching latest code"
git fetch "$REMOTE" --prune git fetch "$REMOTE" --prune
resolve_default_branch() {
local remote="$1"
local head_ref
head_ref="$(git symbolic-ref -q --short "refs/remotes/$remote/HEAD" 2>/dev/null || true)"
if [[ -n "$head_ref" ]]; then
echo "${head_ref#${remote}/}"
return 0
fi
head_ref="$(git remote show "$remote" 2>/dev/null | awk '/HEAD branch/ {print $NF; exit}')"
if [[ -n "$head_ref" ]]; then
echo "$head_ref"
return 0
fi
return 1
}
if [[ -n "$BRANCH_INPUT" ]]; then
BRANCH="$BRANCH_INPUT"
else
BRANCH="$(resolve_default_branch "$REMOTE" || true)"
if [[ -z "$BRANCH" ]]; then
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
fi
if [[ -z "$BRANCH" || "$BRANCH" == "HEAD" ]]; then
echo "Error: could not determine deploy branch. Please pass one explicitly:"
echo " sh deploy.prod.sh <branch> [remote]"
exit 1
fi
fi
echo "==> [prod] Using branch: $BRANCH"
if ! git show-ref --verify --quiet "refs/remotes/$REMOTE/$BRANCH"; then if ! git show-ref --verify --quiet "refs/remotes/$REMOTE/$BRANCH"; then
echo "Error: remote branch '$REMOTE/$BRANCH' not found." echo "Error: remote branch '$REMOTE/$BRANCH' not found."
exit 1 exit 1