Skip to content

Instantly share code, notes, and snippets.

@bnf
Last active December 11, 2017 13:32
Show Gist options
  • Save bnf/e644ff9f1c2aec2ec83e25598e011b56 to your computer and use it in GitHub Desktop.
Save bnf/e644ff9f1c2aec2ec83e25598e011b56 to your computer and use it in GitHub Desktop.
TYPO3 init-remote.php

Usage

HEADS UP! It's important to add a trailing folder named "/repo". That folder is the deployment endpoint and is named "repo" by convention. The script will create folders /path/to/root/repo and /path/to/root/releases and a symlink /path/to/root/current pointing to the current version in the releases folder. That means when using a web-dir called "web" your webserver's document root needs to point to /path/to/root/current/web.

composer config extra.remote.production ssh://user@host:22/absolute/path/to/root/repo
php init-remote.php production
<?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']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment