Gogs同步钩子和web钩子(php)
1.同个服务器下的钩子
a.设置gogs上的配置
shell :
#!/bin/sh export GIT_WORK_TREE=网站根路径 export GIT_DIR=${GIT_WORK_TREE}/.git cd ${GIT_WORK_TREE} git pull
b.设置网站下的git的分支拉取路径
vi 网站根目录路径/.git/config
设置 git请求链接(带账号密码) http://用户名:密码@域名/hhb/swoole.git
2.web钩子
a.gogs上的配置
设置访问的服务器的钩子地址
密码文本的格式(可直接黏贴进文本中)
{ "ref": "refs/heads/develop", "before": "28e1879d029cb852e4844d9c718537df08844e03", "after": "bffeb74224043ba2feb48d137756c8a9331c449a", "compare_url": "http://localhost:3000/unknwon/webhooks/compare/28e1879d029cb852e4844d9c718537df08844e03...bffeb74224043ba2feb48d137756c8a9331c449a", "commits": [ { "id": "bffeb74224043ba2feb48d137756c8a9331c449a", "message": "!@#0^%u003eu003eu003eu003eu003cu003cu003cu003cu003eu003eu003eu003e ", "url": "http://localhost:3000/unknwon/webhooks/commit/bffeb74224043ba2feb48d137756c8a9331c449a", "author": { "name": "Unknwon", "email": "u@gogs.io", "username": "unknwon" }, "committer": { "name": "Unknwon", "email": "u@gogs.io", "username": "unknwon" }, "timestamp": "2017-03-13T13:52:11-04:00" } ], "repository": { "id": 140, "owner": { "id": 1, "login": "unknwon", "full_name": "Unknwon", "email": "u@gogs.io", "avatar_url": "https://secure.gravatar.com/avatar/d8b2871cdac01b57bbda23716cc03b96", "username": "unknwon" }, "name": "webhooks", "full_name": "unknwon/webhooks", "description": "", "private": false, "fork": false, "html_url": "http://localhost:3000/unknwon/webhooks", "ssh_url": "ssh://unknwon@localhost:2222/unknwon/webhooks.git", "clone_url": "http://localhost:3000/unknwon/webhooks.git", "website": "", "stars_count": 0, "forks_count": 1, "watchers_count": 1, "open_issues_count": 7, "default_branch": "master", "created_at": "2017-02-26T04:29:06-05:00", "updated_at": "2017-03-13T13:51:58-04:00" }, "pusher": { "id": 1, "login": "unknwon", "full_name": "Unknwon", "email": "u@gogs.io", "avatar_url": "https://secure.gravatar.com/avatar/d8b2871cdac01b57bbda23716cc03b96", "username": "unknwon" }, "sender": { "id": 1, "login": "unknwon", "full_name": "Unknwon", "email": "u@gogs.io", "avatar_url": "https://secure.gravatar.com/avatar/d8b2871cdac01b57bbda23716cc03b96", "username": "unknwon" } }
b.服务器设置(需要www用户对操作到的文件有读写的权限)
(1)在访问的路径的git-webhook-handler.php 文件 ,要能访问得到
具体代码:
$requestBody = file_get_contents("php://input"); //收取来自git发送过来的请求
$txt = /www/wwwroot/gogs/webhook_log/ . date(Ymd) . .txt;
if (empty($requestBody)) {
die(send fail);
}
$content = json_decode($requestBody, true);
switch ($content[repository][name]) {//对应密钥文本的参数
case swoole:
switch ($content[ref]) {
case refs/heads/master:
//file_put_contents(test.txt ,go here ,FILE_APPEND);
$res = shell_exec("/bin/sh /etc/sh/swoole/api_web");//以www用户运行 --需要在php.ini开启shell_exec 的函数
$res_log = ------------------------- . PHP_EOL;
$res_log .= $content[user_name] . 在 . date(Y-m-d H:i:s) . 向 . $content[repository][name] . 项目的 . $content[ref] . 分支 . PHP_EOL;
file_put_contents($txt, $res_log, FILE_APPEND);//追加写入
break;
default:
break;
}
break;
default:
break;
}
(2)设置shell脚本 api_web(我是将其路径放置在/etc/sh/swoole/api_web ,对应的网站的网站的.git/config 也要配置上带账号和密码的的git请求路径)
内容 :
#!/bin/bash
DIR=/www/wwwroot/swoole #网站路径
cd ${DIR} && git pull && git remote prune origin
