wuzj há 2 meses atrás
pai
commit
f44c6dbfde
2 ficheiros alterados com 54 adições e 0 exclusões
  1. 26 0
      deploy.sh
  2. 28 0
      webhook/webhook.py

+ 26 - 0
deploy.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# 项目目录
+PROJECT_DIR="/home/user/go-msa-auth"
+
+# 分支名称(需要监听的分支,例如 main)
+BRANCH="main"
+
+# 切换到项目目录
+cd $PROJECT_DIR || { echo "Directory $PROJECT_DIR not found!"; exit 1; }
+
+# 拉取最新代码
+echo "Pulling latest changes from branch $BRANCH..."
+git fetch origin $BRANCH
+git reset --hard origin/$BRANCH
+
+# 构建 Docker 镜像
+echo "Building Docker image..."
+docker-compose build
+
+# 停止旧容器并启动新容器
+echo "Restarting service..."
+docker-compose down
+docker-compose up -d
+
+echo "Deployment complete!"

+ 28 - 0
webhook/webhook.py

@@ -0,0 +1,28 @@
+from flask import Flask, request
+import subprocess
+
+app = Flask(__name__)
+
+# Webhook 路由
+@app.route('/webhook', methods=['POST'])
+def webhook():
+    # 获取推送的分支信息
+    data = request.json
+    if not data:
+        return "No data received", 400
+
+    # 确认推送到的分支
+    branch = data.get('ref', '')
+    if branch.endswith('main'):  # 检查是否是 main 分支
+        try:
+            # 执行部署脚本
+            deploy_script = "/home/user/go-msa-auth/deploy.sh"
+            result = subprocess.run([deploy_script], shell=True, capture_output=True, text=True)
+            return f"Deployment triggered:\n{result.stdout}", 200
+        except Exception as e:
+            return f"Error during deployment: {str(e)}", 500
+
+    return "No action taken", 200
+
+if __name__ == '__main__':
+    app.run(host='0.0.0.0', port=5000)