webhook.py 859 B

12345678910111213141516171819202122232425262728
  1. from flask import Flask, request
  2. import subprocess
  3. app = Flask(__name__)
  4. # Webhook 路由
  5. @app.route('/webhook', methods=['POST'])
  6. def webhook():
  7. # 获取推送的分支信息
  8. data = request.json
  9. if not data:
  10. return "No data received", 400
  11. # 确认推送到的分支
  12. branch = data.get('ref', '')
  13. if branch.endswith('main'): # 检查是否是 main 分支
  14. try:
  15. # 执行部署脚本
  16. deploy_script = "/Users/zejiawu/go-msa-auth/deploy.sh"
  17. result = subprocess.run([deploy_script], shell=True, capture_output=True, text=True)
  18. return f"Deployment triggered:\n{result.stdout}", 200
  19. except Exception as e:
  20. return f"Error during deployment: {str(e)}", 500
  21. return "No action taken", 200
  22. if __name__ == '__main__':
  23. app.run(host='0.0.0.0', port=5001)