码云Webhooks配合服务器自动同步代码
一.码云创建ssh公钥
1)生成ssh公钥,跟着步骤执行这些命令:
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"
回车三次即可生成sshkey
2)查看 public key,并把他添加到码云(Gitee.com)
cat ~/.ssh/id_rsa.pub
3)添加用户sshkey 到码云SSH公匙
4)在shell中继续执行
ssh -T git@git.oschina.net
返回Welcome to Git@OSC, yourname!表示添加成功。
二、变更为ssh协议认证
1)将https协议认证变成ssh协议认证
git remote rm origin
将远程的提交方式删除,然后添加新的提交方式
git remote add origin git@gitee.com:xxx/xxx.git
2)拉取代码
git pull origin master
3)文件冲突情况
git stash
git pull origin master
git stash pop
一个PHP swoole 服务器同步代码案例:
<?php
use Swoole\Http\Server as SwooleHttpServer;
class HttpServer
{
protected $log_path = __DIR__ . '/server_log.txt';
protected $server;
public function __construct($host, $port)
{
$this->server = new SwooleHttpServer($host, $port);
$this->server->set([
'daemonize' => 1,
'log_file' => $this->log_path,
]);
$this->init();
// $this->start();
}
public function init()
{
$this->server->on('request', [$this, 'onRequest']);
}
public function onRequest($request, $response)
{
$data = json_decode($request->rawContent(), true);
if ($data['password'] == 'your_webhooks_set_password') {
chdir('/home/wwwroot/xxx_pro');
exec('git pull origin master',$out,$status);
if($status){
$this->log('[pull fail:::]'.var_export($out,true));
$response->end('fail!');
}else{
$this->log('[pull success:::]'.var_export($out,true));
$response->end('ok!');
}
}else{
$response->end('404');
}
}
public function start()
{
$this->server->start();
}
private function log($info)
{
$log = sprintf("[%s] request=%s \n\n", date('Y-m-d H:i:s'), $info);
file_put_contents($this->log_path, $log, FILE_APPEND);
}
}
$server = new HttpServer('0.0.0.0', 9508);
$server->start();
.gitignore空文件夹文件 ``` # Ignore everything in this directory * # Except this file !.gitignore ```