码云 githook, php执行git pll

最近公司要求所有代码迁到码云私有库,并且通过Webhook实现自动拉取部署

1、root权限修改生产服务器只读文件 /etc/sudoers

www     ALL=NOPASSWD:/usr/bin/git

此步骤意义在于,nginx 用户组为 www,执行某个php文件时,实际上是www用户组执行shell_exec()。但是 git属root用户组,通过php执行,没有权限。上面配置就是给www用户执行git 使用sudo免密。

2、php 文件如下:

/**
 *  Hook
 *  
 *  hook 放置于 index.php 同级目录
 *  
 *  @author [email protected]
 *  
 *  2019-3-8
 *  
 */

//获取上级目录,git 仓库所在目录
$savePath = dirname(__DIR__);

//拉去代码
$res = shell_exec("cd {$savePath} && sudo git pull ");

print_r($res);

3、马云设置,并且测试

码云 githook, php执行git pll

完成