分类 git 下的文章

  • 创建 index.php 发布入口文件
<?php
if (empty($_SERVER['DOCUMENT_ROOT']) || strpos($_SERVER['DOCUMENT_ROOT'], ':') === false) {
    $hostSystem = 'linux';
} else {
    $hostSystem = 'windows';
}
echo '<h1><a href="">系统版本: ' . $hostSystem . '</a></h1>';

$baseDir = dirname(__FILE__, 2);
$baseDir = str_replace('\\', '/', $baseDir);
$dirs = scandir($baseDir);
unset($dirs[0]);
unset($dirs[1]);
$dirList = [];
foreach ($dirs as $key => $dir) {
    if (!is_dir($baseDir . '/' . $dir . '/.git')) {
        unset($dirs[$key]);
    } else {
        $dirListItem = [
            'value' => $dir,
            'app_type' => '',
        ];
        if (file_exists($baseDir . '/' . $dir . '/src/public/index.php') || file_exists($baseDir . '/' . $dir . '/public/index.php')) {
            $dirListItem['app_type'] = 'laravel';
        } else if (is_dir($baseDir . '/' . $dir . '/src/runtime') || is_dir($baseDir . '/' . $dir . '/runtime')) {
            $dirListItem['app_type'] = 'hyperf';
        } else {
            $dirListItem['app_type'] = 'front';
        }
        $dirList[$dir] = $dirListItem;
    }
}
$dirs = array_values($dirs);
$requestMethod = empty($_SERVER['REQUEST_METHOD']) ? 'GET' : $_SERVER['REQUEST_METHOD'];
switch ($requestMethod) {
    case 'GET':
        echo '<form action="" method="POST">
<h3>选择需要执行的目录: </h3>
';
        $dirLists = [];
        foreach ($dirList as $dirListItem) {
            $dirLists[$dirListItem['app_type']][] = $dirListItem;
        }
        foreach ($dirLists as $appType => $dirList) {
            echo '<h2><font style="color: blue;">' . $appType . '</font></h2>';
            foreach ($dirList as $dirListItem) {
                echo '<label><input type="radio" name="dir" value="' . $dirListItem['value'] . '">' . $dirListItem['value'] . '</label>';
            }
        }
        echo '
<br><input type="submit" value="执行">
</form>
<style>
    label{
        font-weight: lighter;
    }
</style>';
        break;
    case 'POST':
        // 执行发布
        $request = $_REQUEST;
        $dir = empty($_POST['dir']) ? '' : $_POST['dir'];
        if (empty($dir)) {
            die('请选择目录');
        }
        if (empty($dirList[$dir])) {
            die('目录不在白名单中');
        }

        $cmd = [];

        $appType = $dirList[$dir]['app_type'];
        $startTTL = microtime(true);
        echo "<pre>";
        if ($hostSystem == 'windows') {
        } else {
            $da = [];

            $branch = 'develop';
            $branch = 'master';
            switch ($appType) {
                case 'hyperf':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_hyperf.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                case 'laravel':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_laravel.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                case 'front':
                    $shell = 'sudo ' . dirname(__FILE__) . '/php_deploy_front.sh ' . $dir . ' ' . $branch;
                    echo '<h3>' . $shell . '</h3>';
                    system($shell, $status);
                    break;
                default:
                    die('未能识别需要执行的代码类型');
            }
        }
        echo '用时: ' . intval((microtime(true) - $startTTL) * 1000) / 1000;
        echo '</pre>';
        if ($status) {
            echo "执行失败";
        } else {
            echo "执行成功";
        }
        break;
}
  • vim 前端用到的发布文件 vim php_deploy_front.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project
project_name=$1
echo $project_dir"/"$project_name
branch=$2

function gitPullCover(){
    project_name=$1
    branch=$2
    project_dir=$3
    self_path=$project_dir"/history/"$project_name;
    cp -rf $project_dir"/"$project_name $project_dir"/history/"
    history_path=$project_dir"/history/"$project_name
    covertimes=5;
    if [ ! -d $history_path ];then
      mkdir $history_path
      covertimes=20
    fi
    history_path=$self_path"_history"
    if [ ! -d $history_path ];then
      mkdir $history_path
    fi

    echo "覆盖"covertimes"版本"
    covertimes=5
    for vv in {5..1}
    do
        cd $self_path
        git checkout $branch
        git_hashcode=`git log -n $vv --pretty=format:"%H"`
        v2=0;
        for line in $git_hashcode
        do
            declare -i v2=$v2+1;
            if [ "$v2" -eq "$vv" ]
            then
                echo $line;
                git checkout $line
                git reset --hard
                cd ../
                echo "$self_path/* $history_path"
                cp -rf $self_path/* $history_path
            fi
        done
    done
    cd $self_path
    git checkout $branch
}

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $branch
}

function dockerReload(){
    cd $1"/src"
    docker-compose down && docker-compose up -d
}

gitPullCover $project_name $branch $project_dir

gitPull $project_dir"/"$project_name $branch
  • vim 后端laravel用到的发布文件 vim php_deploy_laravel.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project/"$1"
echo $project_dir
branch=$2

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $2
}

gitPull $project_dir $branch
  • vim 后端hyperf框架用到的发布文件 vim php_deploy_hyperf.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/root/.nvm/versions/node/v13.9.0/bin
export PATH
project_dir=/home/project/"$1"
echo $project_dir
branch=$2

function gitPull(){
    cd $1
    echo `git log -n 1`
    git log -n 1
    git checkout .
    git pull origin $branch
}

function dockerReload(){
    cd $1"/src"
    docker-compose down && docker-compose up -d
}

gitPull $project_dir

dockerReload $project_dir
  • 如果无法执行, 检查是否权限问题, 是否开启发布的用户的 sudo 的免密登录

  • 回退一个版本, 会删除本地最后一个版本(危险操作, 慎用)
git reset HEAD~1 && git reset --hard
  • 如果此刻再次执行git pull后, 会重新下载到最新的状态
  • 查看第N个提交的hash值, 当前值为1
vv=1;
git_hashcode=`git log -n $vv --pretty=format:"%H"`
v2=0;
for line in $git_hashcode
do
    declare -i v2=$v2+1;
    if [ "$v2" -eq "$vv" ]
    then
        echo $line;
    fi
done
  • 代理执行命令
git clone --config http.proxy="http://user:password@agent.com:1234"  https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

  • 创建ssh公钥和私钥 (最好保存到默认位置, 不然一会儿使用私钥的时候还是从默认位置读取的)
ssh-keygen -m PEM -t rsa -b 4096 -C "website@admin.com"
一路回车就好了
  • 默认公钥和私钥存储于用户家目录 .ssh目录下
[root@localhost .ssh]# pwd
/root/.ssh
[root@localhost .ssh]# ls -la
总用量 28
drwx------. 2 root root    81 7月   7 06:29 .
dr-xr-x---. 7 root root  4096 7月   7 06:20 ..
-rw-------. 1 root root  3243 7月   7 06:29 id_rsa
-rw-r--r--. 1 root root   742 7月   7 06:29 id_rsa.pub
  • 配置代码仓库设置
一般在代码仓库, 设置, 部署公钥 下面

然后就可以愉快的克隆啦


如果是服务端测服正服需要使用一样的发布

拷贝正服的 ~/.ssh/id_rsa,id_rsa.pub 到测服该目录
执行
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
git config --global user.email fabu@qq.com
git config --global user.name fabu
同样的git配置完成