Compare commits
13 Commits
a1e8c042ca
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e515395b55 | ||
|
|
59f174322a | ||
|
|
11e4de9071 | ||
|
|
75cfb27bb1 | ||
|
|
f91fabad0c | ||
|
|
ae2da39496 | ||
|
|
bb814061e7 | ||
|
|
d74eb795c3 | ||
|
|
8b8e1d51ce | ||
|
|
4a36952484 | ||
|
|
8b672d026d | ||
|
|
841bf23d4d | ||
|
|
373ce8cb2e |
@@ -10,36 +10,138 @@ jobs:
|
|||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu
|
runs-on: ubuntu
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
# 直接使用 expect 脚本处理交互,比 sshpass 更稳定,尤其是在 Alpine 上
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
# 建议在 Gitea 仓库设置中配置 Secrets,而不是直接写在文件中
|
- name: Install dependencies
|
||||||
# SERVER_HOST, SERVER_USER, SERVER_PASSWORD
|
run: |
|
||||||
- name: Copy files to server
|
if command -v apt-get &> /dev/null; then
|
||||||
uses: appleboy/scp-action@v0.1.7
|
apt-get update
|
||||||
with:
|
apt-get install -y expect openssh-client
|
||||||
host: "6.6.6.66"
|
elif command -v apk &> /dev/null; then
|
||||||
username: "quant-speed"
|
apk update
|
||||||
password: "123quant-speed"
|
apk add expect openssh-client bash
|
||||||
source: "."
|
else
|
||||||
target: "/home/quant-speed/luna2025"
|
echo "Unknown package manager"
|
||||||
# 排除不需要上传的文件
|
exit 1
|
||||||
exclude: ".git,.github,.gitea,.trae,__pycache__,*.pyc,*.pyo,*.pyd,.DS_Store"
|
fi
|
||||||
|
|
||||||
- name: Deploy on server
|
- name: Deploy to server
|
||||||
uses: appleboy/ssh-action@v1.0.3
|
env:
|
||||||
with:
|
HOST: "6.6.6.66"
|
||||||
host: "6.6.6.66"
|
USER: "quant"
|
||||||
username: "quant-speed"
|
PASS: "123quant-speed"
|
||||||
password: "123quant-speed"
|
TARGET_DIR: "/home/quant-speed/data/dev/ESP32_GDEY042T81_server"
|
||||||
script: |
|
REPO_URL: "https://gitea.tangledup-ai.com/Tangledup-ai/ESP32_GDEY042T81_server.git"
|
||||||
# 进入项目目录
|
run: |
|
||||||
cd /home/quant-speed/luna2025
|
# 创建一个 shell 脚本,包含所有需要在服务器上执行的逻辑
|
||||||
|
# 这样可以避免在 expect 中处理复杂的条件判断和转义
|
||||||
|
cat > remote_script.sh <<'EOS'
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
# 停止容器并删除同名镜像
|
# 获取密码
|
||||||
# --rmi local: 删除服务构建的镜像
|
PASSWORD="$1"
|
||||||
echo "123quant-speed" | sudo -S docker compose down --rmi local
|
TARGET_DIR="$2"
|
||||||
echo "123quant-speed" | sudo -S docker rmi epaper_server:latest || true
|
RUN_USER="$3"
|
||||||
|
REPO_URL="$4"
|
||||||
|
|
||||||
# 启动服务
|
# 1. 确保目录存在 (脚本已通过 sudo 运行)
|
||||||
echo "123quant-speed" | sudo -S docker compose up -d --build
|
if [ ! -d "$TARGET_DIR" ]; then
|
||||||
|
mkdir -p "$TARGET_DIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 2. 修正权限,确保用户拥有目录
|
||||||
|
chown -R "$RUN_USER:$RUN_USER" "$TARGET_DIR"
|
||||||
|
|
||||||
|
# 3. 进入目录
|
||||||
|
cd "$TARGET_DIR"
|
||||||
|
|
||||||
|
# 4. 执行 Git 操作 (以用户身份执行,避免 .git 权限问题)
|
||||||
|
# 使用 sudo -u 切换到普通用户执行 git
|
||||||
|
echo "Deploying code as $RUN_USER..."
|
||||||
|
|
||||||
|
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"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
NEW_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD)
|
||||||
|
|
||||||
|
if [ "$OLD_HEAD" == "$NEW_HEAD" ]; then
|
||||||
|
echo "No changes detected, skipping deploy"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
OLD_HEAD=""
|
||||||
|
NEW_HEAD=$(sudo -u "$RUN_USER" git rev-parse HEAD)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 5. 执行 Docker 操作 (以 root 身份执行)
|
||||||
|
# 检查构建文件变动
|
||||||
|
FORCE_REBUILD=0
|
||||||
|
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 rmi epaper_server:latest || true
|
||||||
|
docker compose up -d --build
|
||||||
|
else
|
||||||
|
echo "Only code changed, restarting container..."
|
||||||
|
docker compose down
|
||||||
|
docker compose up -d
|
||||||
|
fi
|
||||||
|
EOS
|
||||||
|
|
||||||
|
# 创建 expect 脚本,负责上传到 /tmp 并 sudo 执行
|
||||||
|
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"
|
||||||
|
set repo_url "$REPO_URL"
|
||||||
|
|
||||||
|
# 1. 上传脚本到 /tmp (避免目标目录权限问题)
|
||||||
|
spawn scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null remote_script.sh \$user@\$host:/tmp/luna_deploy.sh
|
||||||
|
expect {
|
||||||
|
"yes/no" { send "yes\r"; exp_continue }
|
||||||
|
"password:" { send "\$password\r" }
|
||||||
|
}
|
||||||
|
expect eof
|
||||||
|
|
||||||
|
# 2. SSH 登录并执行 sudo bash /tmp/luna_deploy.sh
|
||||||
|
# 我们把密码传给脚本,让脚本内部决定怎么用,或者直接用 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' '\$repo_url'"
|
||||||
|
expect {
|
||||||
|
"password:" { send "\$password\r" }
|
||||||
|
}
|
||||||
|
|
||||||
|
# 保持交互直到脚本执行完毕
|
||||||
|
expect eof
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 执行 expect 脚本
|
||||||
|
chmod +x deploy_script.exp
|
||||||
|
./deploy_script.exp
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ services:
|
|||||||
- "8199:8199"
|
- "8199:8199"
|
||||||
volumes:
|
volumes:
|
||||||
- ./static:/app/static
|
- ./static:/app/static
|
||||||
|
- ./:/app
|
||||||
env_file:
|
env_file:
|
||||||
- .env.docker
|
- .env.docker
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|||||||
Reference in New Issue
Block a user