Last active
October 31, 2016 19:36
-
-
Save nbpalomino/4e12740c2f082ce43c7a to your computer and use it in GitHub Desktop.
Script for Deploy using Webhooks
This file contains hidden or 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 | |
interface GitProvider { | |
public function getRepository(); | |
public function getUser(); | |
public function getCommit(); | |
public function getBranch(); | |
public function hashCommit(); | |
} | |
class Git { | |
private $path; | |
private $repo; | |
public $output; | |
public function __construct($repo, $path = '/usr/bin/git') | |
{ | |
$this->path = $path; | |
$this->repo = "/var/www/html/" . $repo; | |
} | |
public function pull() | |
{ | |
return shell_exec("cd {$this->repo}; {$this->path} pull"); | |
} | |
public function fetch() | |
{ | |
return shell_exec("cd {$this->repo}; {$this->path} fetch"); | |
} | |
} | |
class GitLab implements GitProvider { | |
/** | |
* var data array | |
*/ | |
protected $data; | |
public function __construct($data) | |
{ | |
$this->data = $data; | |
} | |
public function getRepository() | |
{ | |
return $this->data->repository->name; | |
} | |
public function getUser() | |
{ | |
return [ | |
'name' => $this->data->user_name, | |
'id' => $this->data->user_id, | |
'email' => $this->data->user_email | |
]; | |
} | |
public function getCommit() | |
{ | |
return $this->data->commits[0]; | |
} | |
public function getBranch() | |
{ | |
$branch = explode('/', $this->data->ref); | |
$branch = isset($branch[2]) ? $branch[2] : 'master'; | |
return $branch; | |
} | |
public function hashCommit() | |
{ | |
return substr($this->getCommit()->id, 0, 7); | |
} | |
} | |
class Log { | |
protected $file; | |
public function __construct($repo = 'repository') | |
{ | |
$this->file = __DIR__ . "/log/" . $repo . ".log"; | |
} | |
public function add($data) | |
{ | |
file_put_contents($this->file, date('d-m-Y h:i:s a') . $data . "\n", FILE_APPEND); | |
} | |
} | |
class Payload { | |
public function __construct(GitProvider $provider) | |
{ | |
$this->provider = $provider; | |
$this->git = new Git($this->provider->getRepository()); | |
$this->log = new Log($this->provider->getRepository()); | |
} | |
public function checkCommit() | |
{ | |
$hash = $this->provider->hashCommit(); | |
// Si el commit contiene la clave [deploy] entonces pull | |
if ( preg_match('/deploy/i', $this->provider->getCommit()->message) ) { | |
$res = $this->git->pull(); | |
$method = 'pull'; | |
} else { | |
$res = $this->git->fetch(); | |
$method = 'fetch'; | |
} | |
$this->log->add(" Resultado: {$res} | Repository:{$this->provider->getRepository()} | Commit:{$hash} | Author:{$this->provider->getUser()['name']} | Branch: {$this->provider->getBranch()} | Git Method:{$method} "); | |
} | |
} | |
$data = json_decode(trim(file_get_contents('php://input'))); | |
// $fp = fopen('php://stdout', 'w'); | |
// fwrite($fp, json_encode($data)); //User will see Hello World! | |
// fclose($fp); | |
//file_put_contents(__DIR__.'/test.log', json_encode($data) . "\n", FILE_APPEND); | |
$deploy = new Payload(new GitLab($data)); | |
$deploy->checkCommit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CONFIGURACION
Habilitar el Deploy Key para el usuario www-data
Link: http://stackoverflow.com/questions/7306990/generating-ssh-keys-for-apache-user
Dar permisos al usuario www-data