Last active
December 28, 2015 00:19
-
-
Save vmassuchetto/7412299 to your computer and use it in GitHub Desktop.
PHP script to automate deploy using Git
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 | |
/** | |
* This scripts is intended to be triggered by Git hooks. For deployment you'll | |
* probably want to use `post-update`: | |
* http://schacon.github.io/git/githooks.html#post-update | |
* | |
* First, set the `PASS` constant to limit access to this script and use it as | |
* a `pass` GET variable. | |
* | |
* Here's an example of a `post-update` script for deployment when pushing only | |
* to the branch master. Edit the `your-repository/.git/hooks/post-update` | |
* file, and make it executable. | |
* | |
* #!/bin/sh | |
* case " $* " in | |
* *' refs/heads/master '*) | |
* curl -s http://your-website/deploy.php?pass=<value of PASS constant> | |
* ;; | |
* esac | |
*/ | |
define( 'PASS', 'some_password' ); | |
date_default_timezone_set('America/Sao_Paulo'); | |
class Deploy { | |
/** | |
* A callback function to call after the deploy has finished. | |
* | |
* @var callback | |
*/ | |
public $post_deploy; | |
/** | |
* The name of the file that will be used for logging deployments. Set to | |
* FALSE to disable logging. | |
* | |
* @var string | |
*/ | |
public $_log = 'deployments.log'; | |
/** | |
* The timestamp format used for logging. | |
* | |
* @link http://www.php.net/manual/en/function.date.php | |
* @var string | |
*/ | |
private $_date_format = 'Y-m-d H:i:sP'; | |
/** | |
* The name of the branch to pull from. | |
* | |
* @var string | |
*/ | |
private $_branch = 'master'; | |
/** | |
* The name of the remote to pull from. | |
* | |
* @var string | |
*/ | |
private $_remote = 'origin'; | |
/** | |
* The directory where your website and git repository are located, can be | |
* a relative or absolute path | |
* | |
* @var string | |
*/ | |
private $_directory; | |
/** | |
* Sets up defaults. | |
* | |
* @param string $directory Directory where your website is located | |
* @param array $data Information about the deployment | |
*/ | |
public function __construct($directory, $options = array()) | |
{ | |
// Determine the directory path | |
$this->_directory = realpath($directory).DIRECTORY_SEPARATOR; | |
$available_options = array('log', 'date_format', 'branch', 'remote'); | |
foreach ($options as $option => $value) | |
{ | |
if (in_array($option, $available_options)) | |
{ | |
$this->{'_'.$option} = $value; | |
} | |
} | |
$this->log('Attempting deployment...'); | |
} | |
/** | |
* Writes a message to the log file. | |
* | |
* @param string $message The message to write | |
* @param string $type The type of log message (e.g. INFO, DEBUG, ERROR, etc.) | |
*/ | |
public function log($message, $type = 'INFO') | |
{ | |
if ($this->_log) | |
{ | |
// Set the name of the log file | |
$filename = $this->_log; | |
if ( ! file_exists($filename)) | |
{ | |
// Create the log file | |
file_put_contents($filename, ''); | |
// Allow anyone to write to log files | |
chmod($filename, 0666); | |
} | |
// Write the message into the log file | |
// Format: time --- type: message | |
file_put_contents($filename, date($this->_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND); | |
} | |
} | |
/** | |
* Executes the necessary commands to deploy the website. | |
*/ | |
public function execute() | |
{ | |
try | |
{ | |
// Make sure we're in the right directory | |
exec('cd '.$this->_directory, $output); | |
$this->log('Changing working directory... '.implode(' ', $output)); | |
// Discard any changes to tracked files since our last deploy | |
exec('git reset --hard HEAD', $output); | |
$this->log('Reseting repository... '.implode(' ', $output)); | |
// Update the local repository | |
exec('git pull '.$this->_remote.' '.$this->_branch, $output); | |
$this->log('Pulling in changes... '.implode(' ', $output)); | |
// Secure the .git directory | |
exec('chmod -R og-rx .git'); | |
$this->log('Securing .git directory... '); | |
if (is_callable($this->post_deploy)) | |
{ | |
call_user_func($this->post_deploy, $this->_data); | |
} | |
$this->log('Deployment successful.'); | |
} | |
catch (Exception $e) | |
{ | |
$this->log($e, 'ERROR'); | |
} | |
} | |
} | |
if ( empty( $_GET['pass'] ) || $_GET['pass'] != PASS ) | |
die( 'Permission denied.' ); | |
$directory = dirname( __FILE__ ); | |
$deploy = new Deploy( dirname( $directory ) ); | |
$deploy->execute(); | |
if ( is_file( $directory . '/' . $deploy->_log ) ) { | |
echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'; | |
echo '<pre>'; | |
echo file_get_contents( $deploy->_log ); | |
echo '</pre>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment