/** * This is a generic form, but it will probably work in most cases. * * @param array $fields * @return array */ public function addInstallFormFields(array $fields) { $fields[] = HiddenField::create('RepoType', '', $this->getType()); $fields[] = HiddenField::create('RepoID', '', $this->repoID); $fields[] = LiteralField::create('repo', sprintf("<p>" . "You appear to be deploying from %s (%s.git). " . "If you enter your username and password below we will set up a service hook to deploy automatically. " . "Your credentials will not be logged or saved." . "</p>", $this->getHumanName(), $this->repoID)); $fields[] = TextField::create('PostURL', 'Commit Hook URL (must be unique to this server)', DeployController::default_hook_url()); $fields[] = TextField::create('ApiUser', $this->getHumanName() . ' Username'); $fields[] = PasswordField::create('ApiPassword', $this->getHumanName() . ' Password'); return $fields; }
/** * 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'; }