php钩子自动注册,通过git@osc的push钩子实现自动部署

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

deploy.php<?php

header("Content-type: text/html; charset=utf-8");

if (! isset($_REQUEST['hook'])) die ('非法请求');

$config = require('config.php');

//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('request.json'), true);

$project = $hook['push_data']['repository']['name'];

//判断密码

if ($hook['password'] != $config['projects'][$project]['password']) die ("密码错误");

//判断branch

if (trim(strrchr($hook['push_data']['ref'], '/'), '/') != $config['projects'][$project]['branch']) die ("非自动部署分支");

$shell = <<

WEB_PATH='{$config['projects'][$hook['push_data']['repository']['name']]['web_path']}'

WEB_USER='{$config['web_user']}'

WEB_GROUP='{$config['web_group']}'

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('deploy.sh', $shell);

$res = shell_exec("bash deploy.sh");

$log_file = "{$project}.log";

foreach ($hook['push_data']['commits'] as $commit) {

file_put_contents($log_file,

"※" . date('Y-m-d H:i:s') . "\t" .

$hook['push_data']['repository']['name'] . "\t" .

$commit['message'] . "\t" .

$commit['author']['name'] . PHP_EOL,

FILE_APPEND

);

}

file_put_contents($log_file, $res . PHP_EOL, FILE_APPEND);

config.php<?php

return [

'web_user' => 'www',

'web_group' => 'www',

'projects' => [

'project' => [

'password' => 'password',

'web_path' => '/home/wwwroot/default/project',

],

]

];

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值