From 345533ed6e45526fae678b2f7a395b9f990423e2 Mon Sep 17 00:00:00 2001 From: Song367 <601337784@qq.com> Date: Tue, 29 Jul 2025 18:23:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0yaml=20=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/deploy.yaml | 44 ++++++++++++++ Dockerfile | 113 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 26 ++++++++ 3 files changed, 183 insertions(+) create mode 100644 .gitea/workflows/deploy.yaml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml new file mode 100644 index 0000000..c72996b --- /dev/null +++ b/.gitea/workflows/deploy.yaml @@ -0,0 +1,44 @@ +name: Gitea Actions Demo +run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 +on: + push: + branches: + - 'dev' + +env: + BUILD: staging + + +jobs: + Explore-Gitea-Actions: + runs-on: stream9 + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event." + - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" + - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." + - name: Check out repository code + uses: https://gitea.yantootech.com/neil/checkout@v4 + - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner." + - run: echo "🖥️ The workflow is now ready to test your code on the runner." + - name: List files in the repository + run: | + whoami + uname -a + pwd + ls ${{ gitea.workspace }} + - name: Build and push + uses: https://gitea.yantootech.com/neil/build-push-action@v6 + with: + push: false + tags: emotion-digital-video:${{ gitea.run_id }} + - name: Run docker + run: | + pwd + if [ "$(docker ps -q -f name=^emotion-digital-video$)" ]; then + docker stop emotion-digital-video + fi + docker run -d --rm --name emotion-digital-video \ + -v /usr/share/fonts/opentype/noto:/usr/share/fonts \ + -p 6900:3000 \ + emotion-digital-video:${{ gitea.run_id }} + - run: echo "🍏 This job's status is ${{ job.status }}." \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ce2a872 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,113 @@ +# 使用官方Node.js运行时作为基础镜像 +FROM node:18-alpine + +# 安装系统依赖和监控工具 +RUN apk add --no-cache \ + bash \ + procps \ + curl \ + && npm install -g yarn nodemon + +# 设置工作目录 +WORKDIR /app + +# 复制package.json和yarn.lock(如果存在) +COPY package.json yarn.lock* ./ + +# 安装项目依赖 +RUN yarn install --frozen-lockfile + +# 复制项目文件 +COPY . . + +# 创建videos目录(如果不存在) +RUN mkdir -p videos + +# 创建CPU监控和重启脚本 +RUN cat > /app/monitor.sh << 'EOF' +#!/bin/bash + +# CPU阈值设置(百分比) +CPU_THRESHOLD=80 +# 检查间隔(秒) +CHECK_INTERVAL=30 +# 日志文件 +LOG_FILE="/app/monitor.log" + +log_message() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" +} + +get_cpu_usage() { + # 获取Node.js进程的CPU使用率 + local pid=$(pgrep -f "node.*server.js" | head -1) + if [ -n "$pid" ]; then + ps -p $pid -o %cpu --no-headers | awk '{print int($1)}' + else + echo "0" + fi +} + +restart_app() { + log_message "CPU使用率过高,重启应用..." + pkill -f "node.*server.js" + sleep 5 + cd /app + yarn dev & + log_message "应用已重启" +} + +log_message "开始CPU监控,阈值: ${CPU_THRESHOLD}%" + +while true; do + cpu_usage=$(get_cpu_usage) + log_message "当前CPU使用率: ${cpu_usage}%" + + if [ "$cpu_usage" -gt "$CPU_THRESHOLD" ]; then + log_message "警告: CPU使用率 ${cpu_usage}% 超过阈值 ${CPU_THRESHOLD}%" + restart_app + fi + + sleep $CHECK_INTERVAL +done +EOF + +# 给监控脚本执行权限 +RUN chmod +x /app/monitor.sh + +# 创建启动脚本 +RUN cat > /app/start.sh << 'EOF' +#!/bin/bash + +# 启动应用 +echo "启动WebRTC应用..." +cd /app +yarn dev & + +# 等待应用启动 +sleep 10 + +# 启动CPU监控 +echo "启动CPU监控..." +/app/monitor.sh & + +# 保持容器运行 +wait +EOF + +# 给启动脚本执行权限 +RUN chmod +x /app/start.sh + +# 暴露端口(默认3000,支持环境变量配置) +EXPOSE 3000 + +# 设置环境变量 +ENV NODE_ENV=production +ENV PORT=3000 + +# 健康检查 +HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ + CMD curl -f http://localhost:$PORT || exit 1 + +# 启动应用和监控 +CMD ["/app/start.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d89aff3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.8' + +services: + webrtc-app: + build: . + ports: + - "3000:3000" # 映射到主机的3000端口,外网可访问 + environment: + - NODE_ENV=production + - PORT=3000 + volumes: + - ./videos:/app/videos # 挂载视频文件目录 + - ./logs:/app/logs # 挂载日志目录 + restart: unless-stopped + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:3000"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 60s + networks: + - webrtc-network + +networks: + webrtc-network: + driver: bridge \ No newline at end of file