gogs 代码同步软件

hopaheal 167b38b773 更新 'db/data.sql' há 5 anos atrás
conf 110fb7b114 first commit há 5 anos atrás
db 167b38b773 更新 'db/data.sql' há 5 anos atrás
doc 110fb7b114 first commit há 5 anos atrás
install 110fb7b114 first commit há 5 anos atrás
.gitignore 110fb7b114 first commit há 5 anos atrás
README.md e39212b25f 更新 'README.md' há 5 anos atrás
config.py 4e52f82bc3 readme há 5 anos atrás
config.py.product 110fb7b114 first commit há 5 anos atrás
core.py 110fb7b114 first commit há 5 anos atrás
requirements.txt 110fb7b114 first commit há 5 anos atrás
webhooks.py 110fb7b114 first commit há 5 anos atrás

README.md

1.安装

yum install epel-release wget -y
yum update 
yum install python-pip
yum install supervisor
pip install flask gunicorn pymysql sqlalchemy
  1. 拷贝 webhooks 到 /opt 目录下

    # 创建日志目录
    mkdir /opt/webhooks/log/
    
  2. 创建配置文件

    touch /etc/supervisord.d/webhook.ini
    

添加以下内容

   [program:webhooks]
   command=gunicorn -w1 -b0.0.0.0:21332 --error-logfile /opt/webhooks/log/error.log --access-logfile  /opt/webhooks/log/access.log webhooks:app ;应用入口
   user=root
   directory=/opt/webhooks                         ;web目录
   startsecs=5                                      ;启动时间
   stopwaitsecs=0                                  ;终止等待时间
   startretries = 3
   autostart=true                                   ;是否自动启动
   autorestart=true                                 ;是否自动重启
   redirect_stderr=true                             ;错误日志输出到标准日志
   stdout_logfile=/dev/null                      ;标准日志不输出
   stdout_logfile_maxbytes=10MB                        ;标准日志大小
   environment=PYTHONPATH=$PYTHONPATH:/opt/webhooks
  1. 导入数据库并修改密码

数据库在 db 目录下的data.sql里面,需要导入使用。数据库配置在 config.py 里面

   SQLALCHEMY_DATABASE_SQLITE_URI = "mysql+pymysql://root:password@localhost:3306/webhook?charset=utf8"
   
  1. 测试是否可用

    cd /opt/webhooks
    python webhooks.py
    
  2. 使 supervisor 自启动

    systemctl enable supervisord  # supervisord 自启动
    systemctl start supervisord   # supervisor 启动
       
    

supervisor 常用命令

   supervisorctl status   # 查看 supervisor 状态
   supervisorctl reload   # 重新加载 supervisor 配置文件,这个是在修改配置文件以后需要使用这个命令才能生效
   supervisorctl restart webhook # 重启 webhook 服务

测试

   curl -s http://localhost:21332/hookssync/38daa500-55a8-11e8-acd6-704d7b885ead

注意事项

1.CentOS 防火墙记得放行 21332 端口或者直接关闭防火墙systemctl stop firewalld;systemctl disable firewalld

  1. 如果在除了

    PATH_WHITE_LIST = [
       # 匹配所有
       '.*',
       # /app/web/test
       '^/app/web/.*',
       # /app/web/tests
       '^/var/www/html/.*',
       '/www/wwwroot/.*'
    ]
    

目录同步代码需要修改 config.py 文件

如果在同步过程中需要执行其他命令需要添加规则

#允许在webhooks执行时执行的命令 使用正则表达式进行匹配
EXEC_WHITE_LIST = [
     # 匹配所有
     '.*',
     # git pull origin master
    '^git pull \w+ \w+$',
     # git fetch origin master && git reset --hard origin/master
    '^git fetch \w+ \w+ && git reset --hard \w+/\w+$',
    # git pull origin master && supervisorctl restart webhooks
    '^git pull \w+ \w+ && supervisorctl restart \w+$',
    # git fetch origin master && git reset --hard origin/master && supervisorctl restart webhooks
    '^git fetch \w+ \w+ && git reset --hard \w+/\w+ && supervisorctl restart \w+$'
]