Home php教程 PHP源码 通过git@osc的push钩子实现自动部署

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

May 23, 2016 pm 04:40 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)