Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Last active December 24, 2015 10:08
Show Gist options
  • Save wturnerharris/6781587 to your computer and use it in GitHub Desktop.
Save wturnerharris/6781587 to your computer and use it in GitHub Desktop.
Auto-deployment script for php, github, apache.
<?php
/**
* Auto Deployment Script for GitHub and Apache.
*
*
* @since deploy 0.1
*/
$auth = md5('SECRET_PHRASE'); // set a private hash to validate against
if ( empty($_REQUEST['auth']) || $_REQUEST['auth'] != $auth) exit;
$args = array(
'target' => '/path/to/git/repo',
'git' => '/path/to/git',
'mailto' => '[email protected]',
'commands' => array(
'echo $PWD',
"git branch",
"git stash save --keep-index",
"git stash drop",
"git pull",
"git status",
)
);
function deploy($args) {
chdir( $args['target'] );
$git = shell_exec('which git');
if (empty($git)) $git = $args['git'];
$whoami = trim(shell_exec('whoami'));
$output = array(
"<b>$whoami\$ php deploy.php</b>",
"<span>Git Deployment Script, version 0.1 (apache)",
"Creative Commons Attribution-NoDerivs 3.0 Unported",
"Copyright (C) 2013 Wes Turner, Barrel LLC, All Rights Reserved</span>"
);
foreach($args['commands'] as $command){
$command = str_replace('git', $git, $command);
$tmp = shell_exec($command);
if (empty($tmp)) $tmp = "No output.";
$output[] = "<b>$whoami\$ {$command}\n</b>".htmlentities(trim($tmp));
}
$return = implode("\n", $output);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n" .
$headers .= 'Reply-To: [email protected]' . "\r\n" .
$headers .= 'X-Mailer: PHP/' . phpversion();
if (!empty($args['mailto'])) mail( $args['mailto'], 'Latest commit deployed', $output, $headers );
return $return;
} ?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>deploy.php</title>
<style>
body {
background-color: black;
line-height: 1.1em;
margin: 0;
}
pre {
background-color: black;
color: white;
margin: 0;
padding: 5%;
white-space: pre-line;
}
span {
color: #829AAE;
}
b {
color: #6BE234;
font-weight: normal;
}
</style>
</head>
<body>
<pre>
<?php echo deploy($args); ?>
</pre>
</body>
</html>
@wturnerharris
Copy link
Author

Everybody has their own auto-deployment process out there. This is the one I use.

Auto-deployment Setup

This setup assumes you already have a repository on github and the target repository is already initialized and set to the correct branch on the server to be auto-deployed.

  1. Set "SECRET_PHRASE" in $auth variable. This md5 hash must be set as a GET variable on the service hook in github. (eg. deploy.php?auth=HASH)
  2. Populate $args per your project.
  3. Ensure apache (or _www) user has permissions to perform operations: chown -r owner:apache dir/
  4. Upload deploy.php to webroot

Setup Apache User

  1. Create an ssh directory for the apache user
sudo mkdir /var/www/.ssh
sudo chown -R apache:apache /var/www/.ssh/
  1. Generate a deploy key for apache user
# you might need to be root if sudo doesn't work and do not use a passphrase
sudo -Hu apache ssh-keygen -t rsa 
sudo cat /var/www/.ssh/id_rsa.pub

Configure GitHub and setup service hook

  1. Add Key to either: https://github.com/settings/ssh OR https://github.com/barrel/{repo}/settings/keys
  2. Select the Post-Receive URL service hook: https://github.com/{user}/{repo}/admin/hooks

Enter the URL to your deployment script - http://server.com/deploy.php?auth=HASH (use md5 hash as created in first step)
Click Update Settings

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