免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3013 | 回复: 0
打印 上一主题 下一主题

通过git@osc的push钩子实现自动部署 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-12 13:35 |只看该作者 |倒序浏览
为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用Git@OSC来实现该需求。
详见:http://blog.skyx.in/archives/158/

deploy.php
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3.   
  4. if (! isset($_REQUEST['hook'])) die ('非法请求');
  5.   
  6. $config = require('config.php');
  7. //hook内容详见http://git.oschina.net/oschina/git-osc/wikis/HOOK%E9%92%A9%E5%AD%90
  8. $hook = json_decode($_REQUEST["hook"], true);
  9. //$hook = json_decode(file_get_contents('request.json'), true);
  10. $project = $hook['push_data']['repository']['name'];
  11.   
  12. //判断密码
  13. if ($hook['password'] != $config['projects'][$project]['password']) die ("密码错误");
  14. //判断branch
  15. if (trim(strrchr($hook['push_data']['ref'], '/'), '/') != $config['projects'][$project]['branch']) die ("非自动部署分支");
  16.   
  17. $shell = <<<EOF
  18. WEB_PATH='{$config['projects'][$hook['push_data']['repository']['name']]['web_path']}'
  19. WEB_USER='{$config['web_user']}'
  20. WEB_GROUP='{$config['web_group']}'
  21.   
  22. echo "Start deployment"
  23. cd \$WEB_PATH
  24. echo "pulling source code..."
  25. git reset --hard origin/master
  26. git clean -f
  27. git pull
  28. git checkout master
  29. echo "changing permissions..."
  30. chown -R \$WEB_USER:\$WEB_GROUP \$WEB_PATH
  31. echo "Finished."
  32. EOF;
  33.   
  34. file_put_contents('deploy.sh', $shell);
  35. $res = shell_exec("bash deploy.sh");
  36.   
  37. $log_file = "{$project}.log";
  38. foreach ($hook['push_data']['commits'] as $commit) {
  39.     file_put_contents($log_file,
  40.         "※" . date('Y-m-d H:i:s') . "\t" .
  41.         $hook['push_data']['repository']['name'] . "\t" .
  42.         $commit['message'] . "\t" .
  43.         $commit['author']['name'] . PHP_EOL,
  44.         FILE_APPEND
  45.     );
  46. }
  47. file_put_contents($log_file, $res . PHP_EOL, FILE_APPEND);
复制代码
config.php
  1. <?php
  2. return [
  3.     'web_user' => 'www',
  4.     'web_group' => 'www',
  5.     'projects' => [
  6.         'project' => [
  7.             'password' => 'password',
  8.             'web_path' => '/home/wwwroot/default/project',
  9.         ],
  10.     ]
  11. ];
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP