Skip to content

Instantly share code, notes, and snippets.

@nbpalomino
Last active October 31, 2016 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbpalomino/4e12740c2f082ce43c7a to your computer and use it in GitHub Desktop.
Save nbpalomino/4e12740c2f082ce43c7a to your computer and use it in GitHub Desktop.
Script for Deploy using Webhooks
<?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();
@nbpalomino
Copy link
Author

nbpalomino commented Oct 20, 2016

CONFIGURACION

Habilitar el Deploy Key para el usuario www-data

Link: http://stackoverflow.com/questions/7306990/generating-ssh-keys-for-apache-user

mkdir -p /var/www/.ssh/
cp /root/.ssh/id_rsa_webhook /var/www/.ssh/id_rsa

chown -R apache:apache /var/www/.ssh
chmod 0700 /var/www/.ssh
chmod 0600 /var/www/.ssh/id_rsa

sudo -u apache ssh -Ta git@git.server.com

Dar permisos al usuario www-data

chown www-data:project -R project
chmod 775 -R project

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