首页 > php教程 > PHP源码 > 正文

通过git@osc的push钩子实现自动部署

PHP中文网
发布: 2016-05-23 16:40:55
原创
1813 人浏览过

为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用Git@OSC来实现该需求。

deploy.php

<?php
header("Content-type: text/html; charset=utf-8");
  
if (! isset($_REQUEST[&#39;hook&#39;])) die (&#39;非法请求&#39;);
  
$config = require(&#39;config.php&#39;);
//hook内容详见http://git.oschina.net/oschina/git-osc/wikis/HOOK%E9%92%A9%E5%AD%90
$hook = json_decode($_REQUEST["hook"], true);
//$hook = json_decode(file_get_contents(&#39;request.json&#39;), true);
$project = $hook[&#39;push_data&#39;][&#39;repository&#39;][&#39;name&#39;];
  
//判断密码
if ($hook[&#39;password&#39;] != $config[&#39;projects&#39;][$project][&#39;password&#39;]) die ("密码错误");
//判断branch
if (trim(strrchr($hook[&#39;push_data&#39;][&#39;ref&#39;], &#39;/&#39;), &#39;/&#39;) != $config[&#39;projects&#39;][$project][&#39;branch&#39;]) die ("非自动部署分支");
  
$shell = <<<EOF
WEB_PATH=&#39;{$config[&#39;projects&#39;][$hook[&#39;push_data&#39;][&#39;repository&#39;][&#39;name&#39;]][&#39;web_path&#39;]}&#39;
WEB_USER=&#39;{$config[&#39;web_user&#39;]}&#39;
WEB_GROUP=&#39;{$config[&#39;web_group&#39;]}&#39;
  
echo "Start deployment"
cd \$WEB_PATH
echo "pulling source code..."
git reset --hard origin/master
git clean -f
git pull
git checkout master
echo "changing permissions..."
chown -R \$WEB_USER:\$WEB_GROUP \$WEB_PATH
echo "Finished."
EOF;
  
file_put_contents(&#39;deploy.sh&#39;, $shell);
$res = shell_exec("bash deploy.sh");
  
$log_file = "{$project}.log";
foreach ($hook[&#39;push_data&#39;][&#39;commits&#39;] as $commit) {
    file_put_contents($log_file, 
        "※" . date(&#39;Y-m-d H:i:s&#39;) . "\t" . 
        $hook[&#39;push_data&#39;][&#39;repository&#39;][&#39;name&#39;] . "\t" . 
        $commit[&#39;message&#39;] . "\t" . 
        $commit[&#39;author&#39;][&#39;name&#39;] . PHP_EOL, 
        FILE_APPEND
    );
}
file_put_contents($log_file, $res . PHP_EOL, FILE_APPEND);
登录后复制

config.php

<?php
return [
    &#39;web_user&#39; => &#39;www&#39;,
    &#39;web_group&#39; => &#39;www&#39;,
    &#39;projects&#39; => [
        &#39;project&#39; => [
            &#39;password&#39; => &#39;password&#39;,
            &#39;web_path&#39; => &#39;/home/wwwroot/default/project&#39;,
        ],
    ]
];
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板