79 lines
1.6 KiB
Bash
Executable File
79 lines
1.6 KiB
Bash
Executable File
#!/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 "$@"
|