From c39a8f5f80041a2f6b6b19d4feb635c494f01aa3 Mon Sep 17 00:00:00 2001 From: chenhang Date: Fri, 12 Jun 2026 14:39:09 +0800 Subject: [PATCH] refactor(deploy): parameterize git remote in deploy scripts Replace hardcoded GIT_REMOTE variable with a function parameter so the shared deploy library can push to either the production or test remote from the same script. Rename push_to_server to push_to_remote and update both deploy_web.sh and deploy_web_test.sh to pass the appropriate remote. --- scripts/deploy/_deploy_lib.sh | 27 +++++++++++++-------------- scripts/deploy/deploy_web.sh | 2 +- scripts/deploy/deploy_web_test.sh | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/scripts/deploy/_deploy_lib.sh b/scripts/deploy/_deploy_lib.sh index 947a5a7b..01fce4b3 100755 --- a/scripts/deploy/_deploy_lib.sh +++ b/scripts/deploy/_deploy_lib.sh @@ -1,21 +1,20 @@ #!/bin/bash -# Git remote 名(在仓库根目录配置): -# git remote add server root@8.166.137.51:/root/frontend/cozsweet -GIT_REMOTE="test" - -# 推送指定分支到服务器 +# 推送指定分支到指定远端 # 期望服务器端 post-receive hook 已就位;本脚本只做 git push,不负责构建/启动 -push_to_server() { - local branch="$1" +# 调用方:deploy_web.sh(production)→ push_to_remote "production" "main" +# deploy_web_test.sh(test)→ push_to_remote "test" "test" +push_to_remote() { + local remote="$1" + local branch="$2" - if [ -z "$branch" ]; then - echo "错误: 未指定分支名" + if [ -z "$remote" ] || [ -z "$branch" ]; then + echo "错误: 未指定 remote 或 branch" return 1 fi echo "==========================================" - echo "git push $GIT_REMOTE $branch ..." + echo "git push $remote $branch ..." echo "==========================================" local current=$(git branch --show-current 2>/dev/null || echo "unknown") @@ -24,13 +23,13 @@ push_to_server() { fi # 检查 remote 是否存在 - if ! git remote get-url "$GIT_REMOTE" >/dev/null 2>&1; then - echo "错误: git remote '$GIT_REMOTE' 未配置" - echo " 请运行: git remote add $GIT_REMOTE root@8.166.137.51:/root/frontend/cozsweet" + if ! git remote get-url "$remote" >/dev/null 2>&1; then + echo "错误: git remote '$remote' 未配置" + echo " 请运行: git remote add $remote " return 1 fi - git push "$GIT_REMOTE" "$branch" + git push "$remote" "$branch" if [ $? -eq 0 ]; then echo "==========================================" diff --git a/scripts/deploy/deploy_web.sh b/scripts/deploy/deploy_web.sh index d5fdf377..d64d2939 100755 --- a/scripts/deploy/deploy_web.sh +++ b/scripts/deploy/deploy_web.sh @@ -9,7 +9,7 @@ source "$SCRIPT_DIR/_deploy_lib.sh" # 主函数 main() { - push_to_server "main" + push_to_remote "production" "main" NEW_VERSION=$(bump_version) purge_cdn_cache } diff --git a/scripts/deploy/deploy_web_test.sh b/scripts/deploy/deploy_web_test.sh index f89e680c..90181b1b 100755 --- a/scripts/deploy/deploy_web_test.sh +++ b/scripts/deploy/deploy_web_test.sh @@ -9,7 +9,7 @@ source "$SCRIPT_DIR/_deploy_lib.sh" # 主函数 main() { - push_to_server "test" + push_to_remote "test" "test" purge_cdn_cache }