Files
MarceloZoeng 26a5f99587
All checks were successful
CI / lint-and-build (push) Successful in 8s
中文+README
2026-06-14 12:08:27 +08:00

92 lines
2.7 KiB
YAML
Raw Permalink 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: CD
on:
push:
tags:
- "v*"
workflow_dispatch:
env:
DEPLOY_DIR: /opt/syscall_monitor
HEALTH_URL: http://127.0.0.1:5000
jobs:
deploy:
runs-on: self-hosted
steps:
- name: 检出代码
uses: https://gitea.com/actions/checkout@v4
- name: 显示目标主机信息
run: |
echo "部署主机: $(hostname)"
echo "执行用户: $(id -un) ($(id -u))"
echo "触发引用: ${{ gitea.ref }}"
echo "部署目录: $DEPLOY_DIR"
- name: 停止旧实例(若存在)
run: |
if [ -x "$DEPLOY_DIR/stop.sh" ]; then
sudo -n "$DEPLOY_DIR/stop.sh" || true
else
echo "$DEPLOY_DIR 下未发现旧实例,跳过停止步骤"
fi
- name: 同步代码到部署目录
run: |
sudo -n /usr/bin/rsync -a --delete \
--exclude='.git' \
--exclude='.venv' \
--exclude='.venv-ci' \
--exclude='logs' \
--exclude='.pid' \
./ "$DEPLOY_DIR/"
sudo -n chmod +x "$DEPLOY_DIR/setup.sh" "$DEPLOY_DIR/run.sh" "$DEPLOY_DIR/stop.sh"
- name: 安装 venv 与依赖
run: sudo -n "$DEPLOY_DIR/setup.sh"
- name: 启动服务
run: sudo -n "$DEPLOY_DIR/run.sh"
- name: 校验进程存活
run: |
sleep 5
PID_FILE="$DEPLOY_DIR/.pid"
if [ ! -f "$PID_FILE" ]; then
echo "未找到 pid 文件: $PID_FILE" >&2
sudo -n tail -n 50 "$DEPLOY_DIR/logs/app.log" >&2 || true
exit 1
fi
PID=$(cat "$PID_FILE")
echo "pid=$PID"
for i in 1 2 3; do
if [ -d "/proc/$PID" ]; then
echo "服务运行中pid=$PID第 $i 次检查通过)"
exit 0
fi
echo "第 $i 次检查pid $PID 尚未存活,重试中..."
sleep 2
done
echo "重试 3 次后 pid $PID 仍未存活" >&2
sudo -n tail -n 50 "$DEPLOY_DIR/logs/app.log" >&2 || true
exit 1
- name: HTTP 健康检查
run: |
for i in 1 2 3 4 5 6 7 8 9 10; do
if curl -fsS -o /dev/null "$HEALTH_URL"; then
echo "第 $i 次健康检查通过"
exit 0
fi
echo "第 $i 次健康检查失败,重试中..."
sleep 2
done
echo "健康检查失败:$HEALTH_URL 不可达" >&2
sudo -n tail -n 80 "$DEPLOY_DIR/logs/app.log" >&2 || true
exit 1
- name: 输出启动日志
if: always()
run: sudo -n tail -n 30 "$DEPLOY_DIR/logs/app.log" || true