Home >PHP Framework >ThinkPHP >About web hooks server automatically pulling code php thinkphp6

About web hooks server automatically pulling code php thinkphp6

藏色散人
藏色散人forward
2021-06-24 15:18:572326browse

Github preparations

1. Add webhooks in the warehouse settings

Take a private warehouse as an example:
About web hooks server automatically pulling code php thinkphp6

1. After creating the warehouse, click settings.

Set webhooks access path:
About web hooks server automatically pulling code php thinkphp6

Take my own project as an example, the framework uses tp6. Create a file named basic.php under public/, which is a file that github can access and execute when the code is pushed to the server.
This file is used to execute shell commands. When the link is triggered, the commands in the file are automatically executed, thereby realizing automatic git pull.

2. Test whether the link is valid and whether the file has been accessed

About web hooks server automatically pulling code php thinkphp6

As shown in the picture above, when green ✅ appears, it means that the link has been accessed , when the red ⚠️ mark appears, it means that the file has not been accessed. Use this to check whether there is a link problem or a code problem.
  • [x] Github is now complete.

    PHP code deployment

    1. Add a hook file and execute the shell command

    Some inline code pieces are shown below.

<?php     //git webhook 自动部署脚本
    $requestBody = file_get_contents("php://input");    //接收数据
    if (empty($requestBody)) {              //判断数据是不是空
        die(&#39;send fail&#39;);
    }
    $content = json_decode($requestBody, true);     //数据转换
    //若是主分支且提交数大于0
    if ($content[&#39;ref&#39;]==&#39;refs/heads/main&#39;) {
     //或将命令加入 shell里,看个人需求 git reset --hard origin/master && git clean -f
        $res = shell_exec(&#39;cd /www/wwwroot/xxxx/ && echo `sudo git pull` >> b.log');//PHP函数执行git命令
        $res_log = '-------------------------'.PHP_EOL;
        $res_log .= ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push'.$res;
        file_put_contents("git-webhook.txt", $res_log, FILE_APPEND);//将每次拉取信息追加写入到日志里
    }
?>
  • [x] At this point, the php shell part of the command has been completed and can be debugged according to different project requirements.

    Common Error Handling

    1. The shell is not disabled in the .php.ini file. This error is relatively common and is easy to find during debugging, so I won’t go into details.

    Processing method Delete shell_exec in php.ini and restart the service.

    2. .git permission problem. Changing the file permission group in the .git/ directory to www

    will generally not occur. When a permission error occurs, it is recommended to first check whether the file directory is The permission group you set. If not, you can use the following command to modify the file permission group
    //以www用户组为例
    chown -R www:www file/

    3. Permission group problem

    Check step by step. When you are sure that there is no problem with the previous process but the code still does not automatically When pulling, manually execute the above shell. If the code is successfully pulled, the shell problem can be eliminated. Turn on the log record. If the content in the log record is empty, you can try to replace the old version with the new version

    4. An error similar to Host key verification failed.^M fatal: Could not read from remote appears. rep......

    It's because there is a problem with the permission key. The public key to connect to github is not created in the www permission group
    The author here uses this method to solve it. If there is a big boss If you have other solutions, please leave a message in private message or comment area.
    vim Open /etc/sudoers Add a line of www under root to execute sudo permissions without password
    About web hooks server automatically pulling code php thinkphp6
    ##5. Finished, now your webhook is It's ready to work. Modify the file and submit it, and query the log.

    About web hooks server automatically pulling code php thinkphp6

    Local environment centos php7.3 nginx

The above is the detailed content of About web hooks server automatically pulling code php thinkphp6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete