Skip to content

Instantly share code, notes, and snippets.

@rufhausen
Last active August 10, 2021 08:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rufhausen/5e4c746e0150b6aa1d44 to your computer and use it in GitHub Desktop.
Save rufhausen/5e4c746e0150b6aa1d44 to your computer and use it in GitHub Desktop.
Laravel Artisan command for pushing to remotes via git
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class Push extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'push';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Push application updates to remote';
public function __construct()
{
parent::__construct();
$this->set_version = true;
$this->set_changelog = true;
$this->local_path_to_git = '/usr/bin/git';
$this->version_file = 'version.txt';
$this->changelog_file = 'changelog.txt';
$this->branch = 'master';
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$remote = $this->argument('remote');
//old habits die hard.
if ($remote == 'prod')
{
$remote = 'production';
}
$remote_path = Config::get('remote.connections.' . $remote . '.root');
$remote_repo_path = Config::get('remote.connections.' . $remote . '.repo_path');
$remote_version_file = $remote_path . '/' . $this->version_file;
$remote_changelog_file = $remote_path . '/' . $this->changelog_file;
//creates local version.txt and local changelog.txt in project root
if ( ! is_null($this->local_path_to_git))
{
if ($this->set_version)
{
exec($this->local_path_to_git . ' describe --tags --abbrev=0 > ' . base_path() . '/' . $this->version_file);
}
if ($this->set_changelog)
{
exec($this->local_path_to_git . ' log --date=short --cherry-pick --oneline --branches=master --format="%cd%n%s%n" --remove-empty --since=3.months > ' . base_path() . '/' . $this->changelog_file);
}
}
$this->info('Pushing to ' . $remote);
if ( ! is_null($this->option('branch')))
{
$this->branch = $this->option('branch');
}
//Git Push to chosen remote
exec('git push ' . $remote . ' ' . $this->branch . ' --tags --force');
$this->info('Git Push Complete');
$commands = [
'cd ' . $remote_repo_path,
'GIT_WORK_TREE=' . $remote_path . ' git checkout ' . $this->branch . ' -f',
];
if ($this->set_version)
{
array_push($commands, 'git describe --tags --abbrev=0 > ' . $remote_version_file);
}
if ($this->set_changelog)
{
array_push($commands, 'git log --date=short --cherry-pick --oneline --branches=master --format="%cd%n%s%n" --remove-empty --since=3.months > ' . $remote_changelog_file);
}
array_push($commands, 'cd ' . $remote_path);
array_push($commands, 'composer install');
array_push($commands, 'php artisan cache:clear');
array_push($commands, 'rm -f app/storage/cache/*');
array_push($commands, 'rm -f app/storage/views/*');
array_push($commands, 'php artisan migrate --env=' . $remote);
array_push($commands, 'chmod 774 -R ' . $remote_path . ' -f');
//Remote Commands
SSH::into($remote)->run($commands);
$this->info('Git Checkout Complete');
Slack::send('Application updates have been pushed to ' . $remote, true);
$this->info('Operation Complete!');
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('remote', InputArgument::REQUIRED, 'Remote Host'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('branch', null, InputOption::VALUE_OPTIONAL, 'branch', null),
);
}
}
@vahidalvandi
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment