2021年2月

项目托管到码云,每一次提交想自动同步到我的服务器,不想每次都通过winscp这种来上传代码:

1、码云上项目》管理》WebHook,配置你的项目链接,如:http://xxx.xxxmobile.com/hook.php,不用配置密码啥的,直接一个链接就行

2、把hook.php放入网站根目录(public下),代码内容:

<?php
$json = file_get_contents("php://input");

$data = json_decode($json,true);

if (isset($data['ref']) && $data['total_commits_count']>0) {

    $res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL;
//cd你的服务器项目地址,pull你的码云项目地址
    $res .= shell_exec("cd /data/wwwroot/xxx.xxxmobile.com && git pull https://gitee.com/xxx/fuma.git 2<&1 ");

    $res_log = '------------------------------------------------------------'.PHP_EOL;

    $res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '项目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '个commit:'.$data['commits'][0]['message'];

    $res_log .= $res.PHP_EOL;

    $res_log .= "pull end -----------------------------------------------------".PHP_EOL;
//日志文件,如果测试失败,会有错误原因描述
    file_put_contents("/data/wwwroot/xxx.xxxmobile.com/public/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//写入日志到log文件中

}
?>
通俗上来说,就是在你的服务器上用git拉取代码,本地提交以后同步到你的链接上,触发pull操作,更新项目
错误1、fatal: Not a git repository:在你的项目目录下执行 git init 即可
错误2、Permission denied:权限不足,你可以直接粗暴的把你的项目权限给成777(正式上线部署时不要这样)
错误3、Untracked working tree file,执行一下Git流程,在项目目录下,3步走:
    1、git reset --hard HEAD

    2、git clean -f -d
//码云项目地址
    3、git pull https://gitee.com/xxx/fuma.git

完成以上,测试即可通过,其他错误暂未遇到

public function downloadOne()
    {
        $file_name = input('file_name'); //远程地址或本地都可以
        $mime = 'application/force-download';
        header('Pragma: public'); // required
        header('Expires: 0'); // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        readfile($file_name); // push it out
        exit();
    }