基本介绍:

使用webhook部署时git的一些设置

环境介绍:

阿里云上部署的Lnmp
git使用的是gitee

部署流程

服务器

  1. git clone 好项目

  2. 创建gitautoload.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?php
    header('Content-Type: text/html; charset=UTF-8');
    $token = 'token';
    $patharr=array(
    'keyword' => "{ProjectPath}"
    );
    $path = $patharr[$_GET['key']];
    $requestBody = file_get_contents("php://input");
    if (empty($requestBody)) {
    die('send fail');
    }
    $content = json_decode($requestBody, true);
    $res_log = '-------------------------'.PHP_EOL;
    if ($content['password'] != $token) {
    $res_log .= 'Waring:'. date('Y-m-d H:i:s'). ' URL:'.$_SERVER['REMOTE_ADDR'].":".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].":".$_SERVER["HTTP_REFERER"].' REQUEST:'.$requestBody.PHP_EOL;
    } else {
    if ($content['ref']=='refs/heads/master' && $content['total_commits_count']>0) {
    $res = shell_exec("cd {$path} && git pull 2>&1");//以www用户运行
    $res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL;
    $res_log .= $res.PHP_EOL;
    }
    }
    $result = iconv("utf-8", "gb2312", $res_log);
    file_put_contents("git-webhook.txt", $result, FILE_APPEND);//追加写入
  3. 进入项目设置git

    1
    2
    git remote set-url origin https://<username>:<password>@gitee.com/<username>/<repo_name>.git
    git branch --set-upstream-to=origin/<branch> master

gitee设置

  1. 访问git项目 https://gitee.com/{username}/{repo_name>}
  2. 进入 管理 -> WebHooks
  3. Url填写gitautoload.php的访问路径,例如:

    1
    http://yourdomain/gitautoload.php?key={keyword}
  4. 填写token

  5. 选择push并提交
    选择测试后可访问 http://yourdomain/git-webhook.txt 查看结果

错误处理

请保证git账户是否正确
git是否有权限(因为是通过shell_exec执行的,项目中git文件夹是否有相应的权限)