Skip to content

Instantly share code, notes, and snippets.

@casaval
Created June 15, 2012 18:13
Show Gist options
  • Save casaval/2937933 to your computer and use it in GitHub Desktop.
Save casaval/2937933 to your computer and use it in GitHub Desktop.
Github WebHook processor
<?php
/**
* Github WebHook processor
* POST to: postreceive.php?key=REPLACE_ME_WITH_A_UNIQUE_KEY
*
* @author Luis Abreu
* @version 0.1
* @copyright Quodis, 24 February, 2011
* @package default
**/
/**
* path to projects in server
**/
define('PROJECTS_PATH', 'PATH_TO_PROJECTS_ON_SERVER');
/**
* server key for authentication
**/
define('SERVER_KEY', 'REPLACE_ME_WITH_A_UNIQUE_KEY');
// parse the json payload
$payload = json_decode($_REQUEST['payload']);
if (!$payload) exit();
// check for payload and server key
if ( $payload->ref === 'refs/heads/master' && $_REQUEST['key'] == SERVER_KEY ) {
// parse the payload for the project name
$project_name = strtolower($payload->{'repository'}->{'name'});
// define the cd directory based on config and project name
$project_directory = PROJECTS_PATH . $project_name;
// cd into the project dir, git reset and pull changes
shell_exec( 'cd ' . $project_directory . '/ && git reset --hard HEAD && git pull' );
}
?>
@casaval
Copy link
Author

casaval commented Jul 3, 2012

change .ssh owner since we're going to write keypairs and known_hosts

sudo chown www-data /var/www/.ssh

create ssh keypair to be added to github project deploy keys

sudo -u www-data ssh-keygen

create the known_hosts file so that www-data connects silently to github

sudo -u www-data touch /var/www/.ssh/known_hosts

cd into a repo and add github to the known hosts

sudo -u www-data git pull

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