Last active
December 24, 2015 10:08
-
-
Save wturnerharris/6781587 to your computer and use it in GitHub Desktop.
Auto-deployment script for php, github, apache.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Setup Apache User
Configure GitHub and setup service hook
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