自己实践了github的webhooks, linux上面的权限需要注意

环境, 阿里云服务器

1. 本地创建项目, push到github服务器上面

2. 生成www用户的密钥

sudo -u www ssh-keygen -t rsa -C "[email protected]"

3. 将密钥添加到github帐号的SSH_KEYS里面

3. 用www用户执行克隆, 源使用ssh的克隆方式, 不要用https

sudo -u www git clone [email protected]:xxx/xxx.git

4. 创建更新的php脚本,放到网站的根目录, 外网可以正常访问到这个脚本即可

cat update_hook.php

<?php
$www_folder = "/alidata/www/default/movie";

//在这里获取到了用户提交的内容, 以及提交者等等, 可以记录到数据库中供以后使用
$raw_json = file_get_contents('php://input');
print_r(json_decode($raw_json, true));

echo shell_exec(" cd $www_folder ; git pull 2>&1");

5. 将脚本地址填写到项目的webhooks

http://x.x.x.x/update_hook.php

提示: 

注意不要在服务器上用root用户git pull, 这样一些文件的权限会变为root, 就混乱了, 下次 www用户push后就无法通过webhook部署了

sudo -u www git push

服务器上面配置全局的用户和邮箱

git config --global user.name “xxx”
git config --global user.email “[email protected]"

将项目根目录权限设置为www用户

chown -R www:www /alidata/www/default/movie

猜你喜欢

转载自duchengjiu.iteye.com/blog/2173819