Files
ESP32_GDEY042T81_server/.gitea/workflows/deploy.yaml
jeremygan2021 d74eb795c3
Some checks failed
Deploy to Server / deploy (push) Failing after 3s
action
2026-03-02 14:27:01 +08:00

74 lines
2.8 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:
# 直接使用 sshpass 连接服务器操作,完全绕过本地 Checkout 和 Node.js 环境依赖
# 因为我们的逻辑全部都在服务器上执行git pull所以本地 Runner 只需要能连上 SSH 即可
- name: Install dependencies
run: |
if command -v apt-get &> /dev/null; then
apt-get update
apt-get install -y sshpass openssh-client
elif command -v apk &> /dev/null; then
apk update
apk add sshpass openssh-client bash
else
echo "Unknown package manager"
exit 1
fi
- name: Deploy to server
env:
HOST: "6.6.6.66"
USER: "quant-speed"
SSHPASS: "123quant-speed" # sshpass 使用 SSHPASS 环境变量
# 目标目录
TARGET_DIR: "/home/quant-speed/data/dev/ESP32_GDEY042T81_server"
run: |
# 远程执行部署命令
# 使用 -e 从环境变量读取密码,避免特殊字符问题
# 添加 -o UserKnownHostsFile=/dev/null 避免 host key 问题
sshpass -e ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $USER@$HOST "bash -s" <<EOF
# 确保目标目录存在
mkdir -p $TARGET_DIR
cd $TARGET_DIR
# 记录当前 HEAD用于后续 diff
OLD_HEAD=\$(git rev-parse HEAD 2>/dev/null || echo "")
# 拉取最新代码
echo "正在拉取最新代码..."
git pull || { echo "Git pull failed"; exit 1; }
NEW_HEAD=\$(git rev-parse HEAD)
# 检查是否有代码更新
if [ "\$OLD_HEAD" == "\$NEW_HEAD" ]; then
echo "代码没有变化,无需重启"
exit 0
fi
# 检查是否需要重新构建镜像 (Dockerfile 或 requirements.txt 变动)
# 使用 git diff 检查此次 pull 更新的文件列表
if git diff --name-only \$OLD_HEAD \$NEW_HEAD | grep -E 'Dockerfile|requirements.txt'; then
echo "检测到构建文件变动,开始重新构建镜像..."
echo "$SSHPASS" | sudo -S docker compose down --rmi local
# 确保清理干净
echo "$SSHPASS" | sudo -S docker rmi epaper_server:latest || true
echo "$SSHPASS" | sudo -S docker compose up -d --build
else
echo "仅代码变动,无需重新构建镜像,重启容器..."
echo "$SSHPASS" | sudo -S docker compose down
echo "$SSHPASS" | sudo -S docker compose up -d
fi
EOF