From b1aae8aac4746345b63dbd3a35f5a65381b2a087 Mon Sep 17 00:00:00 2001 From: hy Date: Thu, 23 Apr 2026 12:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=80=E9=94=AE=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=8E=A8=E9=80=81=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/commit_push_and_deploy.sh | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 scripts/commit_push_and_deploy.sh diff --git a/scripts/commit_push_and_deploy.sh b/scripts/commit_push_and_deploy.sh new file mode 100755 index 0000000..9df7a3a --- /dev/null +++ b/scripts/commit_push_and_deploy.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" +ROOT_DIR="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)" + +if [ -x "$ROOT_DIR/.venv/bin/python" ]; then + PYTHON_BIN="$ROOT_DIR/.venv/bin/python" +else + PYTHON_BIN="python3" +fi + +fail() { + echo "$*" >&2 + exit 1 +} + +require_command() { + if ! command -v "$1" >/dev/null 2>&1; then + fail "missing command: $1" + fi +} + +current_branch() { + local branch + branch="$(git -C "$ROOT_DIR" rev-parse --abbrev-ref HEAD)" + if [ "$branch" = "HEAD" ]; then + fail "current git HEAD is detached; switch to a branch first" + fi + echo "$branch" +} + +has_upstream() { + git -C "$ROOT_DIR" rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' >/dev/null 2>&1 +} + +commit_message() { + if [ "$#" -gt 0 ]; then + printf '%s\n' "$1" + return 0 + fi + date '+deploy %Y-%m-%d %H:%M:%S' +} + +main() { + require_command git + require_command "$PYTHON_BIN" + + local branch + local message + branch="$(current_branch)" + message="$(commit_message "$@")" + + cd "$ROOT_DIR" + + # 统一先把当前工作区改动全部纳入本次发布。 + git add -A + + # 没有新增改动时跳过提交,仍然允许继续推送和部署。 + if ! git diff --cached --quiet; then + git commit -m "$message" + else + echo "no local changes to commit" + fi + + if has_upstream; then + git push + else + git push -u origin "$branch" + fi + + # 强制让远端按当前分支拉取,避免部署脚本和本地分支漂移。 + MONITOR_REPO_REF="$branch" \ + MONITOR_REPO_URL="$(git -C "$ROOT_DIR" remote get-url origin)" \ + "$PYTHON_BIN" "$ROOT_DIR/ops/deploy_via_tat.py" +} + +main "$@"