action
All checks were successful
Deploy to Server / deploy (push) Successful in 4m27s

This commit is contained in:
jeremygan2021
2026-03-02 15:37:39 +08:00
parent 59f174322a
commit e515395b55

View File

@@ -31,6 +31,7 @@ jobs:
USER: "quant" USER: "quant"
PASS: "123quant-speed" PASS: "123quant-speed"
TARGET_DIR: "/home/quant-speed/data/dev/ESP32_GDEY042T81_server" TARGET_DIR: "/home/quant-speed/data/dev/ESP32_GDEY042T81_server"
REPO_URL: "https://gitea.tangledup-ai.com/Tangledup-ai/ESP32_GDEY042T81_server.git"
run: | run: |
# 创建一个 shell 脚本,包含所有需要在服务器上执行的逻辑 # 创建一个 shell 脚本,包含所有需要在服务器上执行的逻辑
# 这样可以避免在 expect 中处理复杂的条件判断和转义 # 这样可以避免在 expect 中处理复杂的条件判断和转义
@@ -41,6 +42,7 @@ jobs:
PASSWORD="$1" PASSWORD="$1"
TARGET_DIR="$2" TARGET_DIR="$2"
RUN_USER="$3" RUN_USER="$3"
REPO_URL="$4"
# 1. 确保目录存在 (脚本已通过 sudo 运行) # 1. 确保目录存在 (脚本已通过 sudo 运行)
if [ ! -d "$TARGET_DIR" ]; then if [ ! -d "$TARGET_DIR" ]; then
@@ -55,26 +57,51 @@ jobs:
# 4. 执行 Git 操作 (以用户身份执行,避免 .git 权限问题) # 4. 执行 Git 操作 (以用户身份执行,避免 .git 权限问题)
# 使用 sudo -u 切换到普通用户执行 git # 使用 sudo -u 切换到普通用户执行 git
echo "Pulling latest code as $RUN_USER..." echo "Deploying code as $RUN_USER..."
OLD_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD 2>/dev/null || echo "")
if ! sudo -u "$RUN_USER" git pull; then FRESH_CLONE=0
if [ ! -d ".git" ]; then
echo "Not a git repository. Cloning from $REPO_URL..."
# Handle non-empty dir if necessary
if [ "$(ls -A)" ]; then
echo "Directory not empty. initializing git..."
sudo -u "$RUN_USER" git init
sudo -u "$RUN_USER" git remote add origin "$REPO_URL"
sudo -u "$RUN_USER" git fetch origin
sudo -u "$RUN_USER" git checkout -b main --track origin/main || sudo -u "$RUN_USER" git reset --hard origin/main
else
sudo -u "$RUN_USER" git clone "$REPO_URL" .
fi
FRESH_CLONE=1
fi
if [ "$FRESH_CLONE" -eq 0 ]; then
OLD_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD 2>/dev/null || echo "")
echo "Pulling latest code..."
if ! sudo -u "$RUN_USER" git pull origin main; then
echo "Git pull failed" echo "Git pull failed"
exit 1 exit 1
fi fi
NEW_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD) NEW_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD)
if [ "$OLD_HEAD" == "$NEW_HEAD" ]; then if [ "$OLD_HEAD" == "$NEW_HEAD" ]; then
echo "No changes detected, skipping deploy" echo "No changes detected, skipping deploy"
# exit 0 # 即使代码没变,如果用户想强制重启也可以注释掉这行
exit 0 exit 0
fi fi
else
OLD_HEAD=""
NEW_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD)
fi
# 5. 执行 Docker 操作 (以 root 身份执行) # 5. 执行 Docker 操作 (以 root 身份执行)
# 检查构建文件变动 # 检查构建文件变动
if sudo -u "$RUN_USER" git diff --name-only $OLD_HEAD $NEW_HEAD | grep -E 'Dockerfile|requirements.txt'; then FORCE_REBUILD=0
echo "Build files changed, rebuilding..." if [ "$FRESH_CLONE" -eq 1 ]; then
FORCE_REBUILD=1
fi
if [ "$FORCE_REBUILD" -eq 1 ] || sudo -u "$RUN_USER" git diff --name-only $OLD_HEAD $NEW_HEAD | grep -E 'Dockerfile|requirements.txt'; then
echo "Build files changed or fresh clone, rebuilding..."
docker compose down --rmi local docker compose down --rmi local
docker rmi epaper_server:latest || true docker rmi epaper_server:latest || true
docker compose up -d --build docker compose up -d --build
@@ -94,6 +121,7 @@ jobs:
set user "$USER" set user "$USER"
set password "$PASS" set password "$PASS"
set target_dir "$TARGET_DIR" set target_dir "$TARGET_DIR"
set repo_url "$REPO_URL"
# 1. 上传脚本到 /tmp (避免目标目录权限问题) # 1. 上传脚本到 /tmp (避免目标目录权限问题)
spawn scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null remote_script.sh \$user@\$host:/tmp/luna_deploy.sh spawn scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null remote_script.sh \$user@\$host:/tmp/luna_deploy.sh
@@ -105,7 +133,7 @@ jobs:
# 2. SSH 登录并执行 sudo bash /tmp/luna_deploy.sh # 2. SSH 登录并执行 sudo bash /tmp/luna_deploy.sh
# 我们把密码传给脚本,让脚本内部决定怎么用,或者直接用 sudo 执行脚本 # 我们把密码传给脚本,让脚本内部决定怎么用,或者直接用 sudo 执行脚本
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t \$user@\$host "echo '\$password' | sudo -S bash /tmp/luna_deploy.sh '\$password' '\$target_dir' '\$user'" spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t \$user@\$host "echo '\$password' | sudo -S bash /tmp/luna_deploy.sh '\$password' '\$target_dir' '\$user' '\$repo_url'"
expect { expect {
"password:" { send "\$password\r" } "password:" { send "\$password\r" }
} }