Skip to content

Instantly share code, notes, and snippets.

@mdutt247
Created November 27, 2023 11:00
Show Gist options
  • Save mdutt247/c6e09508ac55f8ab0d14f9b45b54bed0 to your computer and use it in GitHub Desktop.
Save mdutt247/c6e09508ac55f8ab0d14f9b45b54bed0 to your computer and use it in GitHub Desktop.
Laravel command to update the application (git pull and composer install) using Laravel scheduler.
# Author: M. Dutt ([email protected])
# Date: 27/11/2023
# Purpose: Laravel command to update the application (git pull and composer install) using Laravel scheduler.
#
# Running The Scheduler
# The schedule:run Artisan command will evaluate all of your scheduled tasks and determine if they need to run based on the server's current time.
# Create a cron job in your server
# * * * * * cd /path/to/project && php artisan schedule:run >> storage/logs/cron.log 2>&1
#
# Directory structure
# project-dir
# app
# Console
# Commands
# UpdateApp.php
# Kernel.php
#
=========================================================
<?php
// app/Console/Kernal.php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule. MD
*
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('mditech:update-app') // Don't edit this line
->timezone('Asia/Kolkata')
->hourly()
->sendOutputTo('storage/logs/schedule.log')
->emailOutputTo('[email protected]')
->emailOutputOnFailure('[email protected]');
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}
=========================================================
<?php
// app/Console/Commands/UpdateApp.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class UpdateApp extends Command
{
protected $signature = 'mditech:update-app';
protected $description = 'Pull files from GitHub';
private $alreadyUpToDate;
private $pullLog = [];
private $composerLog = [];
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.");
}
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();
}
private function runComposer()
{
$process = new Process(['composer', 'install']);
$this->info("Running 'composer install'");
$process->run(function($type, $buffer) {
$this->composerLog[] = $buffer;
});
return $process->isSuccessful();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment