Created
October 11, 2019 00:38
-
-
Save twfahey1/73b9a1b0e91476221f77605f876fcae8 to your computer and use it in GitHub Desktop.
Drush script for automatic updates and commits
This file contains 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
#!/usr/bin/env drush | |
<?php | |
/** | |
* This script updates eligible modules and commits automatically. | |
* | |
* To use this, place this script in your site docroot. Run it with | |
* `drush update-modules.php`. | |
* This script will need the "eligible_modules" key defined in a | |
* drushrc.php or drushrc.local.php., e.g.: | |
* $options['eligible_modules'] = [ | |
* 'admin_toolbar', | |
* 'config_base', | |
* ] | |
* | |
*/ | |
// check if we can bootstrap | |
$self = drush_sitealias_get_record('@self'); | |
if (empty($self)) { | |
drush_die("I can't bootstrap from the current location.", 0); | |
} | |
drush_print("Time to prepare the working environment."); | |
// let's jump to our site directory before we do anything else | |
drush_op('chdir', $self['root']); | |
$updateHelper = new GitModuleUpdater(); | |
$modules = drush_get_option("eligible_modules"); | |
$updateHelper->doSecurityUpdates($modules); | |
class GitModuleUpdater { | |
/** | |
* Helper method to perform a commit of a module folder. | |
* | |
* @string $folder | |
* @string null $message | |
*/ | |
public function addFolder($folder, $message = NULL) { | |
shell_exec("git add $folder"); | |
} | |
/** | |
* Commit changes with a message | |
* @string $message | |
*/ | |
public function commit($message) { | |
shell_exec("git commit -m \"$message\""); | |
} | |
public function branch($name) { | |
shell_exec("git branch $name && git checkout $name"); | |
} | |
public function updateModule($module) { | |
drush_set_context('DRUSH_AFFIRMATIVE', TRUE); | |
drush_invoke_process("@self", "up", ["-y"], [$module]); | |
} | |
public function doSecurityUpdates($modules) { | |
$branch = "sec-update-" . date('Ymd', time()); | |
$this->branch($branch); | |
foreach ($modules as $module) { | |
$module_dir = drupal_get_path('module', $module); | |
if (isset($module_dir)) { | |
$this->updateModule($module); | |
$this->addFolder($module_dir); | |
$commit_message = "Update for $module"; | |
$this->commit($commit_message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment