Commit d9e721c2 authored by Jelle den Butter's avatar Jelle den Butter 💧
Browse files

Add all tasks for Drupal 8

parent d3bc80ef
......@@ -2,4 +2,266 @@
namespace Deployer;
use Deployer\Exception\Exception;
require_once __DIR__ . '/common.php';
// Gitlab repository for this project
set('repository', "git@gitlab.frmwrk.nl:drupal/" . get('application') . ".git");
// Set Gitlab variables
set('gitlab_api_url', 'https://gitlab.frmwrk.nl/api/v4');
set('client_repo_id', 25);
set('private_token', getenv('FRMWRK_TOKEN'));
// Set configurations for deployer and environments.
set('keep_releases', 3);
// Default config values.
set('backup_path', '{{deploy_path}}/shared/backups');
// Add server credentials
if (!file_exists('creds.yml')) {
file_put_contents('creds.yml', file_get_contents(get('gitlab_api_url') . '/projects/' . get('client_repo_id') . '/repository/files/' . get('application') . '%2Fservers.yml/raw?ref=master&private_token=' . get('private_token')));
}
inventory('creds.yml');
// Define tasks
desc('Host settings');
task('deploy:settings', function () {
// Default config values for sites.
foreach (get('sites') as $site) {
$files_dirs[] = get('webroot') . 'sites/' . $site['folder'] . '/files';
$files_dirs[] = get('webroot') . 'sites/' . $site['folder'] . '/private';
$shared_files[] = get('webroot') . 'sites/' . $site['folder'] . '/settings.php';
}
$files_dirs[] = 'private';
set('shared_dirs', $files_dirs);
set('writable_dirs', $files_dirs);
set('shared_files', $shared_files);
})->addBefore('deploy');
desc('Check code quality');
task('quality:code_quality', function () {
runLocally('grumphp run');
});
desc('Validate packages');
task('quality:validate_packages', function () {
runLocally('composer validate');
});
desc('Composer packages');
task('packages:composer_packages', function () {
runLocally('composer install --verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloader');
});
desc('NPM packages');
task('packages:npm_packages', function () {
// Check for Yarn
$yarnVersion = runLocally('which yarn')->toString();
if ($yarnVersion != '') {
$name = 'yarn';
} else {
$name = 'npm';
}
// Yarn/NPM Install
// New Drupal 8 standard
if (runLocally("if [ -f web/themes/custom/" . get('theme_name') . "/package.json ]; then echo 'true'; fi")->toBool()) {
runLocally("cd web/themes/custom/" . get('theme_name') . " && " . $name . " install", 240);
}
// Drupal 7 and old Drupal 8 standard
if (runLocally("if [ -f package.json ]; then echo 'true'; fi")->toBool()) {
runLocally($name . " install", 240);
}
// Bundle install
// New Drupal 8 standard
if (runLocally("if [ -f web/themes/custom/" . get('theme_name') . "/Gemfile ]; then echo 'true'; fi")->toBool()) {
runLocally("cd web/themes/custom/" . get('theme_name') . " && bundle install ", 120);
}
// Drupal 7 and old Drupal 8 standard
if (runLocally("if [ -f Gemfile ]; then echo 'true'; fi")->toBool()) {
runLocally("bundle install ", 120);
}
});
desc('Grunt');
task('build:grunt', function () {
// Check for Yarn
$yarnVersion = runLocally('which yarn')->toString();
if ($yarnVersion != '') {
$name = 'yarn';
} else {
$name = 'npm';
}
// If Grunt is not standardized then set 'grunt_task' in environment.
try {
if ($name != 'yarn') {
$name = '';
}
// New Drupal 8 standard
if (runLocally("if [ -d web/themes/custom/" . get('theme_name') . " ]; then echo 'true'; fi")->toBool()) {
runLocally("cd web/themes/custom/" . get('theme_name') . " && " . $name . " grunt " . get('grunt_task'), 120);
} else {
// Drupal 7 and old Drupal 8 standard
runLocally("grunt " . get('grunt_task'), 120);
}
} catch (Exception $e) {
if (runLocally("if [ -d sites/all/themes/" . get('theme_name') . "/less ]; then echo 'true'; fi")->toBool()) {
writeln('Old less project, so no need to grunt it');
} else {
runLocally("grunt css");
}
}
});
/*
* Build and upload an angular app
*/
desc('Build Angular app');
task('build:angular', function () {
// Check if Angular-CLI is installed and compile Angular app
$ngVersion = runLocally('which ng')->toString();
if (get('ng_folder') != '') {
if ($ngVersion != '') {
runlocally("cd " . get('ng_folder') . " && npm install", 300);
if ((null !== get('ng_aot')) && get('ng_aot') == TRUE) {
runLocally("cd " . get('ng_folder') . " && ng build --prod --aot=false --environment=" . get('ng_env') . " --base-href /" . get('ng_base_href') . "/", 300);
} else {
runLocally("cd " . get('ng_folder') . " && ng build --prod --environment=" . get('ng_env') . " --base-href /" . get('ng_base_href') . "/", 300);
}
}
}
});
desc('Phpunit');
task('test:phpunit', function () {
runLocally('vendor/bin/phpunit -c web/core/ --testsuite=unit');
});
desc('Run custom shell commands');
task('deploy:custom_shell', function () {
$commands = get('custom_shell');
if ($commands) {
run($commands);
}
});
desc('Diff the git versions');
task('deploy:diff', function () {
$commit = run('cd {{current}} && git rev-parse HEAD');
$run_env = 'run';
$go_to_current = '';
if (input()->getOption('docker')) {
$run_env = 'runLocally';
} else {
run('cd {{current}} && git fetch --all');
$go_to_current = "cd {{current}} && ";
}
$diff = $run_env("$go_to_current git log --pretty=oneline --stat $commit ..origin/{{branch}}");
writeln('Commit:' . $commit);
writeln($diff);
});
/*
* Download all db's from the current release to local
*/
desc('Download current db.');
task('drupal:downloaddb', function () {
foreach (get('sites') as $site) {
run("mkdir -p {{deploy_path}}/shared/backups/{{release}}");
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush sql-dump | gzip -9 > {{deploy_path}}/shared/backups/{{release}}/' . $site['folder'] . '.' . input()->getArgument('stage') . '.sql.gz');
download('../' . $site['folder'] . '.' . input()->getArgument('stage') . '.sql.gz', '{{deploy_path}}/shared/backups/{{release}}/' . $site['folder'] . '.' . input()->getArgument('stage') . '.sql.gz');
}
});
/*
* Create and upload .htaccess using values from yml file
*/
desc('Build .htaccess file on root level @ the given environment');
task('drupal:htaccess', function () {
$releases = get('releases_list');
$deployer_htaccess = [];
$deployer_htaccess[] = "# Custom htaccess generated by Deployer \n RewriteEngine On \n";
if ($htaccess = get('htaccess')) {
if (!empty($htaccess["whitelist"])) {
$deployer_htaccess[] = "Order deny,allow";
$deployer_htaccess[] = "Deny from all";
foreach ($htaccess["whitelist"] as $key => $value) {
$deployer_htaccess[] = "Allow from " . $value;
}
$deployer_htaccess[] = "Satisfy any";
writeln('Added ip whitelist');
}
if (!empty($htaccess["www_force"]) && $htaccess["www_force"] == TRUE) {
$deployer_htaccess[] = "RewriteCond %{HTTP_HOST} !^www\. [NC]";
$deployer_htaccess[] = "RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]";
writeln('Added www-force');
}
if (!empty($htaccess["non_www_force"]) && $htaccess["non_www_force"] == TRUE) {
$deployer_htaccess[] = "RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]";
$deployer_htaccess[] = "RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]";
writeln('Added non-www-force');
}
if (!empty($htaccess["ssl_force"]) && $htaccess["ssl_force"] == TRUE) {
$deployer_htaccess[] = "RewriteCond %{SERVER_PORT} !^443$";
$deployer_htaccess[] = "RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]";
writeln('Added https force');
}
if (!empty($htaccess["no_index"]) && $htaccess["no_index"] == TRUE) {
$deployer_htaccess[] = 'Header set X-Robots-Tag "noindex, follow"';
writeln('Added no-index');
}
if (!empty($htaccess["custom"])) {
$deployer_htaccess[] = $htaccess["custom"];
writeln('Added custom htaccess script');
}
$deployer_htaccess[] = "# End custom content";
$htaccess = implode($deployer_htaccess, "\n");
$htaccess .= file_get_contents(get('webroot') . '.htaccess');
file_put_contents($htaccess, '.htaccess');
}
});
desc('Deploy builded folder');
task('deploy:upload', function () {
$excludedPatterns = [
'*/.git',
'/*.md',
'/*.txt',
'sass/',
'deployer/',
'.gitlab-ci.yml',
'/patches',
'Gemfile.lock',
'Gemfile',
'Gruntfile',
];
foreach ($excludedPatterns as $pattern) {
$excludedOptions[] = "--exclude='$pattern'";
}
upload('vendor/', '{{release_path}}/');
upload(get('webroot'), '{{release_path}}/', $excludedOptions);
});
<?php
namespace Deployer;
require_once __DIR__ . '/frmwrk.common.php';
set('webroot', '.');
......@@ -2,5 +2,174 @@
namespace Deployer;
use Deployer\Exception\Exception;
require_once __DIR__ . '/frmwrk.common.php';
set('webroot', 'web/');
//Create and upload Drupal 7 settings.php using values from secrets
// Create and upload Drupal settings.php using values from secrets.
task('drupal:settings', function () {
$basepath = get('base_path');
foreach (get('sites') as $site) {
//Create settings from template
$deployer_settings = array();
$deployer_settings[] = "\n// Settings generated by Deployer";
if (!empty($site)) {
// Set error reporting
if (!empty($site['error_reporting'])) {
$deployer_settings[] = "error_reporting(" . $site['error_reporting'] . ');';
}
// Set php variables
if (!empty($site['php_variables'])) {
foreach ($site['php_variables'] as $key => $value) {
$deployer_settings[] = $key . ' = ' . var_export($value, TRUE) . ';';
}
}
// Create database credentials
if (!empty($site['databases'])) {
$deployer_settings[] = '$databases = ' . var_export($site['databases'], TRUE) . ';';
}
// Create settings (settings.php settings variables)
if (!empty($site['settings'])) {
$deployer_settings[] = '$settings = ' . var_export($site['settings'], TRUE) . ';';
}
// Set site configuration variables (Drupal variables)
if (!empty($site['config'])) {
foreach ($site['config'] as $key => $value) {
$new_key = str_replace('_dot_', '.', $key);
$site['config'][$new_key] = $value;
unset($site['config'][$key]);
}
$deployer_settings[] = '$config = ' . var_export($site['config'], TRUE) . ';';
}
if(!empty($site['config_directories'])) {
foreach ($site['config_directories'] as $key => $config_directory) {
$site['config_directories'][$key] = $config_directory;
}
$deployer_settings[] = '$config_directories = ' . var_export($site['config_directories'], TRUE) . ';';
}
// Create php ini_set lines
foreach ($site['ini_set'] as $key => $value) {
$deployer_settings[] = 'ini_set("' . $key . '","' . $value . '");';
}
// Create custom settings
try{
$deployer_settings[] = get('shared_settings');
}
catch (Exception $e){}
// Create custom lines
if (!empty($site['custom'])) {
$deployer_settings[] = $site['custom'];
}
}
$settings = file_get_contents($basepath. '/' . get('webroot') . 'sites/default/default.settings.php');
$settings .= implode($deployer_settings, "\n");
file_put_contents($settings, 'sites/' . $site['folder'] . '/settings.php');
}
});
desc('Rebuilding cache and removing obsolete folders');
task('drupal:cleanup', function () {
// Cleanup database backups in shares folder {{backup_path}}/201606211223/site.url.sql.gz
$releases = get('releases_list');
$keep = get('keep_releases');
while ($keep > 0) {
array_shift($releases);
--$keep;
}
foreach ($releases as $release) {
run("rm -rf " . get('backup_path') . "/$release");
}
});
/*
* Backup the drupal database to a backup folder for the current release.
*/
desc('Backup the database');
task('drupal:backup_db', function () {
// Create the release backup folders
run("mkdir -p " . get('backup_path') . "/{{release}}");
foreach (get('sites') as $site) {
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush sql-dump --gzip --result-file={{deploy_path}}/shared/backups/{{release}}/' . $site['folder'] . '.sql.gz');
}
});
/*
* Update the database.
*/
desc('Update the database');
task('drupal:update_db', function () {
foreach (get('sites') as $site) {
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && ' . 'drush updb -y');
}
});
/*
* Import configuration for the website.
*/
desc('Update all entities in Drupal to match code configuration');
task('drupal:entup', function () {
foreach (get('sites') as $site) {
write(run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush entup -y'));
}
});
/*
* Import configuration for the website.
*/
desc('Import configuration for the website');
task('drupal:cim', function () {
foreach (get('sites') as $site) {
$changes = run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush cim --preview=diff -n');
write($changes);
$changes = (strpos($changes, 'no changes') === FALSE) ? TRUE : FALSE;
if($changes && askConfirmation('Do you want to import this configuration?', FALSE)){
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush cim --preview=diff -y');
}
}
});
/*
* Clear the cache
*/
desc('Clear the drupal cache');
task('drupal:clear_cache', function () {
foreach (get('sites') as $site) {
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush cr all');
}
});
/*
* Update locale
*/
desc('Update locale');
task('drupal:locale_update', function () {
foreach (get('sites') as $site) {
run('cd {{current}}/' . get('webroot') . 'sites/' . $site['folder'] . ' && drush locale-check' . ' && drush locale-update');
}
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment