Skip to content

Instantly share code, notes, and snippets.

@zorca
Created March 4, 2019 19:31
Show Gist options
  • Save zorca/8d5e18d1ce774a7caeb0f38f7812152f to your computer and use it in GitHub Desktop.
Save zorca/8d5e18d1ce774a7caeb0f38f7812152f to your computer and use it in GitHub Desktop.
<?php
namespace Deployer;
use Automate\CloudwaysApiClient;
require_once __DIR__ . '/vendor/autoload.php';
require 'recipe/common.php';
// Functions
/**
* Execute commands on local machine without environment variables
*
* @param string $command Command to run locally.
* @param array $options
* @return string Output of command.
*/
function runLocallyClean($command, $options = [])
{
$process = Deployer::get()->processRunner;
$command = parse($command);
$output = $process->run('localhost', $command, $options);
return rtrim($output);
}
// Windows or NOT
function check_win()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return true;
}
return false;
}
// Configuration file
inventory('deploy.yml');
// Shared files/dirs between deploys
set('shared_files', []);
set('shared_dirs', []);
// Writable dirs by web server
set('writable_dirs', []);
// Date
set('date', date('d-m-Y_H-i-s'));
// Fix for Runcloud server
//set('env', [
// 'PATH' => '/home/runcloud/bin:/home/runcloud/.local/bin:/RunCloud/Packages/php71rc/bin:/RunCloud/Packages/httpd-rc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin',
//]);
// Tasks
desc('Test task');
task('test', function () {
run('php -v > php_version.txt');
});
desc('Export current database');
task('local:export:database', function () {
$local_database_current = 'current.sql';
runLocallyClean('wp db export --add-drop-table '.$local_database_current);
});
desc('Upload current database');
task('local:upload:database', function () {
upload('current.sql', get('domain_folder').'/current.sql');
});
// Import database
desc('Database import');
task('import:db', function () {
run('cd '.get('domain_folder').' && wp db import current.sql');
// Search-replace domain name
run('cd '.get('domain_folder').' && wp search-replace "//'.get('domain_local').'" "//'.get('domain_remote').'"');
run('cd '.get('domain_folder').' && wp search-replace "no-reply@'.get('domain_local').'" "no-reply@'.get('domain_remote').'"');
});
// Push all from local to remote
desc('Push all from local to remote');
task('push', [
'push:wp-content',
'push:db',
]);
// Push db from local to remote
desc('Push db from local to remote');
task('push:db', [
'local:export:database',
'local:upload:database',
'import:db'
]);
// Push wp-content from local to remote
desc('Push wp-content from local to remote');
task('push:wp-content', function () {
runLocally('rsync -avz --delete web/app/ '.get('user').'@'.get('hostname').':'.get('domain_folder').'/web/app/');
});
// Export remote database
desc('Export remote database');
task('remote:export:database', function () {
run('cd '.get('domain_folder').' && wp db export --add-drop-table current.sql');
});
// Pull remote database
desc('Pull remote database');
task('remote:download:database', function () {
upload('current.sql', get('domain_folder').'/current.sql');
});
// Elementor fix
desc('Elementor push fix');
task('elementor:fix', function () {
if (get('stage') == 'production') {
run('cd '.get('domain_folder').' && wp elementor replace-urls "'.get('local_protocol').'//'.get('domain_local').'" "'.get('remote_protocol').'//'.get('domain_remote').'" && wp elementor flush_css');
}
});
desc('Deploy your project');
task('deploy', [
'push:wp-content',
'perm:fix',
'push:db',
'elementor:fix',
]);
// Push wp-content from local to remote
desc('Push themes from local to remote');
task('push:themes', function () {
runLocally('rsync -avz --delete web/app/themes/ '.get('user').'@'.get('hostname').':'.get('domain_folder').'/web/app/themes/');
});
// [Optional] If deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment