V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
HavenShen
V2EX  ›  Python

Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目

  •  
  •   HavenShen ·
    HavenShen · 2016-04-06 10:43:29 +08:00 · 4878 次点击
    这是一个创建于 2942 天前的主题,其中的信息可能已经有所发展或是发生改变。

    为了方便开发测试或项目部署至服务器不那么繁琐,搞一个自动部署的小轮子也是必要的。

    小轮子需要涉及到 Coding 项目托管平台(也可以用 Github 平台),Linux服务器的Nginx、Python( Tornado框架 )。

    同时配置项目托管平台的个人私钥或项目公钥,保证 git pull 能直接拉取。

    GitHub 传送门: https://github.com/HavenShen/gohook

    安装

    1.下载或克隆此项目

    git clone [email protected]:HavenShen/gohook.git
    

    2.部署代码的服务器必须安装 Python 的 Tornado框架

    pip install tornado
    #或
    easy_instal tornado
    

    修改配置

    1.修改 main.pyfile_path 变量路径

    #希望自动部署项目路径
    file_path = '/home/wwwroot/xxx'
    

    2.配置 Nginx 的conf文件

    # http 节点下增加
    upstream frontends{
    		server 127.0.0.1:8765;
    	}
    	
    #增加 server 配置
    server {
        listen 80;
    
        server_name xxx.xxx.com; #你的域名
    
        location / {
                proxy_pass_header Server;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_pass http://frontends;
        }
    }
    

    在此可以重启下 Nginx

    启动

    1.运行tornado框架开启后台进程运行

    #下面路径修改成你自己gohook存放目录文件夹用户组必须跟nginx一致
    setsid python /home/wwwroot/gohook/main.py &
    

    配置 Coding 项目 WebHook

    1.url 填你的域名 http://xxx.xxx.com/gohook

    2.tokengohook

    测试

    1.本地于服务器自动部署的git项目中使用 git 提交更新一下代码

    touch test.md
    git add .
    git commit -m "test gohook"
    git push -u origin master 
    

    2.查看服务器上自动部署的git项目中是否存在 test.md

    done.

    11 条回复    2016-04-06 14:20:16 +08:00
    pypy
        1
    pypy  
       2016-04-06 10:48:44 +08:00
    部署时能自动停止服务、部署之后能够自动启动服务吗?
    HavenShen
        2
    HavenShen  
    OP
       2016-04-06 10:53:25 +08:00
    @pypy 为何要停止?
    这个 setsid python /home/wwwroot/gohook/main.py & 是直接在后台开进程运行的,以保持获取如 Coding 推送代码的时候 WebHook 发起的回调,来完成服务器代码与你本地同步啊。
    kinghui
        3
    kinghui  
       2016-04-06 10:57:50 +08:00
    https://github.com/HavenShen/gohook/blob/master/main.py#L36-L45 确定语法正确? 另外一些持续集成工具比如 buildbot 也可以做类似的事情.
    kinghui
        4
    kinghui  
       2016-04-06 11:01:41 +08:00
    明白了, 你是混合使用空格和 Tab 缩进....
    HavenShen
        5
    HavenShen  
    OP
       2016-04-06 11:05:51 +08:00
    @kinghui 这都被发现了 0.0
    xmbaozi
        6
    xmbaozi  
       2016-04-06 11:12:24 +08:00
    我们用 gitlab ,直接用 BaseHTTPServer 撸了一个
    http://www.baozy.com/archives/12437.html
    kinghui
        7
    kinghui  
       2016-04-06 11:15:25 +08:00
    个人觉得存在以下几个问题 po 主可以看看有没有道理

    1. `pull` 函数阻塞了整个进程, 也就是说每个进程的并发只有 1;
    2. 没有任何安全性可言, 任意一个人都可以 触发 webhook;
    3. 使用 tab 和空格混合使用不符合 [PEP8]( https://www.python.org/dev/peps/pep-0008/#indentation)
    HavenShen
        8
    HavenShen  
    OP
       2016-04-06 11:43:15 +08:00
    @kinghui 嗯,你说的蛮有道理,

    1.subprocess.Popen() 哪里都好,缺陷就是容易阻塞;

    2.安全性,的确,看使用者怎么用了,注重安全可以稍微改良一下,小轮子么。

    3.mark !
    pypy
        9
    pypy  
       2016-04-06 13:10:34 +08:00
    @HavenShen 明白了。谢谢。
    wittyfox
        10
    wittyfox  
       2016-04-06 13:36:26 +08:00 via Android
    我用 Ruby 的 Sinatra 写了一个,用于自己使用。

    你需要验证请求头中的相关信息,比如 committer 等等。
    janxin
        11
    janxin  
       2016-04-06 14:20:16 +08:00
    安全性可以依赖 token 来保证,当然建议使用 https 链接。

    其实我们也做了一个,思路和实现大家都差不多的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2867 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:39 · PVG 19:39 · LAX 04:39 · JFK 07:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.