Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-23 16:40:55
Original
1813 people have browsed it

为了解决自动部署问题,查询了一些解决方案后,为了稳定最终决定使用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);
Copy after login

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;,
        ],
    ]
];
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template