Skip to content

Instantly share code, notes, and snippets.

@jackvial
Last active August 29, 2015 14:04
Show Gist options
  • Save jackvial/001007f1f20f8e2e9220 to your computer and use it in GitHub Desktop.
Save jackvial/001007f1f20f8e2e9220 to your computer and use it in GitHub Desktop.
Ubuntu VM

Generate key pair for www-data

sudo -u www-data:www-data ssh-keygen -t rsa

Keys will be created in /var/www/.ssh/

Add id_rsa.pub to github deploy keys

All project files and directories to be triggered from github webhook should be owned by www-data user

sudo chown www-data:www-data -R my-repo/

Add ssh url to remote repo list

sudo -Hu www-data git remote add githubrepo [email protected]/karstacian/repo.git

Execute commands as www-data user(github web hook executes commands as www-data)

sudo -Hu www-data rest of command
  1 <?php
  2 #Only accept github payload request
  3 #content type must  x-www-form-urlencodeded
  4 
  5 if ( $_POST['payload'] )
  6 {
  7         $cmd1 = shell_exec("cd /var/www/html/ssrs_si/ && git pull github testing 2>&1");
  8         #var_dump($cmd1);
  9 
 10         $obj = json_decode($_POST['payload']);
 11 
 12         #Log events
 13         $file = './webhooklogs.txt';
 14         #Open the file to get existing content
 15         $current = file_get_contents($file);
 16         #Append info about gtihub payload
 17         $current .= 'Pusher name: '.json_encode($obj->pusher->name) .
 18                     'Repo name: '.json_encode($obj->repository->name) .
 19                     'Created at: '.json_encode($obj->repository->created_at) .
 20                     'Pushed at: '.json_encode($obj->repository->pushed_at);
 21         #Write the contents back to the file
 22         file_put_contents($file, $current);
 23 }
 24 ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment