Last active
December 5, 2016 19:06
-
-
Save gilbitron/97f2ff5cea00307b4707 to your computer and use it in GitHub Desktop.
Laravel Post Deployment Command
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 | |
use Illuminate\Console\Command; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
/** | |
* Laravel Post Deployment Command | |
* Author: Gilbert Pellegrom (http://gilbert.pellegrom.me) | |
* License: MIT | |
* | |
* Install: | |
* - Put this file in app/commands | |
* - Add "Artisan::add(new PostDeploy);" to app/start/artisan.php | |
* | |
* Usage: | |
* php artisan post-deploy | |
* php artisan post-deploy --env=production | |
* (use --env to pass an environment to the artisan "migrate" command) | |
* | |
*/ | |
class PostDeploy extends Command { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'post-deploy'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Post deploy setup.'; | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function fire() | |
{ | |
$env = $this->option('env') ? ' --env='. $this->option('env') : ''; | |
$this->runExec('php artisan down'); | |
$this->runExec('composer update'); | |
$this->runExec('php artisan migrate'. $env); | |
$this->runExec('php artisan up'); | |
} | |
/** | |
* Get the console command options. | |
* | |
* @return array | |
*/ | |
protected function getOptions() | |
{ | |
return array( | |
array('env', null, InputOption::VALUE_OPTIONAL, 'Environment to pass to migrate command') | |
); | |
} | |
/** | |
* Utility function to run exec() | |
* | |
* @return mixed | |
*/ | |
private function runExec($command) | |
{ | |
$this->comment('Running command: '. $command); | |
exec($command, $output, $return); | |
if(!empty($output)){ | |
foreach($output as $line){ | |
if($return !== false){ | |
$this->info($line); | |
} else { | |
$this->error($line); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment