Skip to content

Instantly share code, notes, and snippets.

@noxify
Last active November 20, 2016 17:34
Show Gist options
  • Save noxify/e35edf67dc3719f6296b to your computer and use it in GitHub Desktop.
Save noxify/e35edf67dc3719f6296b to your computer and use it in GitHub Desktop.
Simple Command to sync/deploy your changes from a public Github Repository to your local/remote server
<?php
//config/sync.php
return [
'repositories' => [
'soa-sentinel' => [
'repo_path' => 'git_doc/soa-sentinel/',
'owner' => 'Pseudoagentur',
'repo_slug' => 'soa-sentinel-docs',
'branches' => [
'develop',
'master',
'feature/3.0.1'
]
],
'admin-bridge' => [
'repo_path' => 'git_doc/admin-bridge/',
'owner' => 'Pseudoagentur',
'repo_slug' => 'soa-sentinel-docs',
'branches' => null
]
]
];
?>
<?php
//app/Console/Command/Sync.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
class Sync extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'doc:sync';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync external docs to your local environment (e.g. docit)';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$repositories = config('sync.repositories');
foreach ($repositories as $key => $repository) {
$branches = ( empty($repository['branches']) ) ? array('master') : $repository['branches'];
foreach ($branches as $branch) {
$doc_path = base_path($repository['repo_path'].$branch);
$git_uri = sprintf('https://github.com/%s/%s.git', $repository['owner'], $repository['repo_slug']);
if (!is_dir($doc_path) && !mkdir($doc_path, 0700, true)) {
throw new \RuntimeException(sprintf('Cannot create path %s', $repoDir));
}
if (is_dir($doc_path.'/.git')) {
$command = sprintf('cd %s && git reset --hard HEAD && git pull', $doc_path);
} else {
$command = sprintf('git clone %s %s && cd %s && git checkout %s', $git_uri, $doc_path, $doc_path, $branch);
}
$this->info( sprintf("Status for %s/%s - Branch: %s", $repository['owner'], $repository['repo_slug'], $branch) );
$this->info(exec($command));
}
}
}
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment