php搭建webhooks2.0代码

具体代码

如果自动发布失败,多半是因为权限问题,使用 sudo chown -R www:www /mnt/www/wwwroot/testsld 更改权限即可

更改完之后,调试方法,切换 www 用户执行(php 一般为 www 角色):sudo -u www /usr/bin/php webhooks.php

<?php
// 秘钥
$secret = "xxx";

// 接入钉钉发布通知
$webhook = "https://oapi.dingtalk.com/robot/send?access_token=5xxxx";

$json = file_get_contents("php://input");
file_put_contents('webhooks.log', $json . PHP_EOL, FILE_APPEND);

// 调试用
//$output = shell_exec("cd /mnt/www/wwwroot/testsld && git checkout . && git pull");
//file_put_contents('webhooks.log', date('Y-m-d H:i:s') . '    [test]- ' . $output . PHP_EOL, FILE_APPEND);

if (empty($json)) {
    die("require is empty!");
}

$data = json_decode($json, true);
//if($data['password'] != $secret){
//    die("password error!");
//}


// 如果是devtest分支提交,才进行操作
if ($data['ref'] == 'refs/heads/dev') {

    $output = shell_exec("cd /mnt/www/wwwroot/testsld && git pull");
    file_put_contents('webhooks.log', date('Y-m-d H:i:s') . '    - ' . $output . PHP_EOL, FILE_APPEND);

    $isSuccess = $output && strpos($output, 'Fast-forward') !== false;
    $retry = 0;
    while (!$isSuccess) {
        if ($retry > 4) {
            $message = "testsld测试环境代码自动发布失败!需要手动操作。推送者:{$data['user_name']}";
            $pushData = array('msgtype' => 'text', 'text' => array('content' => $message));
            $result = request_by_curl($webhook, $pushData, $secret);
            break;
        }
        sleep(1);

        $output = shell_exec("cd /mnt/www/wwwroot/testsld && git checkout . && git pull");
        file_put_contents('webhooks.log', date('Y-m-d H:i:s') . '    [retry]- ' . $output . PHP_EOL, FILE_APPEND);

        $retry++;
        $isSuccess = $output && strpos($output, 'Fast-forward') !== false;
    }

    if ($isSuccess) {
        $num = count($data['message']);
        $commitMsg = $data['commits'][$num-1]['message'] ?? '';
        $message = "testsld测试环境代码自动发布成功!\n版本号:{$data['after']}\n发布内容:{$commitMsg}\n推送者:{$data['user_name']}";
        $data = array('msgtype' => 'text', 'text' => array('content' => $message));
        $result = request_by_curl($webhook, $data, $secret);
        echo $result;
    }
}


function request_by_curl($remote_server, $data, $secret)
{

    $microtime = microtime();
    list($millisecond, $timestamp) = explode(' ', $microtime);
    $millisecond_time = sprintf('%.0f', ($timestamp + $millisecond) * 1000);
    $signToSign = $millisecond_time . "\n" . $secret;
    $sign = hash_hmac('sha256', $signToSign, $secret, true);
    $sign = urlencode(base64_encode($sign));
    $remote_server .= "&timestamp=" . $millisecond_time;
    $remote_server .= "&sign=" . $sign;


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。