feat: 添加 Gitea Actions 自动部署功能
Some checks failed
Deploy BillAI / Deploy to Production (push) Failing after 0s

This commit is contained in:
CHE LIANG ZHAO
2026-01-26 17:21:12 +08:00
parent a1eebd0b3f
commit ed0a44851d
5 changed files with 218 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
# BillAI 自动部署工作流
# 当 master 分支有 push 时自动触发部署
name: Deploy BillAI
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy to Production
runs-on: self-hosted
steps:
- name: Checkout code
run: |
echo "=== 拉取最新代码 ==="
cd ${{ vars.DEPLOY_PATH }}
git fetch origin master
git reset --hard origin/master
echo "当前版本: $(git log -1 --format='%h %s')"
- name: Build and deploy
run: |
echo "=== 构建并部署服务 ==="
cd ${{ vars.DEPLOY_PATH }}
docker compose up -d --build --remove-orphans
- name: Cleanup
run: |
echo "=== 清理旧镜像 ==="
docker image prune -f
- name: Health check
run: |
echo "=== 健康检查 ==="
echo "等待服务启动..."
sleep 15
# 通过 Docker 健康检查状态判断(不依赖端口暴露)
check_container() {
local name=$1
local container=$2
local status=$(docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null)
if [ "$status" = "healthy" ]; then
echo "✓ $name 服务正常"
return 0
else
echo "✗ $name 服务异常 (状态: $status)"
return 1
fi
}
FAILED=0
check_container "Web" "billai-web" || FAILED=1
check_container "Server" "billai-server" || FAILED=1
check_container "Analyzer" "billai-analyzer" || FAILED=1
check_container "MongoDB" "billai-mongodb" || FAILED=1
if [ $FAILED -eq 0 ]; then
echo "=== 部署成功 ==="
else
echo "=== 部署失败:部分服务异常 ==="
docker compose ps
exit 1
fi

62
deploy.sh Normal file
View File

@@ -0,0 +1,62 @@
#!/bin/bash
# BillAI 部署脚本
# 用于手动部署或 Gitea Actions 自动部署
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== BillAI 部署开始 ===${NC}"
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
echo -e "\n${YELLOW}[1/4] 拉取最新代码${NC}"
git fetch origin master
git reset --hard origin/master
echo "当前版本: $(git log -1 --format='%h %s')"
echo -e "\n${YELLOW}[2/4] 构建并部署服务${NC}"
docker compose up -d --build --remove-orphans
echo -e "\n${YELLOW}[3/4] 清理旧镜像${NC}"
docker image prune -f
echo -e "\n${YELLOW}[4/4] 健康检查${NC}"
echo "等待服务启动..."
sleep 15
# 检查服务状态(通过 Docker 健康检查状态)
check_service() {
local name=$1
local container=$2
local status=$(docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null)
if [ "$status" = "healthy" ]; then
echo -e " ${GREEN}${NC} $name 服务正常"
return 0
else
echo -e " ${RED}${NC} $name 服务异常 (状态: $status)"
return 1
fi
}
FAILED=0
check_service "Web" "billai-web" || FAILED=1
check_service "Server" "billai-server" || FAILED=1
check_service "Analyzer" "billai-analyzer" || FAILED=1
check_service "MongoDB" "billai-mongodb" || FAILED=1
if [ $FAILED -eq 0 ]; then
echo -e "\n${GREEN}=== 部署成功 ===${NC}"
exit 0
else
echo -e "\n${RED}=== 部署失败:部分服务异常 ===${NC}"
docker compose ps
exit 1
fi

View File

@@ -0,0 +1,38 @@
# Gitea Actions Runner - 自动部署
#
# 使用方法:
# 1. 在 Gitea 仓库获取 Runner Token
# 访问https://git.fadinglight.cn/clz/billai/settings/actions/runners
# 点击 "Create new Runner" 复制 Token
#
# 2. 创建 .env 文件或设置环境变量
# echo "GITEA_RUNNER_REGISTRATION_TOKEN=你的Token" > runner/.env
#
# 3. 启动 Runner
# docker compose -f docker-compose.runner.yaml up -d
#
# 4. 在 Gitea 仓库添加变量
# 访问https://git.fadinglight.cn/clz/billai/settings/actions/variables
# 添加 DEPLOY_PATH = /你的项目路径
services:
runner:
image: gitea/act_runner:latest
container_name: billai-runner
restart: unless-stopped
env_file:
- ./runner/.env
environment:
GITEA_INSTANCE_URL: "https://git.fadinglight.cn"
GITEA_RUNNER_NAME: "billai-runner"
GITEA_RUNNER_LABELS: "self-hosted:host,ubuntu-latest:host"
CONFIG_FILE: /config.yaml
volumes:
# Runner 配置文件
- ./runner/config.yaml:/config.yaml
# Runner 数据持久化
- ./runner/data:/data
# Docker socket - 用于在宿主机执行 docker 命令
- /var/run/docker.sock:/var/run/docker.sock
# 项目目录 - 用于执行部署脚本
- .:/workspace/billai

3
runner/.env.example Normal file
View File

@@ -0,0 +1,3 @@
# Gitea Runner 配置
# 从 Gitea 仓库获取 TokenSettings -> Actions -> Runners -> Create new Runner
GITEA_RUNNER_REGISTRATION_TOKEN=你的Token

48
runner/config.yaml Normal file
View File

@@ -0,0 +1,48 @@
# Gitea Actions Runner 配置
# 文档: https://docs.gitea.com/usage/actions/act-runner
log:
# 日志级别: debug, info, warn, error
level: info
runner:
# Runner 注册信息存储文件
file: .runner
# 同时运行的任务数量
capacity: 1
# 环境变量传递给 job
envs: {}
# 任务超时时间
timeout: 1h
# 关机超时时间
shutdown_timeout: 3h
# 是否获取远程任务时不进行 TLS 验证(不推荐)
insecure: false
# 任务容器拉取策略: always, if-not-present, never
fetch_timeout: 5s
fetch_interval: 2s
# Runner 标签
labels:
- "ubuntu-latest:host"
- "self-hosted:host"
container:
# 容器网络模式
network: "host"
# 是否启用特权模式
privileged: false
# 容器选项
options:
# 工作目录父路径
workdir_parent:
# 有效的卷挂载
valid_volumes:
- /workspace/**
# Docker 主机
docker_host: ""
# 强制拉取镜像
force_pull: false
host:
# 主机工作目录
workdir_parent: