gitee 和 GitHub 的 webhook 的使用,实现服务器代码的自动更新。

webhook实现服务器代码的自动更新。

使用背景

测试服务器下,我个人觉得每一次commit之后,都要去pull一下,确实是一件比较烦的事情。

于是为了更加便捷的同步服务代码,webhook无法就是你的最佳选择。

每次 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求。

配置问题

1、仓库地址使用ssh例如:git@gitee.com:xxxxx/project.git

2、在gitee或者github上的对应的项目添加你的部署公钥

3、在gitee或者github上的对应的项目添加WebHook对应的url和访问秘钥

代码示例(PHP)

<?php

//访问秘钥
$keySecret = '^!@KVzPZ^DaRNA7R53353%7aEAfsfdOptH7b%0i5';

//需要更新的项目目录
$wwwRoot = [
    '/www/wwwroot/test1',
    '/www/wwwroot/test2',
];

//保存运行脚本的日志
$logFile = 'log/layuimini-githook.log';

//git执行命令
$gitCommand = 'git pull';

// 判断是否开启秘钥认证(已实现gitee和github)
if (isset($keySecret) && !empty($keySecret)) {
    list($headers, $gitType) = [[], null];
    foreach ($_SERVER as $key => $value) {
        'HTTP_' == substr($key, 0, 5) && $headers[str_replace('_', '-', substr($key, 5))] = $value;
        if (empty($gitType) && strpos($key, 'GITEE') !== false) {
            $gitType = 'GITEE';
        }
        if (empty($gitType) && strpos($key, 'GITHUB') !== false) {
            $gitType = 'GITHUB';
        }
    }
    if ($gitType == 'GITEE') {
        if (!isset($headers['X-GITEE-TOKEN']) || $headers['X-GITEE-TOKEN'] != $keySecret) {
            die('GITEE - 请求失败,秘钥有误');
        }
    } elseif ($gitType == 'GITHUB') {
        $json_content = file_get_contents('php://input');
        $signature = "sha1=" . hash_hmac('sha1', $json_content, $keySecret);
        if ($signature != $headers['X-HUB-SIGNATURE']) {
            die('GITHUB - 请求失败,秘钥有误');
        }
    } else {
        die('请求错误,未知git类型');
    }
}

!is_array($wwwRoot) && $wwwRoot = [$wwwRoot];
foreach ($wwwRoot as $vo) {
    $shell = sprintf("cd %s && git pull 2>&1", $vo);
    $output = shell_exec($shell);
    $log = sprintf("[%s] %s \n", date('Y-m-d H:i:s', time()) . ' - ' . $vo, $output);
    echo $log;
    file_put_contents($logFile, $log, FILE_APPEND);
}

权限问题

由于使用webhook出发回调访问的时候是,运行的是php-fpm的执行用户,例如我的是www,可使用ps -ef,查看对应进程的执行用户。

修改项目目录权限chown -R www:www 项目路径

公钥问题

如果提示下方这两点,就是公钥问题

[2019-09-21 14:03:27] Could not create directory '/home/www/.ssh'.
Host key verification failed.
fatal: Could not read from remote repository.
[2019-09-21 14:11:00] Host key verification failed.
fatal: Could not read from remote repository.

1、使用php-fpm执行用户www进行登录

2、进入\home\www目录下执行ssh-keygen -t rsa -C "xxxxx@xxxxx.com"

3、把id_rsa.pub内的公钥信息拷贝出来,在gitee或者github上的对应的项目添加你的部署公钥

备注信息

建议自动更新只使用在测试服务器上。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 5

那么问题来了,这个php文件放到哪呢?

4年前 评论
Mr_Chung (楼主) 4年前
UpGod (作者) 4年前
Mr_Chung (楼主) 4年前
Mr_Chung (楼主) 4年前
kis龍 4年前
UpGod (作者) 4年前

好想法

4年前 评论
Mr_Chung (楼主) 4年前

当初处理自动部署考虑过php写个文件但是觉得好麻烦(权限、对外访问什么的。)。。可以看一下我这篇文章,感觉现在云函数挺好的,毕竟我是一个云程序员。。。。

4年前 评论
Mr_Chung (楼主) 4年前
UpGod 4年前
Mr_Chung (楼主) 4年前
UpGod 4年前

你好,我使用了这个脚本执行 gitee 的 webhook 服务器的 php 环境是用宝塔面板搭建的,用的 www 用户创建的 git 公钥。但是 webhook 执行的时候显示 sh: git: command not found 请问这是什么原因?

3年前 评论
Mr_Chung (楼主) 3年前
chesterlyd (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!