Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lcherone/e2d48c8f5fbfec7b3da7 to your computer and use it in GitHub Desktop.
Save lcherone/e2d48c8f5fbfec7b3da7 to your computer and use it in GitHub Desktop.
Git Deployment PHP class
<?php
// Set your local timezone - http://www.php.net/manual/en/timezones.php
date_default_timezone_set('Europe/London');
/**
* Git Repo Deploment class.
* @version 1-2
* @author (original) arindambiswas <http://www.collectivezen.com>
* @link https://gist.github.com/arindambiswas/4634fff09b3ad4e4bf48
*
* @version 3
* @author lcherone <http://iCodes.it>
* @link https://gist.github.com/lcherone/e2d48c8f5fbfec7b3da7
*
*/
class Deploy {
/**
* The name of the file that will be used for logging deployments. Set to
* FALSE to disable logging.
*
* @var string
*/
private $_log = './deploy.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;
private $_cwd;
/**
* 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 paths
$this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
$this->_cwd = realpath(getcwd()).DIRECTORY_SEPARATOR;
}
/**
* Writes a message to the log file.
*
* @param string $message The message to write
*/
public function log($message)
{
// change to scripts working directory
chdir($this->_cwd);
// echo the log message
echo $message.'<br>';
// log it to file if enabled
if ($this->_log != false) {
if (!file_exists($this->_log)) {
// create the log file
file_put_contents($this->_log, '');
// allow anyone to write to log files
chmod($this->_log, 0666);
}
// write the message into the log file
// format: time --- type: message
file_put_contents($this->_log,
date($this->_date_format).' : '.$message.PHP_EOL, FILE_APPEND);
}
// change back to repos working directory
chdir($this->_directory);
}
/**
* Executes the necessary commands to deploy the website.
*/
public function execute()
{
try {
$this->log('Attempting deployment.');
// make sure we're in the right directory
chdir($this->_directory);
$this->log('Changing working directory.');
// discard any changes to tracked files since our last deploy
exec('git reset --hard HEAD', $output);
if(empty($output))
throw new Exception ('Error resetting local repo HEAD');
$this->log('Reseting repository: '.implode(' ', $output));
// update the local repository
exec('git pull '.$this->_remote.' '.$this->_branch, $output);
if(empty($output[1]))
throw new Exception ('Error pulling in changes');
$this->log('Pulling in changes: '.implode(' ', $output));
// secure the .git directory
exec('chmod -R og-rx .git', $output);
$this->log('Securing .git directory.');
$this->log('Deployment successful!');
}catch(Exception $e) {
$this->log($e->getMessage());
}
}
}
/**
* The root directory where the repos live, relative to script or use absolute.
*
* @var string
*/
$root_dir = '../Git_Projects/';
/**
* Name of the repo to pull
*
* @var string
*/
$repo = 'tridentCMS';
/**
* Go deployment!
*/
$deploy = new Deploy($root_dir.$repo);
$deploy->execute();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment