Files
ESP32_GDEY042T81_server/.gitea/workflows/deploy.yaml
jeremygan2021 f91fabad0c
Some checks failed
Deploy to Server / deploy (push) Failing after 2s
action
2026-03-02 15:29:38 +08:00

113 lines
4.1 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Deploy to Server
on:
push:
branches:
- main
- master
jobs:
deploy:
runs-on: ubuntu
steps:
# 直接使用 expect 脚本处理交互,比 sshpass 更稳定,尤其是在 Alpine 上
- name: Install dependencies
run: |
if command -v apt-get &> /dev/null; then
apt-get update
apt-get install -y expect openssh-client
elif command -v apk &> /dev/null; then
apk update
apk add expect openssh-client bash
else
echo "Unknown package manager"
exit 1
fi
- name: Deploy to server
env:
HOST: "6.6.6.66"
USER: "quant-speed"
PASS: "123quant-speed"
TARGET_DIR: "/home/quant-speed/data/dev/ESP32_GDEY042T81_server"
run: |
# 创建 expect 脚本
cat > deploy_script.exp <<EOF
#!/usr/bin/expect -f
set timeout 300
set host "$HOST"
set user "$USER"
set password "$PASS"
set target_dir "$TARGET_DIR"
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \$user@\$host "bash -s"
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "\$password\r" }
}
# 发送远程命令
# 注意:这里的 EOF 结束符不能缩进
send "mkdir -p \$target_dir\r"
send "cd \$target_dir\r"
# 记录旧版本号
send "OLD_HEAD=\\\$(git rev-parse HEAD 2>/dev/null || echo '')\r"
# 拉取代码 - 可能需要 sudo 权限或者目录权限问题
send "echo 'Pulling latest code...'\r"
# 尝试直接 git pull如果失败则尝试 sudo git pull
# 但 sudo git pull 可能会因为 .git 目录的所有者问题而失败,或者需要输入密码
# 最好的方式是确保当前用户对目录有写权限。
# 既然用户提示 "Permission denied" 并且提到 "sudo",我们尝试在 git pull 前确保权限,或者用 sudo git pull
# 先尝试修正权限 (需要 sudo)
send "echo '\$password' | sudo -S chown -R \$user:\$user \$target_dir\r"
# 然后执行 git pull
send "git pull\r"
# 检查拉取是否成功
expect {
"Already up to date." { send "echo 'Code is up to date'\r" }
"Updating" { send "echo 'Code updated'\r" }
"Permission denied" {
puts "Git pull permission denied, trying with sudo..."
send "echo '\$password' | sudo -S git pull\r"
}
timeout { puts "Git pull timeout"; exit 1 }
}
# 获取新版本号
send "NEW_HEAD=\\\$(git rev-parse HEAD)\r"
# 判断是否需要构建
send "if \\[ \\\"\\\$OLD_HEAD\\\" == \\\"\\\$NEW_HEAD\\\" \\]; then\r"
send " echo 'No changes detected, skipping deploy'\r"
send "else\r"
# 检查构建文件变动
send " if git diff --name-only \\\$OLD_HEAD \\\$NEW_HEAD | grep -E 'Dockerfile|requirements.txt'; then\r"
send " echo 'Build files changed, rebuilding...'\r"
send " echo '\$password' | sudo -S docker compose down --rmi local\r"
send " echo '\$password' | sudo -S docker rmi epaper_server:latest || true\r"
send " echo '\$password' | sudo -S docker compose up -d --build\r"
send " else\r"
send " echo 'Only code changed, restarting container...'\r"
send " echo '\$password' | sudo -S docker compose down\r"
send " echo '\$password' | sudo -S docker compose up -d\r"
send " fi\r"
send "fi\r"
# 退出 SSH
send "exit\r"
expect eof
EOF
# 执行 expect 脚本
chmod +x deploy_script.exp
./deploy_script.exp