|
<?php |
|
function run($cmdline, ...$args) { |
|
$cmd = vsprintf($cmdline, array_map('escapeshellarg', $args)); |
|
printf(">> %s\n", $cmd); |
|
passthru($cmd); |
|
} |
|
|
|
|
|
$remote = $_SERVER['argv'][1] ?? 'production'; |
|
|
|
|
|
$c = json_decode(@file_get_contents('composer.json')); |
|
if (!$c) { |
|
throw new Exception('Could not read composer.json'); |
|
} |
|
|
|
if (!isset($c->extra->remote->production)) { |
|
throw new Exception('Invalid SSH format. Use: composer config extra.remote.' . $remote . ' ssh://user@host:22/absolute/path/to/root/repo'); |
|
} |
|
|
|
$url = $c->extra->remote->{$remote}; |
|
$p = parse_url($url); |
|
|
|
if (!isset($p['scheme']) || $p['scheme'] !== 'ssh' || !isset($p['host']) || !isset($p['path'])) { |
|
throw new Exception('Invalid SSH format. Use: composer config extra.remote.' . $remote . ' ssh://user@host:22/absolute/path/to/root/repo'); |
|
} |
|
|
|
if (basename($p['path']) != 'repo') { |
|
throw new Exception('Invalid SSH format. The directory MUST include the git repo, e.g: composer config extra.remote.' . $remote . ' ssh://user@host:22/absolute/path/to/root/repo'); |
|
} |
|
|
|
$ssh_cmd = 'ssh' . (isset($p['port']) ? ' -p' . $p['port'] : ''); |
|
$user_host = (isset($p['user']) ? $p['user'] . '@' : '') . $p['host']; |
|
|
|
$ssh_run = function($cmdline, ...$args) use ($ssh_cmd, $user_host) { |
|
$cmd = vsprintf($cmdline, array_map('escapeshellarg', $args)); |
|
run($ssh_cmd . ' ' . $user_host . ' ' . escapeshellarg($cmd)); |
|
}; |
|
|
|
|
|
# Upload files using rsync |
|
run("rsync -e %s -avz --include 'web/' --include 'web/fileadmin/***' --include='web/uploads/***' --include='web/typo3conf/' --include='web/typo3conf/l10n/' --include='web/typo3conf/l10n/***' --exclude='*' ./ %s:%s/shared/", |
|
$ssh_cmd, $user_host, dirname($p['path'])); |
|
|
|
# Init remote git (deployment endpoint) |
|
$ssh_run('git init --bare %1$s && curl -s %2$s -o %1$s/hooks/update && chmod +x %1$s/hooks/update', $p['path'], 'https://raw.githubusercontent.com/bnf/giddyup/master/update-hook'); |
|
run('git remote set-url %1$s %2$s 2>/dev/null || git remote add %1$s %2$s', $remote, $url); |
|
run('git push %1$s HEAD:master', $remote); |
|
|
|
# Upload database |
|
run('./vendor/bin/typo3cms database:export | ' . $ssh_cmd . ' %s %s/current/vendor/bin/typo3cms database:import', $user_host, dirname($p['path'])); |