Compare commits

..

2 Commits

4 changed files with 144 additions and 1 deletions

132
.gitea/workflows/deploy.yml Normal file
View File

@@ -0,0 +1,132 @@
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
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="${{ github.sha }}"
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}"
# Copy repo content to release dir (exclude .git, .env, venv, data)
rsync -a --exclude='.git' \
--exclude='.env' \
--exclude='.env.local' \
--exclude='.venv' \
--exclude='data/' \
--exclude='__pycache__' \
--exclude='.gitea' \
"${CLONE_DIR}/" "${RELEASE_DIR}/"
echo "=== 2.5 Ensure .env secrets ==="
mkdir -p "${DEPLOY_BASE}/shared"
SHARED_ENV="${DEPLOY_BASE}/shared/.env"
touch "${SHARED_ENV}"
ensure_env_var() {
key="$1"; val="$2"
if [ -z "$val" ]; then
echo " ! ${key} 未提供(CI secret 为空),跳过写入"
return
fi
if ! grep -q "^${key}=" "${SHARED_ENV}"; then
echo "${key}=${val}" >> "${SHARED_ENV}"
echo " + 已写入 ${key}"
else
echo " = ${key} 已存在,保留原值"
fi
}
ensure_env_var "SECRET_KEY" "${{ secrets.ZW_SECRET_KEY }}"
echo "=== 3. Link shared resources ==="
mkdir -p "${RELEASE_DIR}/data"
# .env from shared dir (not in git)
ln -sfn "${DEPLOY_BASE}/shared/.env" "${RELEASE_DIR}/.env"
# Database dir symlink to persist across releases
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 service ==="
if ! systemctl is-enabled "${SERVICE_NAME}" >/dev/null 2>&1; then
cat > "/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
[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
EOF
systemctl daemon-reload
systemctl enable "${SERVICE_NAME}"
fi
echo "=== 6. Restart service ==="
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 for service..."
sleep 2
done
# Final verify
if ! curl -fsS http://127.0.0.1:5058/api/health >/dev/null 2>&1; then
echo "ERROR: Health check failed after 5 attempts"
echo "Rolling back to previous release..."
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 old releases ==="
ls -dt "${DEPLOY_BASE}"/releases/*/ | tail -n +6 | xargs -r rm -rf
echo "=== 9. Cleanup temp ==="
rm -rf "${CLONE_DIR}"
echo "=== Deploy complete: ${RELEASE_ID} ==="

7
app.py
View File

@@ -406,6 +406,13 @@ def api_calendar_sync_all():
return jsonify({'ok': True, 'data': results, 'saved_days': saved_weeks})
# ── 健康检查 ──────────────────────────────────────────
@app.route('/api/health')
def api_health():
return jsonify({'ok': True, 'service': 'ziwei-power'})
# ── 启动 ──────────────────────────────────────────────
if __name__ == '__main__':

View File

@@ -353,7 +353,7 @@
div.innerHTML = '<span class="drag-handle" title="拖动排序">⋮⋮</span>' +
'<span class="idx">' + (idx+1) + '.</span>' +
'<input type="text" value="' + esc(text) + '" placeholder="本周最重要的一件事…">' +
'<button class="btn-note-toggle' + (hasNote ? ' has-note' : '') + '" onclick="openNoteModal(this)" title="备注">💬</button>' +
'<button class="btn-note-toggle' + (hasNote ? ' has-note' : '') + '" onclick="openNoteModal(this)" title="备注"><svg class="icon-sm"><use href="#icon-file-text"/></svg></button>' +
'<button class="btn-del" onclick="this.parentElement.remove();renumberMorning()"><svg class="icon-sm"><use href="#icon-x"/></svg></button>';
// 隐藏存储备注
var noteHidden = document.createElement('input');

View File

@@ -71,4 +71,8 @@
<symbol id="icon-star" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.563.563 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.563.563 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z"/>
</symbol>
<!-- 备注 -->
<symbol id="icon-file-text" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/>
</symbol>
</svg>

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB