unemployment Posted April 11, 2013 Share Posted April 11, 2013 I'm in the process of trying to set up automatic deployments when I make a git push to my bitbucket repository. I have a php deploy script that I leveraged from http://brandonsummers.name/blog/2012/02/10/using-bitbucket-for-automated-deployments/ but when the script runs it is logging that it's only updating from a previous commit. Here is an example. Let say I log into my server and type git pull. The server will update with the latest changes and lets say the hash for that commit was 001. However if I make serveral commits lets call them 002, 003, and 004 my script should run every time assuming I pushed those changes to bitbucket after every commit. The script runs but every time it will keep the changes from 001. Only when I log into my server and type git pull, will the server update to 004. Do you know what would cause this? <?php $ajax = true; include('assets/init.inc.php'); // date_default_timezone_set('America/Los_Angeles'); // Also, this Class is not very secure... $path = dirname(__FILE__); 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 */ private $_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; /** * The IP address from where BitBucket will be sending the IP address (Davey) * * @var string */ private $_ip = '63.246.22.222'; /** * The IP address from where BitBucket will be sending the IP address (Davey) * * @var string */ private $_request_method = 'POST'; /** * Error variable for processing * * @var bool */ private $_error = false; /** * 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()) { $this->log('Starting...'); if (stripos($this->_request_method, $_SERVER['REQUEST_METHOD']) === false) { $this->_error = true; $this->log('Request method "'.$_SERVER['REQUEST_METHOD'].'" is not valid', 'ERROR'); } else { $this->log('Request method "'.$_SERVER['REQUEST_METHOD'].'": passed. Checking IP...'); if (!empty($_SERVER['REMOTE_ADDR'])) { if (strlen($this->_ip) > 0) { if (stripos($this->_ip, $_SERVER['REMOTE_ADDR']) !== false) { $this->log('IP "'.$_SERVER['REMOTE_ADDR'].'": passed. Continuing... '); } else { $this->_error = true; $this->log('This ip "'.$_SERVER['REMOTE_ADDR'].'" is not allowed', 'ERROR'); } } if ($this->_error !== true) { // 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...'); } } else { $this->log('Missing REMOTE_ADDR', 'ERROR'); } } } /** * 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'); } } } // This is just an example $deploy = new Deploy('/var/www/trekeffect.com/trekeffect.com'); // $deploy->post_deploy = function() use ($deploy) { // hit the wp-admin page to update any db changes // exec('curl http://www.foobar.com/wp-admin/upgrade.php?step=upgrade_db'); // $deploy->log('Done.'); // }; // file_put_contents("{$GLOBALS['path']}/deployments.log", date('c'), FILE_APPEND); $deploy->execute(); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.