当前位置:   article > 正文

Git Gitee仓库自动部署

gitee .net 自动部署
WechatIMG633.jpeg

环境

配置公钥

  • 查看 nginx 使用的用户和用户组
  1. $ vi /etc/nginx/nginx.conf
  2. user www-data;
  3. ......
  • 生成 www-data 用户的公钥
  1. $ sudo -u www-data ssh-keygen -t rsa -C 'Gitee的登录邮箱'
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/var/www/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /var/www/.ssh/id_rsa.
  7. Your public key has been saved in /var/www/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 70:32:50:0d:07:82:b9:8c:81:37:a7:20:9f:7d:77:0c zhangyuhai@Xoncology.com
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. |. oo.++. |
  13. |= = .o .E |
  14. |.B B + .o |
  15. |. * . .=. o |
  16. | . .S. |
  17. | |
  18. | |
  19. | |
  20. | |
  21. +-----------------+
  • 添加公钥,点击确定
2021-03-31_60643b27486da.png

将 /var/www/.ssh/id_rsa.pub 中的内容复制到公钥一栏中。

创建 test 项目

  • 创建 test/index.html,代码如下
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. Hello Yohann! How are you?
  8. I'm fine~
  9. </body>
  10. </html>
  • 创建 test/webhooks.php,代码如下
  1. <?php
  2. // 以流的方式读取
  3. $requestBody = file_get_contents("php:// input");
  4. if (empty($requestBody)) {
  5. // 这里可以配置异常邮件提醒
  6. die('send fail');
  7. }
  8. // 项目目录
  9. $projectPath = '/home/test/';
  10. // nginx使用的用户组
  11. $nginxGroup = 'www-data';
  12. // nginx使用的用户
  13. $nginxUser = 'www-data';
  14. // 得到请求内容
  15. $requestBody = json_decode($requestBody,true);
  16. // 加密字符串
  17. $secret_post = $requestBody['sign'];
  18. // 时间戳参数,单位毫秒级
  19. $time_stamp = $requestBody['timestamp'];
  20. // 在WebHooks签名密钥一栏填写的密钥信息
  21. $access_token = '123456';
  22. // 参考加密文档https:// gitee.com/help/articles/4290
  23. $secret_join = $time_stamp . "\n" . $access_token;
  24. // 加密字符串
  25. $base64 = base64_encode(hash_hmac('sha256', $secret_join, $access_token, true));
  26. // 推送的是哪个分支就构建哪个分支,如有需要可以更改规则,比如屏蔽某些分支不构建
  27. $branch = str_replace('refs/heads/', '', $requestBody['ref']);
  28. // 最后将请求内容请空
  29. $requestBody = null;
  30. // 项目根目录下的webhooks.log文件,需要在服务器上创建,并给写权限
  31. $fs = fopen('/home/test/webhooks.log', 'a');
  32. fwrite($fs, date('Y-m-d H:i:s') . ' ================ Update Start ===============' . PHP_EOL);
  33. // 请求ip
  34. $client_ip = $_SERVER['REMOTE_ADDR'];
  35. // 把请求的IP和时间写进log
  36. fwrite($fs, date('Y-m-d H:i:s') . ' Request on [' . date("Y-m-d H:i:s") . '] from [' . $client_ip . ']' . PHP_EOL);
  37. // 验证token,有错就写进日志并退出
  38. if ($base64 !== $secret_post) {
  39. fwrite($fs, date('Y-m-d H:i:s') . " Invalid token [{$client_token}]" . PHP_EOL);
  40. $fs and fclose($fs);
  41. header("HTTP/1.1 404 Not Found");
  42. header("Status: 404 Not Found");
  43. exit;
  44. }
  45. // 拉取代码前先赋权限
  46. shell_exec('chown -R '.$nginxGroup.':'.$nginxUser.' '.$projectPath);
  47. // 执行shell命令并把返回信息写进日志,php.ini中shell_exec若被禁用,需要先开启。
  48. $output = shell_exec('git pull origin '.$branch.' 2<&1');
  49. // 拉取代码前再赋权限
  50. shell_exec('chown -R '.$nginxGroup.':'.$nginxUser.' '.$projectPath);
  51. // 代码同步信息写入日志
  52. fwrite($fs, date('Y-m-d H:i:s') . 'Info:' . print_r($output, true) . PHP_EOL);
  53. fwrite($fs, date('Y-m-d H:i:s') . '================ Update End ===============' . PHP_EOL . PHP_EOL);
  54. // 关闭日志文件
  55. $fs and fclose($fs);
  • 项目示例
2021-03-31_60643c3ccfdfe.png

登录服务器,在 /home 目录下,拉取 test 项目。

  • test 目录赋权限
$ chown -R www-data:www-data /home/test/

配置WebHooks

  • 点击添加 webHook
2021-03-31_60644025d352d.png
  • 点击添加
2021-03-31_606440d665bfe.png

注意:这里的签名密钥要跟 webhooks.php 中保持一致。

测试自动部署

  • 首次访问
  1. $ curl 47.117.122.160
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title></title>
  6. </head>
  7. <body>
  8. Hello Yohann! How are you?
  9. I'm fine~
  10. </body>
  11. </html>
  • 编辑 test/index.html,代码如下
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. Hello World!
  8. </body>
  9. </html>

提示:可以在 Gitee 仓库的 master 分支上直接编辑。

  • 再次访问
  1. $ curl 47.117.122.160
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title></title>
  6. </head>
  7. <body>
  8. Hello World!
  9. </body>
  10. </html>

访问内容发生变化,表示配置成功。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/488092
推荐阅读
相关标签
  

闽ICP备14008679号