添加 ziwei-power 手动部署工作流

This commit is contained in:
mac
2026-07-28 20:34:31 +08:00
parent 2e5704a615
commit ee60b1e5ac

View File

@@ -0,0 +1,98 @@
name: Deploy ziwei-power
on:
workflow_dispatch:
jobs:
deploy-ziwei:
runs-on: prod-deploy
env:
DEPLOY_BASE: /opt/ziwei-power
REPO_URL: https://qiukai:${{ secrets.DEPLOY_TOKEN }}@git.qiukai.me/qiukai/ziwei-power.git
SERVICE_NAME: ziwei-power
steps:
- name: Clone and deploy
run: |
set -e
RELEASE_ID="$(date +%Y%m%d-%H%M%S)"
RELEASE_DIR="${DEPLOY_BASE}/releases/${RELEASE_ID}"
CLONE_DIR="/tmp/zw-deploy-${RELEASE_ID}"
echo "=== 1. Clone repository ==="
rm -rf "${CLONE_DIR}"
git clone --depth 1 --branch main "${REPO_URL}" "${CLONE_DIR}"
echo "=== 2. Prepare release directory ==="
rm -rf "${RELEASE_DIR}"
mkdir -p "${RELEASE_DIR}"
rsync -a --exclude='.git' --exclude='.env' --exclude='.env.local' --exclude='.venv' --exclude='data/' --exclude='__pycache__' --exclude='.gitea' "${CLONE_DIR}/" "${RELEASE_DIR}/"
echo "=== 3. Link shared resources ==="
mkdir -p "${DEPLOY_BASE}/shared"
mkdir -p "${RELEASE_DIR}/data"
ln -sfn "${DEPLOY_BASE}/shared/.env" "${RELEASE_DIR}/.env"
DB_DIR="$HOME/.workbuddy/data/ziwei-power"
mkdir -p "${DB_DIR}"
ln -sfn "${DB_DIR}" "${RELEASE_DIR}/data"
echo "=== 4. Setup Python venv ==="
cd "${RELEASE_DIR}"
python3 -m venv .venv
. .venv/bin/activate
pip install --no-cache-dir -r requirements.txt
echo "=== 5. Setup systemd (first run only) ==="
if ! systemctl is-enabled "${SERVICE_NAME}" >/dev/null 2>&1; then
cat > "/etc/systemd/system/${SERVICE_NAME}.service" <<SVCEOF
[Unit]
Description=ziwei-power 日课系统
After=network.target
[Service]
Type=simple
User=$USER
WorkingDirectory=${DEPLOY_BASE}/current
ExecStart=${DEPLOY_BASE}/current/.venv/bin/python ${DEPLOY_BASE}/current/app.py
Restart=always
RestartSec=5
Environment=PORT=5058
[Install]
WantedBy=multi-user.target
SVCEOF
systemctl daemon-reload
systemctl enable "${SERVICE_NAME}"
fi
echo "=== 6. Switch and restart ==="
ln -sfn "${RELEASE_DIR}" "${DEPLOY_BASE}/current"
systemctl restart "${SERVICE_NAME}"
sleep 3
echo "=== 7. Health check ==="
for i in 1 2 3 4 5; do
if curl -fsS http://127.0.0.1:5058/api/health >/dev/null 2>&1; then
echo "Health check passed"
break
fi
echo "Attempt $i: waiting..."
sleep 2
done
if ! curl -fsS http://127.0.0.1:5058/api/health >/dev/null 2>&1; then
echo "ERROR: Health check failed"
PREV=$(ls -t "${DEPLOY_BASE}/releases" | sed -n '2p')
if [ -n "${PREV}" ]; then
ln -sfn "${DEPLOY_BASE}/releases/${PREV}" "${DEPLOY_BASE}/current"
systemctl restart "${SERVICE_NAME}"
echo "Rolled back to ${PREV}"
fi
exit 1
fi
echo "=== 8. Cleanup ==="
ls -dt "${DEPLOY_BASE}"/releases/*/ | tail -n +6 | xargs -r rm -rf
rm -rf "${CLONE_DIR}"
echo "=== Deploy complete: ${RELEASE_ID} ==="