推荐学习书目
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 · Apr 6, 2016 · 5845 views
    This topic created in 3714 days ago, the information mentioned may be changed or developed.

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

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

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

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

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

    3.mark !
    pypy
        9
    pypy  
       Apr 6, 2016
    @HavenShen 明白了。谢谢。
    wittyfox
        10
    wittyfox  
       Apr 6, 2016 via Android
    我用 Ruby 的 Sinatra 写了一个,用于自己使用。

    你需要验证请求头中的相关信息,比如 committer 等等。
    janxin
        11
    janxin  
       Apr 6, 2016
    安全性可以依赖 token 来保证,当然建议使用 https 链接。

    其实我们也做了一个,思路和实现大家都差不多的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   839 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    ♥ Do have faith in what you're doing.