华体会平台 - 首页 032-353366542

服务器部署:使用WebHook实现PHP自动部署Git代码

作者:华体会 时间:2021-11-23 00:05
本文摘要:类型coding 钩子github 钩子情况服务端:阿里云虚拟主机(Ubuntu16.04)coding 自动部署 WebHook3.0Windows 10 开发情况部署服务器虚拟主机设置coding 代码托管设置当地代码提交服务端设置1、建立web服务器用户目录这里以www用户为例,差别的情况请凭据自己情况自行修改sudo mkdir /var/www/.sshsudo chown -R www:www /var/www/.ssh/2、生成公钥(两个)git用户公钥(小我

华体会

华体会

类型coding 钩子github 钩子情况服务端:阿里云虚拟主机(Ubuntu16.04)coding 自动部署 WebHook3.0Windows 10 开发情况部署服务器虚拟主机设置coding 代码托管设置当地代码提交服务端设置1、建立web服务器用户目录这里以www用户为例,差别的情况请凭据自己情况自行修改sudo mkdir /var/www/.sshsudo chown -R www:www /var/www/.ssh/2、生成公钥(两个)git用户公钥(小我私家公钥通用)部署公钥(部署公钥用以部署项目, 只针对项目)其实设置一个小我私家公钥就可以,也就是通用公钥了3、用户公钥用于git clone时认证权限ssh-keygen -t rsa -C "Tinywan@gmail.com"# 然后一直回车就行# 生成的文件通常是 /root/.ssh/id_rsa,如果非root用户请检察提示上的路径4、部署公钥(非必须)sudo -Hu www ssh-keygen -t rsa # 请选择 "no passphrase",一直回车下去#sudo cat /var/www/.ssh/id_rsa.pub # 这个只是针对单个项目的sudo cat /home/www/.ssh/id_rsa.pub # 检察生成的密钥内容,复制全部-Hu www 下令: -u 代表切换到哪一个用户,这里说的是www -H 代表切换HOME情况变量的值,也就是password文件中www用户对应的home目录5、准备钩子文件在你的站点目录建设一个目录hook,我这里站点目录为:/home/www/web/,所有hook文件路径为:/home/www/web/hook,在hook目录新建index.php文件参考demo<?phperror_reporting(1);// 生产情况web目录$web_path = '/home/www/web/hook/auto-test';$user = 'www';$group = 'www';//作为接口传输的时候认证的密钥$valid_token = '1954FD0D6';//挪用接口被允许的ip地址$valid_ip = array('192.168.14.2','192.168.14.1','192.168.14.128');$client_ip = $_SERVER['REMOTE_ADDR'];$fs = fopen('./auto_hook.log', 'a');fwrite($fs, 'Request on ['.date("Y-m-d H:i:s").'] from ['.$client_ip.']'.PHP_EOL);$json_content = file_get_contents('php://input');$data = json_decode($json_content, true);fwrite($fs, 'Data: '.json_encode($data).PHP_EOL);fwrite($fs, '======================================================================='.PHP_EOL);$fs and fclose($fs);if (empty($data['token']) || $data['token'] !== $valid_token) { exit('aInvalid token request');}$repo = $data['repository']['name'];$cmd = "cd $web_path && git pull";shell_exec($cmd);在hook目录下建设一个自己coding 项目名(只是为了统一,你可以新建一个其他的):auto-test最后的目录结构为:├── hook│ ├── auto-test│ │ │ └── index.php6、修改目录权限chmod -R u+x /home/www/web/hook7、域名剖析剖析一个域名到Linux系统,使用Nginx做一个署理,nginx虚拟主机设置信息如下:server { server_name auto.tinywan.com; set $root_path /home/www/web/hook; root $root_path; location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } } location ~ .php$ { fastcgi_pass unix:/var/run/php7.1.9-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_connect_timeout 10000; fastcgi_send_timeout 6000; fastcgi_read_timeout 6000; }}以上域名 auto.tinywan.com已经被A记载到Linux外网IP了,阿里云域名剖析8、验证的hook钩子目录的index.php文件可以会见会见:http://auto.tinywan.com/index.php输出:error request // 表现可以正常会见9、设置git git config --global user.name "Tinywan" git config --global user.email "Tinywan@gmail.com" # 邮箱请与conding上一致10、设置公钥复制:/home/www.ssh/id_rsa.pub内容到小我私家设置页https://coding.net/user/account/setting/keys,【新增公钥】添加即可id_rsa.pub 文件内容ssh-rsa AAAAB3NzaC1yc2EAAAADAQABBBABAQChrujULy3U56wS5jLJ0rAJMtv2MNABhbqU1kaiiiyUGFz9+Ndwel8o4dW4whmFRWBodDppc2gpDcF/UM6v7DLzHYOd/38BDp0vRz+zhgZ0BCfyeUV958tpTI6uQyjFil3jwDrKvDqeS4eVnb1fJZfnk/utcFCkVSjhae1sBqM10bkaQmsmwLKr7fN6DeUox9nYkknDqaD645wYplW/qFAXItHOaaZzgTpbAuEb4uss0BCtiutsDFsJwcuXlAsvg4xwsTmagdlz+FhTksCnGALcB10kaz0EY2g9NOHVCqQ4QU4TyNmUVwBHYfj6LAGALO4NAHfwErzKgqfRhBLzDsKB www@Tinywan11、设置 WebHook选择项目(auto-test) > 设置 > 【WebHook】 > 【新建 WebHook】 > 粘贴你的hook/index.php所在的网址:http://auto.tinywan.com/index.php, 令牌可选,可是建议写上。稍过几秒刷新页面检察hook状态,显示为绿色勾就OK了12、服务端初始化项目我们需要先在服务器上clone一次,以后都可以实现自动部署了sudo -Hu www git clone https://git.coding.net/Tinywan/auto-test.git /home/www/web/hook/auto-test/ --depth=113、Windows客户端(1)开发端也克隆一份代码$ git clone https://git.coding.net/Tinywan/auto-test.gitCloning into 'auto-test'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.Checking connectivity... done.(2)新建文件index.php<?phpecho "Hell Coding";(3)提交当地的代码$ git add ./$ git commit -m "test hook"$ git push -u origin master(4)检察服务端文件是否已经更新├── auto-test└── index.php发现目的目录里就适才提交的index.php文件了立刻会见:http://auto.tinywan.com/auto-test/index.php钩子file_get_contents('php://input')接受的文件内容{"ref": "refs/heads/master","before": "90d67c99a3077a7a6823c50a95275812471ecf47","commits": [{"committer": {"name": "Tinywan","email": "756684177@qq.com"},"web_url": "https://coding.net/u/Tinywan/p/auto-test/git/commit/3e55e1c6aa0d064ba4fede1556f0e2bb14c0bed3","short_message": "json_encode($_SERVER)n","sha": "3e55e1c6aa0d064ba4fede1556f0e2bb14c0bed3"}],"after": "3e55e1c6aa0d064ba4fede1556f0e2bb14c0bed3","event": "push","repository": {"owner": {"path": "/u/Tinywan","web_url": "https://coding.net/u/Tinywan","global_key": "Tinywan","name": "Tinywan","avatar": "/static/fruit_avatar/Fruit-14.png"},"https_url": "https://git.coding.net/Tinywan/auto-test.git","web_url": "https://coding.net/u/Tinywan/p/auto-test","project_id": "3351025","ssh_url": "git@git.coding.net:Tinywan/auto-test.git","name": "auto-test","description": "auto-test"},"user": {"path": "/u/Tinywan","web_url": "https://coding.net/u/Tinywan","global_key": "Tinywan","name": "Tinywan","avatar": "/static/fruit_avatar/Fruit-14.png"},"token": "1954FD0D6"}以下是完整的钩子 完整的钩子代码(兼容GitHub和Coding)<?phperror_reporting(1);// 设置$secret = '1989BC88338CB4DABEF20BD7C54FD0D6';$userAgent = $_SERVER['HTTP_USER_AGENT'];$signature = 'sha1=e0ec9317f440f3fd47631852ef585c6b2680e8f8';if (substr_count($userAgent, 'GitHub') >= 1) { $signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];} elseif (substr_count($userAgent, 'Coding') >= 1) { $signature = $_SERVER['HTTP_X_CODING_SIGNATURE'];}list($hash_type, $hash_value) = explode('=', $signature, 2);$jsonContent = file_get_contents("php://input");$checkHash = hash_hmac($hash_type, $jsonContent, $secret); // e0ec9317f440f3fd47631852ef585c6b2680e8f8$fs = fopen('./auto_hook.log', 'a');$data = json_decode($jsonContent, true);fwrite($fs, 'Request on [' . date("Y-m-d H:i:s") . '] from [' . $data['pusher']['name'] . ']' . PHP_EOL);fwrite($fs, 'Data: '.json_encode($data).PHP_EOL);fwrite($fs, 'Service '.json_encode($_SERVER).PHP_EOL);// sha1 验证if ($checkHash && $checkHash === $hash_value) { fwrite($fs, '认证乐成,开始更新 ' . PHP_EOL); $repository = $data['repository']['name']; $pwd = getcwd(); $command = 'cd .. && cd ' . $repository . ' && git pull'; fwrite($fs, 'command '.$command.PHP_EOL); if (!empty($repository)) { shell_exec($command); fwrite($fs, $repository . ' 更新完成 ' . PHP_EOL); } $fs and fclose($fs);}举两个例子,怎么样写好代码最经典的算法,献给正在面试门路上的你如果你现在在面试PHP的门路上,看看面试基础题吧Redis相关面试题。


本文关键词:服务器,部署,使用,WebHook,实现,PHP,自动,Git,华体会官网

本文来源:华体会-www.holabinks.com