/** * Handle commit hook post from bitbucket or github * @return string */ public function commit_hook() { if (isset($_POST['payload'])) { // github and bitbucket use a 'payload' parameter $json = $_POST['payload']; } else { $json = file_get_contents('php://input'); } if (!$json) { DTLog::debug('ignored #1'); return 'ignored'; } $data = $json ? json_decode($json, true) : null; if (!$data || !is_array($data['commits'])) { DTLog::debug('ignored #2'); return 'ignored'; } // look through the commits $found = false; $tags = array(); foreach ($data['commits'] as $commit) { if (preg_match('/\\[deploy(:.+)?\\]/', $commit['message'], $matches)) { $found = true; if (count($matches) > 1 && $matches[1] != '') { $tags[] = substr($matches[1], 1); } else { $tags[] = 'live'; } } } if (!$found) { return 'ignored'; } if (defined('DEPLOY_TAG') && !in_array(DEPLOY_TAG, $tags)) { DTLog::debug('ignored #3'); return 'ignored'; } // create the deployment increase_time_limit_to(600); $deploy = new Deploy(BASE_PATH, array()); $deploy->post_deploy = function () use($deploy) { global $_FILE_TO_URL_MAPPING; // composer install if detected if (file_exists(BASE_PATH . DIRECTORY_SEPARATOR . 'composer.json')) { if (file_exists('/usr/local/bin/composer')) { // TODO: more flexible composer detection exec('composer install', $output); DTLog::info('Executing composer install...' . implode("\n", $output)); //Checking for composer.phar } elseif (file_exists('/usr/local/bin/composer.phar')) { exec('/usr/local/bin/composer.phar install', $output); DTLog::info('Executing composer install...' . implode("\n", $output)); } else { DTLog::info('composer.json detected but unable to locate composer.'); } } // clear cache DTLog::info('Clearing cache...'); DeployController::clear_cache(); // update database if (isset($_FILE_TO_URL_MAPPING[BASE_PATH])) { exec('php framework/cli-script.php dev/build', $output2); DTLog::info('Updating database...' . implode("\n", $output2)); } else { DTLog::info('Database not updated. $_FILE_TO_URL_MAPPING must be set for ' . BASE_PATH); } // SS_ClassLoader::instance()->getManifest()->regenerate(); // ob_start(); // DatabaseAdmin::create()->doBuild(false, true, false); // DTLog::info('dev/build complete: '.ob_get_contents()); // ob_end_clean(); }; $deploy->execute(); return 'ok'; }
* Executes the necessary commands to deploy the website. */ public function execute() { try { // Update the local repository exec('cd ' . $this->_git_dir . ' && ' . $this->_git_bin_path . ' fetch', $output); $this->log('Fetching changes... ' . implode(' ', $output)); // Checking out to web directory exec('cd ' . $this->_git_dir . ' && GIT_WORK_TREE=' . $this->_www_dir . ' ' . $this->_git_bin_path . ' checkout -f', $output); $this->log('Checking out changes to www directory... ' . implode(' ', $output)); 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'); } } } $deploy = new Deploy($git_serverpath, $www_serverpath); /* $deploy->post_deploy = function() use ($deploy) { // hit the wp-admin page to update any db changes exec('curl http://example.com/wp-admin/upgrade.php?step=upgrade_db'); $deploy->log('Updating wordpress database... '); }; */ if ($_GET[key] === $secret_key) { $deploy->execute(); }