diff --git a/README.md b/README.md index 2b83fb0..b9a40de 100644 --- a/README.md +++ b/README.md @@ -10,52 +10,24 @@ ZK Data Agent 是面向中控数据开发流程的 Agent 服务。当前仓库 ### 从 Git 一键部署 -适合首次部署。该命令会在拉取代码后执行 `--bootstrap-system`,必要时使用 sudo 安装 Ubuntu 系统依赖;应用本身仍部署在用户目录。 +适合首次部署。首次安装会默认初始化 Ubuntu 系统依赖,必要时请求 sudo;应用本身仍部署在用户目录。 ```bash -APP_DIR="$HOME/zk-data-agent" BRANCH="main" REPO="git@git.n.xiaomi.com:wuyang6/zk-data-agent.git" bash -lc ' -set -euo pipefail - -if [ -d "$APP_DIR/.git" ]; then - cd "$APP_DIR" - git fetch origin - git checkout "$BRANCH" - git pull --ff-only origin "$BRANCH" -else - rm -rf "$APP_DIR" - git clone --branch "$BRANCH" "$REPO" "$APP_DIR" - cd "$APP_DIR" -fi - -bash scripts/deploy-ubuntu.sh "$BRANCH" --skip-git --bootstrap-system -' +git clone git@git.n.xiaomi.com:wuyang6/zk-data-agent.git "$HOME/zk-data-agent" || true +bash "$HOME/zk-data-agent/scripts/install-from-git.sh" ``` 后续更新不需要安装系统依赖,可以使用: ```bash -APP_DIR="$HOME/zk-data-agent" BRANCH="main" REPO="git@git.n.xiaomi.com:wuyang6/zk-data-agent.git" bash -lc ' -set -euo pipefail - -if [ -d "$APP_DIR/.git" ]; then - cd "$APP_DIR" - git fetch origin - git checkout "$BRANCH" - git pull --ff-only origin "$BRANCH" -else - rm -rf "$APP_DIR" - git clone --branch "$BRANCH" "$REPO" "$APP_DIR" - cd "$APP_DIR" -fi - -bash scripts/deploy-ubuntu.sh "$BRANCH" --skip-git -' +bash "$HOME/zk-data-agent/scripts/install-from-git.sh" ``` -修改部署目录或分支时,只需要调整命令开头的变量: +默认部署目录是 `$HOME/zk-data-agent`,默认分支是 `main`。修改部署目录或分支时再加变量: ```bash -APP_DIR="$HOME/zk-data-agent-test" BRANCH="main" REPO="git@git.n.xiaomi.com:wuyang6/zk-data-agent.git" bash -lc '...' +git clone git@git.n.xiaomi.com:wuyang6/zk-data-agent.git "$HOME/zk-data-agent-test" || true +APP_DIR="$HOME/zk-data-agent-test" bash "$HOME/zk-data-agent-test/scripts/install-from-git.sh" ``` 这个命令会自动完成: diff --git a/scripts/deploy-ubuntu.sh b/scripts/deploy-ubuntu.sh index 8f14994..3b9c9b6 100755 --- a/scripts/deploy-ubuntu.sh +++ b/scripts/deploy-ubuntu.sh @@ -25,7 +25,7 @@ Options: --force 使用 git reset --hard origin/ 覆盖本地改动 --skip-git 跳过 git 拉取,只部署当前工作区 --bootstrap-system - 首次安装时使用 sudo 安装 Ubuntu 系统依赖,应用仍部署在用户目录 + 强制使用 sudo 安装 Ubuntu 系统依赖;首次安装会默认执行 EOF } @@ -81,7 +81,7 @@ require_command() { } bootstrap_system_packages() { - if [[ "${BOOTSTRAP_SYSTEM}" != "1" ]]; then + if [[ "${BOOTSTRAP_SYSTEM}" != "1" && -f "${ENV_FILE}" ]]; then return fi require_command sudo "首次安装系统依赖需要 sudo。" diff --git a/scripts/install-from-git.sh b/scripts/install-from-git.sh index 2724237..e2d19bd 100755 --- a/scripts/install-from-git.sh +++ b/scripts/install-from-git.sh @@ -54,6 +54,15 @@ command -v git >/dev/null 2>&1 || { exit 1 } +if [[ -e "${APP_DIR}" && ! -d "${APP_DIR}/.git" ]]; then + echo "${APP_DIR} already exists but is not a git repository." >&2 + echo "Please move it away, or rerun with --force to replace it." >&2 + if [[ "${FORCE}" != "1" ]]; then + exit 1 + fi + rm -rf "${APP_DIR}" +fi + if [[ -d "${APP_DIR}/.git" ]]; then cd "${APP_DIR}" git fetch origin