Created
October 30, 2016 18:16
-
-
Save vgrish/3785476f1b67c9fdc4406dfb5b541f28 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
define('MODX_API_MODE', true); | |
require dirname(dirname(__FILE__)) . '/index.php'; | |
// sh | |
$hookfile = MODX_BASE_PATH . '.hooks/gitlab-pull.sh'; | |
$buildfile = MODX_BASE_PATH . 'name/_build/build.transport.php'; | |
// refs | |
$ref = 'refs/heads/master'; | |
// $ref = array('refs/heads/master', 'refs/heads/develop'); | |
// password | |
$password = '12345'; | |
function log_append($message, $time = null) | |
{ | |
global $modx; | |
$pre = ' (' . $_SERVER['REMOTE_ADDR'] . '): '; | |
$modx->log(1, print_r($pre . $message, 1)); | |
} | |
function exec_command($command) | |
{ | |
$output = array(); | |
exec($command, $output); | |
foreach ($output as $line) { | |
log_append('shell: ' . $line); | |
} | |
} | |
if (isset($password)) | |
{ | |
if (empty($_REQUEST['p'])) { | |
log_append('Не указан пароль'); | |
die(); | |
} | |
if ($_REQUEST['p'] !== $password) { | |
log_append('Неправильный пароль'); | |
die(); | |
} | |
} | |
$input = file_get_contents("php://input"); | |
$json = json_decode($input); | |
if (!is_object($json) || empty($json->ref)) { | |
log_append('Получены неверные данные'); | |
die(); | |
} | |
if (isset($ref)) | |
{ | |
$_refs = (array) $ref; | |
if ($ref !== '*' && !in_array($json->ref, $_refs)) { | |
log_append('Не работаем ' . $json->ref); | |
die(); | |
} | |
} | |
log_append('старт...'); | |
exec_command('sh ' . $hookfile); | |
log_append('финиш'); | |
// build action | |
$js = $modx->fromJSON($input); | |
if (strpos($js['commits'][0]['message'], '[build]') === false) { | |
// no build | |
} | |
else { | |
// build | |
log_append('старт сборки...'); | |
exec_command('php ' . $buildfile); | |
log_append('финиш сборки пакета'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment