为了方便开发测试或项目部署至服务器不那么繁琐,搞一个自动部署的小轮子也是必要的。
小轮子需要涉及到 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.py 中 file_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 &
1.url 填你的域名 http://xxx.xxx.com/gohook
2.token 填 gohook
1.本地于服务器自动部署的git项目中使用 git 提交更新一下代码
touch test.md
git add .
git commit -m "test gohook"
git push -u origin master 
2.查看服务器上自动部署的git项目中是否存在 test.md
done.
|  |      1pypy      2016-04-06 10:48:44 +08:00 部署时能自动停止服务、部署之后能够自动启动服务吗? | 
|      2HavenShen OP | 
|  |      3kinghui      2016-04-06 10:57:50 +08:00 https://github.com/HavenShen/gohook/blob/master/main.py#L36-L45 确定语法正确? 另外一些持续集成工具比如 buildbot 也可以做类似的事情. | 
|  |      4kinghui      2016-04-06 11:01:41 +08:00 明白了, 你是混合使用空格和 Tab 缩进.... | 
|  |      6xmbaozi      2016-04-06 11:12:24 +08:00 我们用 gitlab ,直接用 BaseHTTPServer 撸了一个 http://www.baozy.com/archives/12437.html | 
|  |      7kinghui      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) | 
|      8HavenShen OP | 
|      10wittyfox      2016-04-06 13:36:26 +08:00 via Android 我用 Ruby 的 Sinatra 写了一个,用于自己使用。 你需要验证请求头中的相关信息,比如 committer 等等。 | 
|  |      11janxin      2016-04-06 14:20:16 +08:00 安全性可以依赖 token 来保证,当然建议使用 https 链接。 其实我们也做了一个,思路和实现大家都差不多的。 |