PullCode.php 1.3 KB
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class PullCode extends Command
{
    protected $signature = 'pull_code';
    protected $description = '自动拉取代码';


    public function handle()
    {
        //查询更新代码任务
        $is_pull = Redis::get('pull_code');
        if (!$is_pull) {
            return true;
        }

        try {
            //拉取代码
            $this->pullCode();
        } catch (\Exception $e) {
            Log::error('pull_code | error:' . $e->getMessage());
            $this->output($e->getMessage());
        }

        Redis::set('pull_code', false);
        return true;
    }

    /**
     * 拉取代码
     * @return bool
     * @author Akun
     * @date 2024/08/14 16:00
     */
    public function pullCode()
    {
        $this->output('自动拉取代码: start.');

        $shell = "cd /www/wwwroot/self_site && sudo git checkout . && sudo git pull 2>&1";
        exec($shell, $out);
        dump($out);

        $this->output('自动拉取代码: end.');

        return true;
    }

    /**
     * 输出处理日志
     * @param $message
     */
    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
    }
}