1.创建脚本,写入以下内容。
脚本放入www根目录下。
<?php
$json = file_get_contents("php://input");
$data = json_decode($json,true);
if (isset($data['ref']) && $data['total_commits_count']>0) {
$res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL;
$res .= shell_exec("cd /opt/www/ci && git pull https://457026453%40qq.com:renhaojie2589@gitee.com/ren_hj/ci.git 2<&1 ");
$res_log = '------------------------------------------------------------'.PHP_EOL;
$res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '项目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '个commit:'.$data['commits'][0]['message'];
$res_log .= $res.PHP_EOL;
$res_log .= "pull end -----------------------------------------------------".PHP_EOL;
file_put_contents("/opt/logs/webhook/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);
}
?>
2.修改项目目录为所有者为root
或者修改目录权限为777,否则从git上拉取代码时会提示文件覆盖失败。
[root@VM_0_12_centos www]
/opt/www
[root@VM_0_12_centos www]
ci hook.php php-delayqueue rhjweb test
[root@VM_0_12_centos www]
3.更改git所有者。
因为是远程脚本访问,所以要更改为访问时的所有者,可以在脚本中加入“whoami”指令查看访问时的所有者,我这里更改为apache。
[root@VM_0_12_centos ci]
/opt/www/ci
[root@VM_0_12_centos ci]
4.码云中配置
填写自己脚本文件的路径即可。每次push代码就会触发向改url发起请求,执行hook.php脚本文件,拉取项目代码。代码同步完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)