Skip to content

Instantly share code, notes, and snippets.

@lifesign
Forked from webjay/gh_hook.php
Last active August 29, 2015 14:23

Revisions

  1. @webjay webjay revised this gist Jan 16, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gh_hook.php
    Original file line number Diff line number Diff line change
    @@ -53,8 +53,8 @@ function git_current_branch ($cwd) {
    $branch = substr($payload->ref, strrpos($payload->ref, '/') + 1);

    // If your website directories have the same name as your repository this would work.
    $repository = $payload->repository->name;
    $cwd = '/var/www/'.$repository;
    $repository = $payload->repository->name;
    $cwd = '/var/www/'.$repository;

    // only pull if we are on the same branch
    if ($branch == git_current_branch($cwd)) {
  2. @webjay webjay revised this gist Jan 16, 2014. 1 changed file with 88 additions and 23 deletions.
    111 changes: 88 additions & 23 deletions gh_hook.php
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,100 @@
    <?php

    //error_reporting(E_ALL);

    ignore_user_abort(true);


    function syscall ($cmd, $cwd) {
    $descriptorspec = array(
    1 => array('pipe', 'w') // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    proc_close($resource);
    return $output;
    }
    $descriptorspec = array(
    1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
    2 => array('pipe', 'w') // stderr
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
    $output = stream_get_contents($pipes[2]);
    $output .= PHP_EOL;
    $output .= stream_get_contents($pipes[1]);
    $output .= PHP_EOL;
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($resource);
    return $output;
    }
    }

    function git_current_branch ($cwd) {
    $result = syscall('git branch', $cwd);
    if (preg_match('/\\* (.*)/', $result, $matches)) {
    return $matches[1];
    }
    }

    // make sure the request is coming from GitHub
    // https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist
    /*
    $gh_ips = array('207.97.227.253', '50.57.128.197', '108.171.174.178');
    if (in_array($_SERVER['REMOTE_ADDR'], $gh_ips) === false) {
    header('Status: 403 Your IP is not on our list; bugger off', true, 403);
    mail('root', 'GitHub hook error: bad ip', $_SERVER['REMOTE_ADDR']);
    exit();
    }
    */


    // cd ..
    // $cwd = dirname(__DIR__);

    // GitHub will hit us with POST (http://help.github.com/post-receive-hooks/)
    if (!empty($_POST['payload'])) {

    // pull from master
    $result = syscall('git pull', '/var/www/example.com');

    // send us the output
    mail('[email protected]', 'GitHub hook `git pull` result', $result);

    // clear APC
    if (apc_clear_cache('opcode') == false) {
    mail('root', 'Unable to apc_clear_cache()', '');
    }


    $payload = json_decode($_POST['payload']);

    // which branch was committed?
    $branch = substr($payload->ref, strrpos($payload->ref, '/') + 1);

    // If your website directories have the same name as your repository this would work.
    $repository = $payload->repository->name;
    $cwd = '/var/www/'.$repository;

    // only pull if we are on the same branch
    if ($branch == git_current_branch($cwd)) {

    // pull from $branch
    $cmd = sprintf('git pull origin %s', $branch);
    $result = syscall($cmd, $cwd);

    $output = '';

    // append commits
    foreach ($payload->commits as $commit) {
    $output .= $commit->author->name.' a.k.a. '.$commit->author->username;
    $output .= PHP_EOL;
    foreach (array('added', 'modified', 'removed') as $action) {
    if (count($commit->{$action})) {
    $output .= sprintf('%s: %s; ', $action, implode(',', $commit->{$action}));
    }
    }
    $output .= PHP_EOL;
    $output .= sprintf('because: %s', $commit->message);
    $output .= PHP_EOL;
    $output .= $commit->url;
    $output .= PHP_EOL;
    }

    // append git result
    $output .= PHP_EOL;
    $output .= $result;

    // send us the output
    mail('root', 'GitHub hook `'.$cmd.'` result', $output);

    // if you use APC, especially if you use apc.stat=0, we should clear APC
    // if (apc_clear_cache('opcode') == false || apc_clear_cache('user') == false) {
    // mail('root', 'Unable to apc_clear_cache', '');
    // }

    }

    }

    ?>
  3. @webjay webjay revised this gist Oct 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gh_hook.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    function syscall ($cmd, $cwd) {
    $descriptorspec = array(
    1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
    1 => array('pipe', 'w') // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
  4. @webjay webjay revised this gist Oct 19, 2012. 1 changed file with 26 additions and 27 deletions.
    53 changes: 26 additions & 27 deletions gh_hook.php
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,35 @@
    <?php

    <?php
    //error_reporting(E_ALL);
    ignore_user_abort(true);

    //error_reporting(E_ALL);
    ignore_user_abort(true);

    function syscall ($cmd, $cwd) {
    $descriptorspec = array(
    1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    proc_close($resource);
    return $output;
    }
    function syscall ($cmd, $cwd) {
    $descriptorspec = array(
    1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    proc_close($resource);
    return $output;
    }
    }

    // GitHub will hit us with POST (http://help.github.com/post-receive-hooks/)
    if (!empty($_POST['payload'])) {
    // GitHub will hit us with POST (http://help.github.com/post-receive-hooks/)
    if (!empty($_POST['payload'])) {

    // pull from master
    $result = syscall('git pull', '/var/www/example.com');
    // pull from master
    $result = syscall('git pull', '/var/www/example.com');

    // send us the output
    mail('[email protected]', 'GitHub hook `git pull` result', $result);
    // send us the output
    mail('[email protected]', 'GitHub hook `git pull` result', $result);

    // clear APC
    if (apc_clear_cache('opcode') == false) {
    mail('root', 'Unable to apc_clear_cache()', '');
    }

    // clear APC
    if (apc_clear_cache('opcode') == false) {
    mail('root', 'Unable to apc_clear_cache()', '');
    }

    }

    ?>
    ?>
  5. @webjay webjay created this gist Oct 19, 2012.
    36 changes: 36 additions & 0 deletions gh_hook.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@

    <?php

    //error_reporting(E_ALL);
    ignore_user_abort(true);

    function syscall ($cmd, $cwd) {
    $descriptorspec = array(
    1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    proc_close($resource);
    return $output;
    }
    }

    // GitHub will hit us with POST (http://help.github.com/post-receive-hooks/)
    if (!empty($_POST['payload'])) {

    // pull from master
    $result = syscall('git pull', '/var/www/example.com');

    // send us the output
    mail('[email protected]', 'GitHub hook `git pull` result', $result);

    // clear APC
    if (apc_clear_cache('opcode') == false) {
    mail('root', 'Unable to apc_clear_cache()', '');
    }

    }

    ?>