Created
February 20, 2013 12:39
-
-
Save gibtang/4995254 to your computer and use it in GitHub Desktop.
Automatic deployment to server using git pull with email notifications. Forked from https://gist.github.com/oodavid/1809044
Go to https://gist.github.com/oodavid/1809044 to read up on set up instructions, then use this script
This file contains 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 | |
/** | |
* GIT DEPLOYMENT SCRIPT | |
* | |
* Used for automatically deploying files via github or bitbucket | |
* | |
* Forked from https://gist.github.com/oodavid/1809044 and with thanks | |
*/ | |
function curPageURL()//get the url directory excluding the file name | |
{ | |
$pageURL = 'http'; | |
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} | |
$pageURL .= "://"; | |
if ($_SERVER["SERVER_PORT"] != "80") { | |
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].dirname($_SERVER['PHP_SELF']); | |
} else { | |
$pageURL .= $_SERVER["SERVER_NAME"].dirname($_SERVER['PHP_SELF']); | |
} | |
return $pageURL; | |
} | |
//Email setup | |
require_once "Mail.php"; | |
$git_dir = "mygitwork";//name of the git repo directory in this server | |
$debug_dir = "";//name of debug folder, if you have. Else you can leave it blank | |
//set up email notification after deployment | |
$from = "gbot@azukisoft.com";//can be a non-existing email address | |
$to = "recipient1@email.com, recipient2@email.com"; | |
$subject = "Deployment for " . $git_dir; | |
$body = "Here is the status\n"; | |
$host = "ssl://smtp.gmail.com";//this is assuming you use gmail | |
$port = "465"; | |
$username = "youremail@email.comm"; //your username from which the email is sent | |
$password = "yourpassword";//your password | |
$headers = array ('From' => $from, | |
'To' => $to, | |
'Subject' => $subject); | |
$smtp = Mail::factory('smtp', | |
array ('host' => $host, | |
'port' => $port, | |
'auth' => true, | |
'username' => $username, | |
'password' => $password)); | |
//end of email set up | |
//array of commands to do the git pull, git status etc, you can add your own commands here into this array | |
$commands = array( | |
'echo $PWD', | |
'whoami', | |
'git reset --hard HEAD', | |
'git pull', | |
'git status', | |
'git submodule sync', | |
'git submodule update', | |
'git submodule status', | |
); | |
//this assumes that the deploy.php is outside of your git repo. | |
//example, www.azukisoft.com/deploy.php is the location of this script | |
//www.azukisoft.com/moe_quiz is the directory that has the git repo | |
chdir($git_dir);//go to the git repo | |
// Execute the commands for output | |
$output = ''; | |
foreach($commands AS $command){ | |
// Run it | |
$tmp = shell_exec($command); | |
// Output | |
$output .= "<span style=\"color: #6BE234;\">\$</span> <span style=\"color: #729FCF;\"><B>{$command}</B>\n</span>"; | |
$output .= htmlentities(trim($tmp)) . "\n"; | |
$emailOutput .= "{$command}\n"; | |
$emailOutput .= trim($tmp) . "\n"; | |
} | |
$app_url = curPageURL() . "/" .$git_dir . "/" . $debug_dir;//Get the url to put in email for easy clicking | |
$body .= $emailOutput;//append output to email body | |
$body .= "Click here to access app " . $app_url; | |
//attempt to send email | |
$mail = $smtp->send($to, $headers, $body); | |
if (PEAR::isError($mail)) { | |
$emailSentStatus = $mail->getMessage(); | |
} else { | |
$emailSentStatus = "Email successfully sent"; | |
} | |
// Make it pretty for manual user access (and why not?) | |
?> | |
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>GIT DEPLOYMENT SCRIPT</title> | |
</head> | |
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;"> | |
<pre> | |
. ____ . ____________________________ | |
|/ \| | | | |
[| <span style="color: #FF0000;">♥ ♥</span> |] | Git Deployment Script v0.1 | | |
|___==___| / © Azukisoft 2012 | | |
|____________________________| | |
If git pull shows no output, that means git pull failed<BR> | |
<?php echo $output; ?> | |
<?php echo $app_url ?> | |
<BR> | |
<?php echo $emailSentStatus ?> | |
</pre> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment