diff --git a/blt/src/Blt/Plugin/Commands/HsCommands.php b/blt/src/Blt/Plugin/Commands/HsCommands.php index 1ea3d7b2e..bc83770be 100644 --- a/blt/src/Blt/Plugin/Commands/HsCommands.php +++ b/blt/src/Blt/Plugin/Commands/HsCommands.php @@ -24,9 +24,10 @@ class HsCommands extends BltTasks { * * @aliases humsci:post-code-update */ - public function postCodeDeployUpdate() { + public function postCodeDeployUpdate($target_env, $deployed_tag) { $sites = $this->getConfigValue('multisites'); - $parallel_executions = 10; + $parallel_executions = (int) getenv('UPDATE_PARALLEL_PROCESSES') ?: 10; + $site_chunks = array_chunk($sites, ceil(count($sites) / $parallel_executions)); $commands = []; foreach ($site_chunks as $sites) { @@ -50,11 +51,42 @@ public function postCodeDeployUpdate() { $failed[] = $site; } } + unlink(sys_get_temp_dir() . '/update-report.txt'); + $this->yell(sprintf('Updated %s sites successfully.', count($success)), 100); + if ($failed) { $this->yell(sprintf("Update failed for the following sites:\n%s", implode("\n", $failed)), 100, 'red'); + + if (in_array($target_env, ['prod', 'test'])) { + $count = count($failed); + $this->sendSlackNotification("A new deployment has been made to *$target_env* using *$deployed_tag*.\n\n*$count* sites failed to update."); + } + throw new \Exception('Failed update'); } - unlink(sys_get_temp_dir() . '/update-report.txt'); + + if (in_array($target_env, ['prod', 'test'])) { + $this->sendSlackNotification("A new deployment has been made to *$target_env* using *$deployed_tag*."); + } + } + + /** + * Send out a slack notification. + * + * @param string $message + * Slack message. + */ + protected function sendSlackNotification(string $message) { + $client = new Client(); + $client->post(getenv('SLACK_NOTIFICATION_URL'), [ + 'form_params' => [ + 'payload' => json_encode([ + 'username' => 'Acquia Cloud', + 'text' => $message, + 'icon_emoji' => 'information_source', + ]), + ], + ]); } /** diff --git a/hooks/common/post-code-deploy/post-code-deploy.sh b/hooks/common/post-code-deploy/post-code-deploy.sh index f0d34dcd7..28a359d20 100644 --- a/hooks/common/post-code-deploy/post-code-deploy.sh +++ b/hooks/common/post-code-deploy/post-code-deploy.sh @@ -24,6 +24,6 @@ repo_root="/var/www/html/$site.$target_env" export PATH=$repo_root/vendor/bin:$PATH cd $repo_root -blt humsci:post-code-deploy +blt humsci:post-code-deploy $target_env $deployed_tag set +v diff --git a/hooks/common/post-code-update/post-code-update.sh b/hooks/common/post-code-update/post-code-update.sh index f91897a9f..ae324c744 100644 --- a/hooks/common/post-code-update/post-code-update.sh +++ b/hooks/common/post-code-update/post-code-update.sh @@ -27,6 +27,6 @@ repo_root="/var/www/html/$site.$target_env" export PATH=$repo_root/vendor/bin:$PATH cd $repo_root -blt humsci:post-code-deploy +blt humsci:post-code-deploy $target_env $deployed_tag set +v diff --git a/hooks/prod/post-code-deploy/slack.sh b/hooks/prod/post-code-deploy/slack.sh deleted file mode 100644 index 813d162b7..000000000 --- a/hooks/prod/post-code-deploy/slack.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# Cloud Hook: post-code-deploy -# -# The post-code-deploy hook is run whenever you use the Workflow page to -# deploy new code to an environment, either via drag-drop or by selecting -# an existing branch or tag from the Code drop-down list. See -# ../README.md for details. -# -# Usage: post-code-deploy site target-env source-branch deployed-tag repo-url -# repo-type - -site="$1" -target_env="$2" -source_branch="$3" -deployed_tag="$4" -repo_url="$5" -repo_type="$6" - -# Load the Slack webhook URL (which is not stored in this repo). -. $HOME/slack_settings - -# Post deployment notice to Slack -curl -X POST --data-urlencode "payload={\"username\": \"Acquia Cloud\", \"text\": \"A new deployment has been made to *$target_env* using *$deployed_tag*.\", \"icon_emoji\": \":information_source:\"}" $SLACK_WEBHOOK_URL diff --git a/hooks/test/post-code-update/.gitignore b/hooks/test/post-code-update/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/hooks/test/post-code-update/slack.sh b/hooks/test/post-code-update/slack.sh deleted file mode 100644 index f0ea6eebd..000000000 --- a/hooks/test/post-code-update/slack.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -# -# Cloud Hook: post-code-deploy -# -# The post-code-deploy hook is run whenever you use the Workflow page to -# deploy new code to an environment, either via drag-drop or by selecting -# an existing branch or tag from the Code drop-down list. See -# ../README.md for details. -# -# Usage: post-code-deploy site target-env source-branch deployed-tag repo-url -# repo-type - -site="$1" -target_env="$2" -source_branch="$3" -deployed_tag="$4" -repo_url="$5" -repo_type="$6" - -# Load the Slack webhook URL (which is not stored in this repo). -. $HOME/slack_settings - -# Post deployment notice to Slack -curl -X POST --data-urlencode "payload={\"username\": \"Acquia Cloud\", \"text\": \"A new deployment has been made to *$target_env* using tag *$deployed_tag*.\", \"icon_emoji\": \":information_source:\"}" $SLACK_WEBHOOK_URL