Last active
April 24, 2024 13:16
-
-
Save leonvanrooijen/9ed0587bd73eb6d4ce86aa5830deb5a5 to your computer and use it in GitHub Desktop.
Laravel GIT pull command to automatically update the application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Process\Process; | |
class RefreshApp extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'app:refresh'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Pull files from GIT'; | |
/** | |
* Is the code already updated or not | |
* | |
* @var boolean | |
*/ | |
private $alreadyUpToDate; | |
/** | |
* Log from git pull | |
* | |
* @var array | |
*/ | |
private $pullLog = []; | |
/** | |
* Log from composer install | |
* | |
* @var boolean | |
*/ | |
private $composerLog = []; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
if(!$this->runPull()) { | |
$this->error("An error occurred while executing 'git pull'. \nLogs:"); | |
foreach($this->pullLog as $logLine) { | |
$this->info($logLine); | |
} | |
return; | |
} | |
if($this->alreadyUpToDate) { | |
$this->info("The application is already up-to-date"); | |
return; | |
} | |
if(!$this->runComposer()) { | |
$this->error("Error while updating composer files. \nLogs:"); | |
foreach($this->composerLog as $logLine) { | |
$this->info($logLine); | |
} | |
return; | |
} | |
$this->info("Succesfully updated the application."); | |
} | |
/** | |
* Run git pull process | |
* | |
* @return boolean | |
*/ | |
private function runPull() | |
{ | |
$process = new Process('git pull'); | |
$this->info("Running 'git pull'"); | |
$process->run(function($type, $buffer) { | |
$this->pullLog[] = $buffer; | |
if($buffer == "Already up to date.\n") { | |
$this->alreadyUpToDate = TRUE; | |
} | |
}); | |
return $process->isSuccessful(); | |
} | |
/** | |
* Run composer install process | |
* | |
* @return boolean | |
*/ | |
private function runComposer() | |
{ | |
$process = new Process('composer install'); | |
$this->info("Running 'composer install'"); | |
$process->run(function($type, $buffer) { | |
$this->composerLog[] = $buffer; | |
}); | |
return $process->isSuccessful(); | |
} | |
} |
hello, please how can i add a ssh key to this git pull to access to a private repository
hello, please how can i add a ssh key to this git pull to access to a private repository
You should add the ssh key to server, not to "git" command.
I hope this can help you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$process = new Process('git pull');
the correct syntax is$process = new Process(['git', pull']);